<template>
  <el-dialog
    class="patientDialog"
    :visible.sync="visible"
    width="40%"
    :title="dataForm.title"
    @close="closeDialog"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule">
      <el-form-item label="登记号:" label-width="120px" prop="patientId" class="formItemOne">
        <el-input v-model="dataForm.patientId" placeholder="请输入登记号" />
      </el-form-item>
      <el-form-item label="患者姓名:" label-width="120px" prop="patientName">
        <el-input v-model="dataForm.patientName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="身份证号:" label-width="120px" prop="patientIdNumber">
        <el-input v-model="dataForm.patientIdNumber" placeholder="请输入身份证号 " />
      </el-form-item>
      <el-form-item label="出生日期:" label-width="120px" prop="patientBirthday">
        <el-date-picker
          v-model="dataForm.patientBirthday"
          align="right"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="性别:" label-width="120px" prop="gender">
        <el-radio-group v-model="dataForm.patientSex" size="medium">
          <el-radio-button label="男">男</el-radio-button>
          <el-radio-button label="女">女</el-radio-button>
          <el-radio-button label="保密">保密</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系电话:" label-width="120px" prop="patientPhone">
        <el-input v-model="dataForm.patientPhone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="家庭地址:" label-width="120px" prop="patientAddress">
        <el-input v-model="dataForm.patientAddress" placeholder="请输入地址" />
      </el-form-item>
      <div style="border-bottom: 1px solid #ccc;margin-bottom: 20px" />
      <el-form-item label="门诊医师:" prop="mzDoctorId" label-width="120px">
        <el-select v-model="dataForm.mzDoctorId" placeholder="请选择门诊医师">
          <el-option
            v-for="item in doctorList"
            :key="item.id"
            :label="item.realName"
            :value="item.employeeId
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item label="视光医师" prop="sgDoctorId" label-width="120px">
        <el-select v-model="dataForm.sgDoctorId" placeholder="请选择视光医师">
          <el-option
            v-for="item in doctorList"
            :key="item.id"
            :label="item.realName"
            :value="item.employeeId
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item label="眼别:" prop="zlEye" label-width="120px">
        <el-select v-model="dataForm.zlEye" placeholder="请选择眼别">
          <el-option v-for="item in zlEyeList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="医疗项目:" label-width="120px" prop="zlProjectCode">
        <el-select v-model="dataForm.zlProjectCode" placeholder="请选择医疗项目">
          <el-option
            v-for="item in projectList"
            :key="item.itemId"
            :label="item.itemName"
            :value="item.itemId
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注:" label-width="120px" prop="remarks">
        <el-input v-model="dataForm.remark" type="textarea" />
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { isMobile, isIDNumber } from '@/utils/validate'
export default {
  props: {
    patientTypeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        platform: 3,
        patientId: '',
        patientName: '',
        patientIdNumber: '',
        patientSex: '男',
        patientPhone: '',
        patientAddress: '',
        patientBirthday: '',
        mzDoctorId: '', // 门诊医师
        sgDoctorId: '', // 视光医师
        zlEye: '',
        zlProjectCode: '',
        visitId: '',
        remark: ''
      },
      zlEyeList: [
        { name: '左眼', value: 'os' },
        { name: '右眼', value: 'od' },
        { name: '双眼', value: 'ou' }
      ],
      params: {},
      visitList: [],
      doctorList: [],
      projectList: []
    }
  },
  computed: {
    dataRule() {
      var validataMobile = (rule, value, callback) => {
        if (value && !isMobile(value)) {
          return callback(new Error('您输入的手机号格式不正确'))
        }
        callback()
      }
      // var validataIDNumber = (rule, value, callback) => {
      //   if (value && !isIDNumber(value)) {
      //     return callback(new Error('您输入的身份证格式不正确'))
      //   } else if (!value) {
      //     return callback(new Error('请输入身份证号'))
      //   }
      //   callback()
      // }
      return {
        patientId: [
          { required: true, message: '请输入登记号', trigger: 'blur' }
        ],
        patientName: [
          { required: true, message: '请输入患者姓名', trigger: 'blur' }
        ],
        patientIdNumber: [
          { required: true, message: '请输入患者身份证号', trigger: 'blur' }
        ],
        sgDoctorId: [
          { required: true, message: '请选择视光医师', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.getDoctorList()
      this.getProject()
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields() // 重置表单
        // this.getVisitList() // 获取随访列表
        if (this.params.patientIdNumber && this.dataForm.title !== 'HIS引入') {
          this.dataForm = {
            title: this.dataForm.title,
            ...this.params
          }
        } else if (this.dataForm.title === 'HIS引入') {
          this.getHisInfo()
        }
      })
    },
    // 获取医师列表
    getDoctorList() {
      this.$http.get('/sys/user').then(data => {
        this.doctorList = data.data.data
        this.dataForm.sgDoctorId = JSON.parse(window.localStorage.getItem('qg-userData')).employeeId
      })
    },
    // 获取项目列表
    getProject() {
      this.$http.get('/patient/getZlItemDict').then(data => {
        this.projectList = data.data.data
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/patient/manage/${this.params.patientCentreId}/${this.params.patientIdNumber}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 获取His信息
    getHisInfo() {
      this.dataForm = { ...this.dataForm, ...this.params }
      this.dataForm.revisitCycles = this.dataForm.revisitCycles ? this.dataForm.revisitCycles : '1'
      this.dataForm.revisitCyclesUnit = this.dataForm.revisitCyclesUnit ? this.dataForm.revisitCyclesUnit : '3'
      console.log(this.dataForm)
    },
    // 获取随访列表
    async getVisitList() {
      const { data: res } = await this.$http.get('/visit/getList')
      if (res.code === 0) {
        this.visitList = res.data
      } else {
        this.$message.error(res.msg)
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/patient/savePatient', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { leading: true, trailing: false }),
    // 关闭弹框
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
.patientDialog {
  .cycle-display {
    .el-form-item__content {
      display: flex;
    }
    .el-input-number {
      width: 100px;
      margin-right: 16px;
    }
  }
}

</style>
<style lang="scss">
.patientDialog {
  .el-dialog__header {
    margin-bottom:12px
  }
    .el-dialog__body {
        padding-right: 30px;
    }
    .formItemOne .el-form-item__content {
      display: flex;
    }
    .el-date-editor.el-input, .el-date-editor.el-input__inner {
      width: 100%;
    }
}
</style>
