<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="500px"
    @closed="closedHandler"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      size="small"
      label-width="150px"
      :rules="rules"
      :disabled="formDisabled"
      v-loading="loading"
    >
      <el-form-item
        label="医院名称"
        prop="hospitalCode"
      >
        <el-select
          v-model="dataForm.hospitalCode"
          placeholder="请选择医院"
          style="width: 100%"
          filterable
          remote
          ref="hospitalCode"
          :loading="selectLoading"
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="item in hospitalList"
            :label="item.hospitalName"
            :key="item.id"
            :value="item.hospitalCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="materialType"
        label="类型"
      >
        <el-radio-group v-model="dataForm.materialType">
          <el-radio :label="1">药品</el-radio>
          <el-radio :label="2">耗材</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="名称"
        prop="materialName"
      >
        <el-input
          placeholder="请输入名称"
          v-model="dataForm.materialName"
        />
      </el-form-item>
      <el-form-item
        label="库存数量"
        prop="storeCount"
      >
        <el-input
          placeholder="请输入库存数量"
          v-model.number="dataForm.storeCount"
        />
      </el-form-item>
      <el-form-item
        prop="storePlace"
        label="所在位置"
      >
        <el-radio-group v-model="dataForm.storePlace">
          <el-radio :label="1">急救车</el-radio>
          <el-radio :label="2">药管处</el-radio>
          <el-radio :label="3">医院急诊科</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <formItem
      labelWidth="150px"
      ref="formItem"
      :disabled="formDisabled"
      :formItemList="formItemList"
    />
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        type="primary"
        size="small"
        :disabled="formDisabled"
        @click="submitForm()"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryHospitalList, queryExtendConfig } from '@/api/common'
import { queryInfoById, addOrUpdateEmergencyMedical } from '@/api/medical'
export default {
  computed: {
    title: function() {
      let str = '武汉市医院急救药品耗材资源信息'
      if (this.type === 'add') {
        str += '填写'
      } else if (this.type === 'update') {
        str += '修改'
      } else if (this.type === 'detail') {
        str += '详情'
      }
      return str
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      formDisabled: false,
      type: 'add',
      dataForm: {
        hospitalCode: '',
        materialType: '',
        materialName: '',
        storeCount: '',
        storePlace: ''
      },
      hospitalList: [],
      selectLoading: false,
      formItemList: [],
      rules: {
        hospitalCode: [
          { required: true, message: '请选择医院' },
        ],
        materialType: [
          { required: true, message: '请选择类型' },
        ],
        materialName: [
          { required: true, message: '请输入名称' },
        ],
        storeCount: [
          { required: true, message: '请输入库存数量' },
          { type: 'number', message: '请输入数字' }
        ],
        storePlace: [
          { required: true, message: '请选择所在位置' },
        ]
      }
    }
  },
  mounted() {
    //获取可选择的医院
    queryHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    queryExtendConfig({ tableType: 3 }).then(res => {
      if (res.msg === 201 && res.respData && res.respData.length > 0) {
        this.formItemList = res.respData
      }
    })
  },
  methods: {
    init(type, id) {
      this.dialogVisible = true
      this.loading = true
      this.type = type
      this.formDisabled = type === 'detail'
      if (type === 'update' || type === 'detail') {
        if (!id) {
          this.$message.error('id不能为空')
          return
        }
        queryInfoById({ id: id }).then(res => {
          if (res.msg === 201) {
            this.dataForm = res.respData
            if (res.respData && res.respData.extendValueDtoList && res.respData.extendValueDtoList.length > 0) {
              this.$refs.formItem.init(res.respData.extendValueDtoList)
            } else {
              this.$refs.formItem.init([])
            }

            this.$nextTick(() => {
              this.loading = false
              this.$refs.dataForm.clearValidate()
            })
          } else {
            this.$message.error(res.content);
          }
        })
      } else {
        this.dataForm = {}
        this.$nextTick(() => {
          this.loading = false
          this.$refs.formItem.init([])
          this.$refs.dataForm.clearValidate()
        })
      }
    },
    submitForm() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          const params = {
            ...this.dataForm,
            auditStatus: 1
          }
          const hospital = this.hospitalList.find(i => i.hospitalCode === this.dataForm.hospitalCode)
          if (!hospital) {
            this.$message.error('无效数据');
            return
          }
          const { hospitalName, hospitalGrade, hospitalStep } = hospital
          params.hospitalName = hospitalName
          // params.hospitalGrade = hospitalGrade
          // params.hospitalStep = hospitalStep

          params.extendValueDtoList = this.$refs.formItem.getDataForm()
          addOrUpdateEmergencyMedical(params).then(res => {
            if (res.msg === 201) {
              this.dialogVisible = false
              this.$message.success('保存成功')
              this.$emit('subsucc')
            } else {
              this.$message.error(res.content)
            }
          })
        }
      })
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryHospitalList(query).then(res => {
        this.hospitalList = res.respData
        this.selectLoading = false;
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>