<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>
<!--      <el-form-item label="随访方案:" label-width="90px" prop="visitId">-->
<!--        <el-select v-model="dataForm.visitId" placeholder="请选择随访方案">-->
<!--          <el-option v-for="item in visitList" :key="item.id" :label="item.name" :value="item.id" />-->
<!--        </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: 1,
        patientId: '',
        patientName: '',
        patientIdNumber: '',
        patientSex: '男',
        patientPhone: '',
        patientAddress: '',
        patientBirthday: '',
        visitId: '',
        remark: ''
      },
      params: {},
      visitList: []
    }
  },
  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' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields() // 重置表单
        // this.getVisitList() // 获取随访列表
        if (this.params.patientIdNumber && this.dataForm.title !== 'HIS引入') {
          this.getInfo()
        } else if (this.dataForm.title === 'HIS引入') {
          this.getHisInfo()
        }
      })
    },
    // 获取信息
    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>
