<template>
  <div>
    <basic-container>
      <avue-crud ref="crud"
        v-model="form"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @search-change="searchChange"
        @search-reset="resetChange"
        @size-change="sizeChange"
        @current-change="currentChange">

      <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addClick">新增</el-button>
        </template>
        <template
          slot="menu"
          slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="editFn(scope.row)">编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="delFn(scope.row)">删除
            </el-button>
        </template>

        <template  slot="drugEntIdSearch">
          <el-select v-model="searchForm.drugEntName" allow-create clearable filterable placeholder="生产企业">
            <el-option :label="item.name" :value="item.name" v-for="item in drugEntInfo" :key="item.deptId"></el-option>
            </el-select>
          </template>
      </avue-crud>
    </basic-container>

    <el-dialog :title="type === 'add' ? '新增' : '编辑'" :visible.sync="dialogVisible"  width="60%" :before-close="handleClose">
      <div class="form">
        <el-form :model="formData" ref="form" :rules="rules" label-width="140px">
          <el-form-item label="器械名称：" prop="name">
             <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="生产厂家：" prop="manufacturer">
             <el-input v-model="formData.manufacturer"></el-input>
          </el-form-item>
          <el-form-item label="所属企业：" prop="drugEntId">
            <el-select v-model="formData.drugEntId" placeholder="请选择所属企业">
              <el-option :label="item.name" :value="item.deptId" v-for="item in drugEntInfo" :key="item.deptId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格：" prop="specification">
             <el-input v-model="formData.specification"></el-input>
          </el-form-item>
          <el-form-item label="计量单位组名称：" prop="measureUnit">
             <el-input v-model="formData.measureUnit"></el-input>
          </el-form-item>
          <el-form-item label="等级：" prop="level">
            <el-select v-model="formData.level" placeholder="请选择所属企业">
              <el-option :label="item.label" :value="item.value" v-for="item in levelInfo" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注：" prop="remark">
             <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInfo">保 存</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { apparatusConfig } from '@/const/crud/apparatus'
import {getDictType} from "@/api/common";
import {addInfo, fetchList, editInfo, delObj, drugEntList } from '@/api/apparatus'
export default {
  data() {
    return {
      type:'add',
      form:{},
      searchForm: {
        drugEntName: '',
      },
      tableData: [],
      tableLoading: false,
      tableOption: apparatusConfig,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      dialogVisible: false,
      formData: {
        name: '',
        manufacturer:'',
        drugEntId: '',
        drugEntName: '',
        specification:'',
        measureUnit: '',
        level:'',
        remark: ''
      },
      drugEntInfo: [],
      levelInfo: [],
      rules: {
        name: [ { required: true, message: '请输入器械名称', trigger: 'blur' }],
        manufacturer: [ { required: true, message: '请输入生产厂家', trigger: 'blur' }],
        drugEntId: [ { required: true, message: '请选择所属企业', trigger: 'change' }],
        specification: [ { required: true, message: '请输入规格', trigger: 'blur' }],
        measureUnit: [ { required: true, message: '请输入计量单位组名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDict()
    this.getList(this.page)
  },

  methods: {
    getDict() {
       drugEntList().then(res => this.drugEntInfo = res.data.data)
       getDictType({ type: 'medical_equipment_level' }).then(res => { this.levelInfo = res.data.data })
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.form.drugEntName = ''
      this.dialogVisible = false
    },
    async getList(page) {
      this.tableLoading = true
      const res = await fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, this.searchForm ))
        if(res.data.code === 0) {
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total
        }
        this.tableLoading = false
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize
      this.getList(this.page)
    },
    currentChange(current) {
      this.page.currentPage = current
      this.getList(this.page)
    },
    addClick() {
      this.dialogVisible = true
      this.type = "add"
    },
    searchChange(from, done) {
      this.searchForm = Object.assign(from, this.searchForm)
      console.log(this.searchForm);
      this.page.currentPage = 1
      this.getList(this.page)
      done()
    },
    resetChange() {
      this.searchForm = {
        drugEntName: ''
      }
      this.page.currentPage = 1
      this.getList(this.page)
    },
    editFn(row) {
      console.log('row', row);
      row.drugEntId = String(row.drugEntId)
      this.formData = row
      this.type = 'edit'
      this.dialogVisible = true

    },
    delFn(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await delObj(row.id)
        if(res.data.code === 0) {
          this.$message.success('删除成功')
          this.getList(this.page)
        }
      }).catch();
    },
    submitInfo() {
      this.$refs.form.validate(async valid => {
        if(valid){
        const item = this.drugEntInfo.find(item => item.deptId === this.formData.drugEntId)
        this.formData.drugEntName = item.name
          let res
          let message = ''
          if(this.type == 'add'){
            res = await addInfo(this.formData)
            message = '添加成功'
          }else {
            res = await editInfo(this.formData)
            message = '编辑成功'
          }
          if(res.data.code === 0) {
            this.$message.success(message)
          }
          this.getList(this.page)
          this.handleClose()
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.form{
  ::v-deep .el-select{
    width: 100%;
  }
}

</style>

