<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"
      >
        {{ dataForm.hospitalName || userInfo.hospitalName }}
        <!-- <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
        label="ICU床位数量"
        prop="icuBedCount"
      >
        <el-input
          placeholder="请输入ICU床位数量"
          v-model="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 { queryHospitalList, queryExtendConfig } from '@/api/common'
import { queryInfoById, addOrUpdateEmergencyRescue } from '@/api/rescue'
import { mapGetters } from 'vuex'
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
    },
    ...mapGetters([
      'userInfo'
    ])
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      formDisabled: false,
      selectLoading: false,
      dataForm: {
        hospitalCode: '',
        icuBedCount: '',
        icuBedFreeCount: '',
        cathLabCount: '',
        cathLabFreeCount: '',
        operateRoomCount: '',
        operateRoomFreeCount: ''
      },
      type: 'add',
      hospitalList: [],
      formItemList: [],
      rules: {
        // hospitalCode: [
        //   { required: true, message: '请选择医院' },
        // ],
        icuBedCount: [
          { required: true, message: '请输入ICU床位数量' },
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ],
        icuBedFreeCount: [
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ],
        cathLabCount: [
          { required: true, message: '请输入导管室数量' },
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ],
        cathLabFreeCount: [
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ],
        operateRoomCount: [
          { required: true, message: '请输入手术室数量' },
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ],
        operateRoomFreeCount: [
          { pattern: /^[0-9]*$/, message: '请输入正确数字' }
        ]
      }
    }
  },
  mounted() {
    // //获取可选择的医院
    // queryHospitalList().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 => {
          this.loading = false
          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.$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,
            hospitalCode: this.userInfo.hospitalCode,
            hospitalName: this.userInfo.hospitalName
          }
          // 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)
            }
          })
        }
      })
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryHospitalList(query).then(res => {
        this.hospitalList = res.respData
        this.selectLoading = false;
      })
    },
    closedHandler() {
      this.$refs.dataForm.resetFields()
    }
  }
}
</script>

<style>
</style>