<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%"
        >
          <el-option
            v-for="item in hospitalList"
            :label="item.hospitalName"
            :key="item.id"
            :value="item.hospitalCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="ICU床位数量"
        prop="icuBedCount"
      >
        <el-input
          placeholder="请输入ICU床位数量"
          v-model.number="dataForm.icuBedCount"
        />
      </el-form-item>
      <el-form-item
        label="ICU空闲数量"
        prop="icuBedFreeCount"
      >
        <el-input
          placeholder="请输入ICU空闲数量"
          v-model.number="dataForm.icuBedFreeCount"
        />
      </el-form-item>

      <el-form-item
        label="导管室数量"
        prop="cathLabCount"
      >
        <el-input
          placeholder="请输入导管室数量"
          v-model.number="dataForm.cathLabCount"
        />
      </el-form-item>
      <el-form-item
        label="导管室空闲数量"
        prop="cathLabFreeCount"
      >
        <el-input
          placeholder="请输入导管室空闲数量"
          v-model.number="dataForm.cathLabFreeCount"
        />
      </el-form-item>

      <el-form-item
        label="手术室数量"
        prop="operateRoomCount"
      >
        <el-input
          placeholder="请输入手术室数量"
          v-model.number="dataForm.operateRoomCount"
        />
      </el-form-item>
      <el-form-item
        label="手术室空闲数量"
        prop="operateRoomFreeCount"
      >
        <el-input
          placeholder="请输入手术室空闲数量"
          v-model.number="dataForm.operateRoomFreeCount"
        />
      </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 { queryEmergencyHospitalList, queryExtendConfig } from '@/api/common'
import { queryInfoById, addOrUpdateEmergencyRescue } from '@/api/rescue'
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,
      dataForm: {
        hospitalCode: '',
        icuBedCount: '',
        icuBedFreeCount: '',
        cathLabCount: '',
        cathLabFreeCount: '',
        operateRoomCount: '',
        operateRoomFreeCount: ''
      },
      type: 'add',
      hospitalList: [],
      formItemList: [],
      rules: {
        hospitalCode: [
          { required: true, message: '请选择医院' },
        ],
        icuBedCount: [
          { required: true, message: '请输入ICU床位数量' },
          { type: 'number', message: '请输入数字' }
        ],
        icuBedFreeCount: [
          { type: 'number', message: '请输入数字' }
        ],
        cathLabCount: [
          { required: true, message: '请输入导管室数量' },
          { type: 'number', message: '请输入数字' }
        ],
        cathLabFreeCount: [
          { type: 'number', message: '请输入数字' }
        ],
        operateRoomCount: [
          { required: true, message: '请输入手术室数量' },
          { type: 'number', message: '请输入数字' }
        ],
        operateRoomFreeCount: [
          { type: 'number', message: '请输入数字' }
        ]
      }
    }
  },
  mounted() {
    //获取可选择的医院
    queryEmergencyHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    queryExtendConfig({ tableType: 4 }).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()
          addOrUpdateEmergencyRescue(params).then(res => {
            if (res.msg === 201) {
              this.dialogVisible = false
              this.$message.success('保存成功')
              this.$emit('subsucc')
            } else {
              this.$message.error(res.content)
            }
          })
        }
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>