<template>
  <div class="app-container">
  <div>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" @blur="parseBirthdayFromIdCard(form.idCard)" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-date-picker clearable size="small"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="性别" clearable size="small">
               <el-option
                 v-for="dict in sexOptions"
                 :key="dict.dictValue"
                 :label="dict.dictLabel"
                 :value="dict.dictValue"
               />
             </el-select>
        </el-form-item>
        <el-form-item label="体重KG" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重KG" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态" clearable size="small">
             <el-option
               v-for="dict in pOptions"
               :key="dict.dictValue"
               :label="dict.dictLabel"
               :value="dict.dictValue"
             />
          </el-select>
        </el-form-item>
        <el-form-item label="是否默认" prop="isDefault">
          <el-radio-group v-model="form.isDefault">
            <el-radio :label="item.dictValue" v-for="item in orOptions" >{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="与本人关系" prop="relation">
          <el-select  v-model="form.relation">
              <el-option v-for="(option, index) in relationOptions" :key="index" :value="option" clearable></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="肝功能是否异常" prop="liverUnusual">
          <el-radio-group v-model="form.liverUnusual">
            <el-radio :label="item" v-for="item in isGoodOptions" >{{item}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="肾功能是否异常" prop="renalUnusual">
          <el-radio-group v-model="form.renalUnusual" >
            <el-radio :label="item" v-for="item in isGoodOptions" >{{item}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="过敏史" prop="historyAllergic">
          <el-radio-group v-model="form.historyAllergic">
            <el-radio :label="item" v-for="item in isOptions" >{{item}}</el-radio>
          </el-radio-group>
          <el-checkbox-group v-model="historyAllergic" size="medium" v-if="form.historyAllergic=='有'">
           <el-checkbox   v-for="item in historyAllergicOptions" :key="item" :label="item"
                      >{{item}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="家族病史" prop="familyMedHistory">
          <el-radio-group v-model="form.familyMedHistory">
            <el-radio :label="item" v-for="item in isOptions" >{{item}}</el-radio>
          </el-radio-group>
          <el-checkbox-group v-model="familyMedHistory" size="medium" v-if="form.familyMedHistory=='有'">
           <el-checkbox   v-for="item in historyOptions" :key="item" :label="item"
                      >{{item}}</el-checkbox>
           </el-checkbox-group>
        </el-form-item>
        <el-form-item label="个人病史" prop="selfMedHistory" >
         <el-radio-group v-model="form.selfMedHistory">
            <el-radio :label="item" v-for="item in isOptions" >{{item}}</el-radio>
          </el-radio-group>
          <el-checkbox-group v-model="selfMedHistory" size="medium" v-if="form.selfMedHistory=='有'">
           <el-checkbox   v-for="item in historyOptions" :key="item" :label="item"
                      >{{item}}</el-checkbox>
           </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div  style="text-align:right;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
  </div>
</div>
</template>

<script>
import { listPatient, getPatient, delPatient, addPatient, updatePatient, exportPatient } from "@/api/store/patient";
export default {
  name: "addPatiet",
  data() {
    return {
      sexOptions: [],
      pOptions: [],
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      historyAllergic:[],
      familyMedHistory:[],
      selfMedHistory:[],
      historyAllergicOptions:['阿司匹林', '磺胺类', '头孢类', '青霉素类', '奶制品', '其他'],
      historyOptions:['糖尿病', '哮喘', '恶性肿瘤', '高血压', '其他'],
      isOptions:['有', '无'],
      isGoodOptions:['正常', '异常'],
      relationOptions: ['本人', '配偶', '父母', '子女', '朋友', '亲戚', '其他'],
      // 总条数
      total: 0,
      // 病人表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      form: {},
      // 表单校验
      rules: {
        patientName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        birthday: [
          { required: true, message: "生日不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        relation: [
          { required: true, message: '请选择与本人关系', trigger: 'change' }
        ],
        liverUnusual: [
          { required: true, message: '请选择肝功能是否异常', trigger: 'change' }
        ],
        renalUnusual: [
          { required: true, message: '请选择肾功能是否异常', trigger: 'change' }
        ],
        historyAllergic: [
          { required: true, message: '请选择过敏史', trigger: 'change' }
        ],
        familyMedHistory: [
          { required: true, message: '请选择家族病史', trigger: 'change' }
        ],
        selfMedHistory: [
          { required: true, message: '请选择个人病史', trigger: 'change' }
        ]
      }
    };
  },
  created() {
    this.getDicts("sys_company_or").then(response => {
      this.orOptions = response.data;
    });
    this.getDicts("sys_patient_status").then(response => {
        this.pOptions = response.data;
    });
    this.getDicts("sys_patient_sex").then(response => {
            this.sexOptions = response.data;
    });
  },
  methods: {

    init(userId){

      this.form.userId=userId;
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientId = row.patientId || this.ids
      getPatient(patientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病人";
       this.form.status = String(this.form.status);
       this.form.sex = String(this.form.sex)
       this.form.isDefault = String(this.form.isDefault)
       if(this.form.historyAllergic=="无"){
          this.historyAllergic=[];
       }else{
         this.historyAllergic=(this.form.historyAllergic).split(",")
         this.form.historyAllergic="有"
       }
       if(this.form.familyMedHistory=="无"){
         this.familyMedHistory=[];
       }else{
         this.familyMedHistory=(this.form.familyMedHistory).split(",")
         this.form.familyMedHistory="有"
       }
        console.log(this.form.selfMedHistory)
       if(this.form.selfMedHistory=="无"){
         this.selfMedHistory=[];
       }else{
         this.selfMedHistory=(this.form.selfMedHistory).split(",")
         this.form.selfMedHistory="有"
       }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.historyAllergic=="有"){
             if(this.historyAllergic==""){
               return this.$message("过敏史不能为空");
             }
          }
          if(this.form.familyMedHistory=="有"){
            if(this.familyMedHistory==""){
              return this.$message("家族病史不能为空");
            }
          }
          if(this.form.selfMedHistory=="有"){
            if(this.selfMedHistory==""){
              return this.$message("个人病史不能为空");
            }
          }
          if(this.form.historyAllergic=="有"){
             this.form.historyAllergic=(this.historyAllergic).toString()
          }
          if(this.form.familyMedHistory=="有"){
            this.form.familyMedHistory=(this.familyMedHistory).toString()
          }
          if(this.form.selfMedHistory=="有"){
            this.form.selfMedHistory=(this.selfMedHistory).toString()
          }
          addPatient(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.$emit("addUserPatient")
          });  

        }
      });
    },


  }
};
</script>
