<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="专家姓名"
        prop="expertName"
      >
        <el-input
          placeholder="请输入专家姓名"
          v-model="dataForm.expertName"
        />
      </el-form-item>
      <!-- <el-form-item
        label="是否值班"
        prop="isDuty"
      >
        <el-radio-group v-model="dataForm.isDuty">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item
        label="擅长专业"
        prop="specialty"
      >
        <el-input
          placeholder="请输入擅长专业"
          :maxlength='100'
          v-model="dataForm.specialty"
        />
      </el-form-item>
      <el-form-item
        label="性别"
        prop="expertSex"
      >
        <el-radio-group v-model="dataForm.expertSex">
          <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-item
        label="职称"
        prop="expertTitle"
      >
        <el-input
          placeholder="请输入职称"
          :maxlength='100'
          v-model="dataForm.expertTitle"
        />
      </el-form-item>
      <el-form-item
        label="科室"
        prop="emergencyName"
      >
        <el-input
          placeholder="请输入科室"
          :maxlength='100'
          v-model="dataForm.emergencyName"
        />
      </el-form-item>
      <el-form-item
        label="附件上传："
        prop="fileList"
      >
        <fileUpload :setFileList="fileList" ref="fileUpload" />
      </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 { queryExtendConfig, queryHospitalList } from '@/api/common'
import { queryInfoById, addOrUpdateEmergencyExpert } from '@/api/expert'
import { mapGetters } from 'vuex'
import fileUpload from '../../../components/fileUpload.vue'
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'
    ])
  },
  components: {
    fileUpload
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      formDisabled: false,
      selectLoading: false,
      fileList: [],
      hospitalList: [],
      type: 'add',
      dataForm: {
        hospital: '',
        expertName: '',
        isDuty: '',
        expertSex: '',
        expertTitle: '',
        emergencyName: ''
      },
      formItemList: [],
      rules: {
        // hospitalCode: [
        //   { required: true, message: '请选择医院' },
        // ],
        expertName: [
          { required: true, message: '请输入专家名称' },
        ],
        emergencyName: [
          { required: true, message: '请输入科室' },
        ]
      }
    }
  },
  mounted() {
    //获取可选择的医院
    // queryHospitalList().then(res => {
    //   this.hospitalList = res.respData
    // })
    queryExtendConfig({ tableType: 2 }).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 || {}
            this.fileList = res.respData.fileList || []
            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,
            fileList: this.$refs.fileUpload.getFileList(),
            // hospitalName: this.$refs.hospitalCode.query
            hospitalCode: this.userInfo.hospitalCode,
            hospitalName: this.userInfo.hospitalName
          }
          params.extendValueDtoList = this.$refs.formItem.getDataForm()
          addOrUpdateEmergencyExpert(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()
      this.fileList = []
    }
  }
}
</script>

<style>
</style>