<template>
  <div >
    <el-row :gutter="10" class="mb8" style="float: right; top: -30px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:patient:add']"
        >新增</el-button>
      </el-col>
      </el-row>
    <el-table v-loading="loading" border :data="patientList" @selection-change="handleSelectionChange" style="top: -30px;">
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="所属会员" align="center" width="150px">
       <template slot-scope="scope">
         <div v-if="scope.row.nickName!=null"> {{scope.row.nickName}}-{{scope.row.phone}}</div>
       </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idCard" width="170px"/>
      <el-table-column label="出生年月" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
             <dict-tag :options="sexOptions" :value="scope.row.sex"/>
           </template>
      </el-table-column>
      <el-table-column label="体重G" align="center" prop="weight" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="状态" align="center" prop="status" >
         <template slot-scope="scope">
              <dict-tag :options="pOptions" :value="scope.row.status"/>
            </template>
      </el-table-column>
      <el-table-column label="与本人关系" align="center" prop="relation" />
      <el-table-column label="肝功能是否异常" align="center" prop="liverUnusual" />
      <el-table-column label="肾功能是否异常" align="center" prop="renalUnusual" />
      <el-table-column label="过敏史" align="center" prop="historyAllergic" />
      <el-table-column label="家族病史" align="center" prop="familyMedHistory" />
      <el-table-column label="个人病史" align="center" prop="selfMedHistory" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['his:patient:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:patient:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <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 slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPatient, getPatient, delPatient, addPatient, updatePatient, exportPatient } from "@/api/store/patient";
export default {
  name: "Patient",
  data() {
    return {
      addPatientInfo: process.env.VUE_APP_ADD_PATIENT,
      orOptions:[],
      show:{
          title:"患者详情",
          open:false,
      },       
      // 遮罩层
      userList:[],
      userName: {name:null},
      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: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        nickName:null,
        userId: null,
        idCard: null,
        birthday: null,
        sex: null,
        weight: null,
        mobile: null,
        isDel: null,
        status: null,
      },
      // 表单参数
      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: {

    /** 查询病人列表 */
    getList() {
      this.loading = true;
      listPatient(this.queryParams).then(response => {
        this.patientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPatList(id){
      this.loading = true;
      this.queryParams.userId=id;
      listPatient(this.queryParams).then(response => {
        this.patientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientName: null,
        userId: null,
        idCard: null,
        birthday: null,
        nickName:null,
        sex: null,
        weight: null,
        mobile: null,
        isDel: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.addPatientInfo||"添加病人";
    },
    /** 修改按钮操作 */
    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()
          }
          if (this.form.patientId != null) {
            updatePatient(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList(this.queryParams.userId);
            });
          } else {
            this.form.userId=this.queryParams.userId
            addPatient(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList(this.queryParams.userId);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patientIds = row.patientId || this.ids;
      this.$confirm('是否确认删除病人编号为"' + patientIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPatient(patientIds);
        }).then(() => {
          this.getList(this.queryParams.userId);
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },

  }
};
</script>
