<template>
  <el-dialog title="药品绑定" :visible.sync="dialogVisible" width="50%" center :before-close="handleClose">
    <div class="form">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="绑定药品" prop="productIds">
          <el-select v-model="form.productIds" placeholder="请选择绑定药品" class="w100" multiple filterable clearable>
            <el-option v-for="item in wmdadrugentdrugtableList" :label="item.drugname" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getWmdadrugentdrugtableListApi, userProdBindApi, getBindProdApi} from '@/api/admin/user';

export default {
  data() {
    return {
      dialogVisible: false,
      currentRow: {},
      type: 'add',
      form: {
        productIds: []
      },

      wmdadrugentdrugtableList: []
    };
  },
  created() {
    this.getWmdadrugentdrugtableList();
  },
  methods: {
    async addFn(row) {
      this.type = 'add';
      this.currentRow = row;
      const res = await getBindProdApi(row.userId);
      if (res.data.code === 0) {
        this.form.productIds = res.data.data;
      }

      this.dialogVisible = true;
    },
    async getWmdadrugentdrugtableList() {
      const res = await getWmdadrugentdrugtableListApi();
      if (res.data.code === 0) {
        this.wmdadrugentdrugtableList = res.data.data;
      }
    },
    async submit() {
      let obj = {
        productIds: this.form.productIds,
        userId: this.currentRow.userId
      };
      const res = await userProdBindApi(obj);
      if (res.data.data) {
        this.$message.success('绑定成功');
        this.$emit('success');
        this.handleClose();
      }
    },
    handleClose() {
      this.form.productIds = [];
      this.dialogVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped></style>
