<template>
  <div style="display: flex">
    <div :style="'width:' + (window.pageWidth - 200 - 120 - 70) + 'px;'">
      <el-table :data="patientRegister.prList" border :height="patientRegister.prListHeight" highlight-current-row
        @row-click="rowick" size="small" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40">

        </el-table-column>
        <el-table-column prop="completeFlag" label="体检进度">
          <template slot-scope="scope">
            <div>{{ ldddw(dict.completeFlag, "id", scope.row.completeFlag, "displayName") }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="isLock" label="锁住">
          <template slot-scope="scope">
            <div>{{ scope.row.isLock == "Y" ? "是" : "否" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="customerOrgParentName" label="单位" width="180">
          <template slot-scope="scope">
            <div>{{ scope.row.customerOrgParentName ? scope.row.customerOrgParentName : scope.row.customerOrgName }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="customerOrgName" label="部门" width="180">
          <template slot-scope="scope">
            <div>{{ scope.row.customerOrgParentName ? scope.row.customerOrgName : "" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="patientName" label="姓名" />
        <el-table-column prop="sexId" label="性别">
          <template slot-scope="scope">
            <div>{{ ldddw(dict.sex, "id", scope.row.sexId, "displayName") }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" />
        <el-table-column prop="patientRegisterNo" label="条码号" width="150" />
        <el-table-column prop="patientNo" label="档案号" />
        <el-table-column prop="medicalTimes" label="体检次数"></el-table-column>
        <el-table-column label="分组/套餐" width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.medicalPackageId !== dict.personOrgId && scope.row.customerOrgId === dict.personOrgId">
              {{ ldddw(dict.medicalPackage, "id", scope.row.medicalPackageId, "displayName") }}
            </div>
            <div v-if="scope.row.medicalPackageId !== dict.personOrgId && scope.row.customerOrgId !== dict.personOrgId">
              {{ ldddw(dict.customerOrgGroupAll, "id", scope.row.customerOrgGroupId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nationId" label="民族">
          <template slot-scope="scope">
            <div>
              {{ ldddw(dict.nation, "nationId", scope.row.nationId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="idNo" label="身份证" width="150" />
        <el-table-column prop="birthDate" label="出生日期" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.birthDate">
              {{ lmoment(scope.row.birthDate, "yyyy-MM-DD") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="mobileTelephone" label="手机" width="100" />
        <el-table-column prop="telephone" label="电话" width="100" />
        <el-table-column prop="address" label="地址" width="300" />
        <el-table-column prop="medicalCardNo" label="体检卡号" />
        <el-table-column prop="jobCardNo" label="工卡号" />
        <el-table-column prop="maritalStatusId" label="婚姻状况">
          <template slot-scope="scope">
            <div>
              {{ ldddw(dict.maritalStatus, "id", scope.row.maritalStatusId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="medicalTypeId" label="体检类别">
          <template slot-scope="scope">
            <div v-if="scope.row.medicalTypeId !== dict.personOrgId">
              {{ ldddw(dict.medicalType, "id", scope.row.medicalTypeId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="personnelTypeId" label="人员类别">
          <template slot-scope="scope">
            <div v-if="scope.row.personnelTypeId !== dict.personOrgId">
              {{ ldddw(dict.personnelType, "id", scope.row.personnelTypeId, "displayName") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="jobPost" label="职务" />
        <el-table-column prop="jobTitle" label="职称" />
        <el-table-column prop="salesman" label="介绍人" />
        <el-table-column prop="isVip" label="是否VIP">
          <template slot-scope="scope">
            <div>{{ scope.row.isVip == "Y" ? "是" : "否" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="登记人" />
        <el-table-column prop="creationTime" label="登记日期" width="100">
          <template slot-scope="scope">
            <div>{{ lmoment(scope.row.creationTime, "yyyy-MM-DD") }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="isUpload" label="是否上传">
          <template slot-scope="scope">
            <div>{{ scope.row.isUpload == "Y" ? "是" : "否" }}</div>
          </template>
        </el-table-column>

        <!--
        "sexHormoneTermId": "00000000-0000-0000-0000-000000000000",
        "interposeMeasure": null,
        "medicalConclusionId": "00000000-0000-0000-0000-000000000000",
        "reportPrintTimes": 0,        
        "isMedicalStart": "N",
        "medicalStartDate": "6/28/2023",
        "isRecoverGuide": "N",
        "summaryDate": "",
        "summaryDoctor": null,
        "isAudit": "N",
        "auditDoctor": null,
        "auditDate": "",        
        "isNameHide": "N",
        "isPhoneFollow": "N",
        "thirdInfo": null,
        "guidePrintTimes": null,
        "remark": null,
        "organizationUnitId": "00000000-0000-0000-0000-000000000000",
        "customerOrgRegisterId": "00000000-0000-0000-0000-000000000000",
        "lastModifierName": "",
        "lastModificationTime": null,
        "lastModifierId": null,
        "creatorId": null,
        "id": "3a0c196d-a6d0-37fe-5c32-4806bdc4530f"
        -->
      </el-table>
      <!-- 给合项目  基本信息  -->
      <el-tabs v-model="tabChoosed">
        <!-- 给合项目 -->
        <el-tab-pane label="已选组合项目" name="1">
          <PatientRegisterAsbItem />
        </el-tab-pane>
      </el-tabs>
    </div>
    <div style="margin-left: 10px;width:110px;">
      <div class="listBtn">
        <el-button type="success" class="btnClass" @click="add">人员登记</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass" @click="edit">编辑</el-button>
      </div>
      <div class="listBtn">
        <el-button type="danger" class="btnClass" @click="del">删除</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass" @click="openCamera" icon="el-icon-camera">拍照</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass">健康档案</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass">调整分组</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass">调整项目</el-button>
      </div>
      <div class="listBtn">
        <el-button type="primary" class="btnClass" @click="guidePrint">打指引单</el-button>
      </div>
    </div>

    <!-- 体检人员登记 -->
    <el-dialog :title="patientRegister.patientRegisterId ? '体检人员--编辑' : '体检人员--新增'" :visible.sync="dialogVisible"
      :close-on-click-modal="false" fullscreen>
      <PatientRegisterEdit :formInitData="formInitData" :editTimes="editTimes" />
    </el-dialog>

    <!-- 拍照 -->
    <el-dialog title="拍照" :visible.sync="patientRegister.cameraVisble" width="400" height="800" :show-close="false"
      :append-to-body="true" :close-on-click-modal="false">
      <Camera :id="patientRegister.patientRegisterId" />
    </el-dialog>

    <!-- 指引单 -->
    <el-dialog title="指引单" :visible.sync="dialogGuide" width="400" height="800" 
      :append-to-body="true" :close-on-click-modal="false">
      <div>{{ guideMsg }}</div>
    </el-dialog>
  </div>
</template>
<script>

import moment from "moment";
import { mapState, mapActions } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { dddw, objCopy, arrayReduce } from "@/utlis/proFunc";

import PatientRegisterEdit from "../../components/patientRegister/PatientRegisterEdit.vue";
import Camera from "../../components/patientRegister/Camera.vue";
import PatientRegisterAsbItem from "../../components/patientRegister/patientRegisterAsbItem.vue";

export default {
  components: {
    PatientRegisterEdit,
    Camera,
    PatientRegisterAsbItem,
  },
  data() {
    return {
      multipleSelection: [], //选中的数据列表
      dialogVisible: false,
      dialogCamera: false,
      dialogGuide:false,
      guideMsg:'guideMsg',
      tabChoosed: "1",
      formInitData: {},      //体检登记初始表单数据
      editTimes: 0,
    };
  },

  created() { },

  //挂载完成
  mounted() { },
  computed: {
    ...mapState(["window", "dict", "patientRegister", "customerOrg"]),
  },
  methods: {
    ...mapActions(["getCustomerOrgGroup", "getPatientRegisterAbs"]),     
  
    // 打印指引单(isPreview)
    async guidePrint(isPreview){
      let token = localStorage.getItem('token');
      let user = localStorage.getItem('user');
      let toOutShell = '';
      let lfind = -1;
 

      if(this.multipleSelection.length < 1){
        this.$message.info("请勾选要打印指引单的人员记录！");
        return;
      }

      this.multipleSelection.forEach((item,index) =>{
        getapi(`/api/app/printreport/getpatientregisterguidereport?PatientRegisterId=${item.id}`)
        .then((res) => {        
          if (res.code != -1) {
            res.data;
            return this.$electronAPI.printPre('web 传入参数');
          }
        })
        .then(res => {
          if(res.toLowerCase() == 'success'){
            //更新打印次数
            return postapi('api/app/patientregister/updatepatientregisterguideprinttimesmany',[item.id])
          }
        })
        .then(res =>{
          if (res.code != -1) {
            //
          }
        });
      });


      

      console.log(msg);

      
      

        
         
    },
    
    handleSelectionChange(val) {
      this.multipleSelection = val;
      //console.log('this.multipleSelection',this.multipleSelection)
    },

    //获取单位分组 /api/app/customer-org-group/in-customer-org-id/3a0c0444-d7a0-871f-4074-19faf1655caf
    getCustomerOrgGroup(customerOrgld) {
      getapi(`/api/app/customer-org-group/in-customer-org-id/${customerOrgld}`)
        .then((res) => {
          console.log("getCustomerOrgGroup", res.data);
          if (res.code == 1) {
            this.patientRegister.customerOrgGroup = res.data;
          }
        });
    },

    //点击体检次数行
    rowick(row) {
      this.patientRegister.photo = ""; //清除照片缓存

      this.patientRegister.patientRegisterId = row.id;
      this.patientRegister.patientRegisterRd = row;
      this.patientRegister.query.customerOrgParentId = row.customerOrgParentId;
      this.patientRegister.customerOrgGroupChange = 0;   //控制体检列表记录切换时，0 无需触发更换分组操作 
      this.patientRegister.medicalPackageChange = 0;     //控制体检列表记录切换时，0 无需触发更换套餐操作
      this.getCustomerOrgGroup(row.customerOrgParentId);
      this.dict.asbItem = [...this.dict.asbItemAll]
      this.getPatientRegisterAbs(row.id);
    },

    //体检次数 相关操作
    add() {
      let customerOrgId = this.patientRegister.query.customerOrgId;
      if (!customerOrgId) {
        alert("请选择单位或个人");
        return;
      }
      //console.log('customerOrgId',customerOrgId)
      // this.patientRegister.patientRegisterId = "";
      // this.patientRegister.patientRegisterRd.photo = '';
      // this.patientRegister.patientRegisterRdInit.id = "";
      // this.patientRegister.patientRegisterRdInit.customerOrgId = this.patientRegister.query.customerOrgId;
      // this.patientRegister.addTimes++;
      // this.patientRegister.patientRegisterTimes++;
      this.patientRegister.patientRegisterRdInit.customerOrgId = this.patientRegister.query.customerOrgId;
      this.formInitData = { ...this.patientRegister.patientRegisterRdInit };
      this.patientRegister.patientRegisterAbs = [];
      this.dialogVisible = true;
      this.editTimes++; //触发表单窗口，数据更新
      this.getPatientRegisterAbs();
    },

    edit() {
      if (!this.patientRegister.patientRegisterRd.id) {
        alert("请选择要操作的记录");
        return;
      }
      //this.patientRegister.patientRegisterTimes++; 
      this.getPatientRegisterAbs(this.patientRegister.patientRegisterRd.id);
      this.formInitData = { ...this.patientRegister.patientRegisterRd };
      this.dialogVisible = true;
      this.editTimes++; //触发表单窗口，数据更新
    },

    //拍照
    openCamera() {
      if (!this.patientRegister.patientRegisterId) {
        alert("请选择要操作的记录");
        return;
      }
      this.patientRegister.cameraVisble = true;
    },

    //删除（可批量删除）
    // /api/app/patient-register/many?PatientRegisterIds=3a0c2cac-f44c-f407-9504-c1fc5e80a159&PatientRegisterIds=3a0c2cb3-d10c-ed70-db6a-b835e75ce641
    del() {
      if (this.multipleSelection.length < 1) {
        alert("请先勾选要操作的记录");
        return;
      }
      let patientRegisterIds = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        patientRegisterIds.push(this.multipleSelection[i]["id"]);
      }

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //console.log('{patientRegisterIds}',{patientRegisterIds})
          return postapi(`/api/app/patient-register/delete-many`, {
            patientRegisterIds,
          });
        })
        .then((res) => {
          this.$message.success("删除成功");
          this.patientRegister.query.times++;
          this.patientRegister.patientRegisterId = "";
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //单个删除方式
    delBak() {
      if (!this.patientRegister.patientRegisterId) {
        alert("请选择要操作的记录");
        return;
      }

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //console.log('${this.patientRegister.patientRegisterId}',this.patientRegister.patientRegisterId)
          return deletapi(`/api/app/patient-register/${this.patientRegister.patientRegisterId}`);
        })
        .then((res) => {
          this.$message.success("删除成功");
          this.patientRegister.query.times++;
          this.patientRegister.patientRegisterId = "";
        })
        .catch(() => {
          this.$message({ type: "info", message: "已取消删除", });
        });
    },

    ldddw(arrayData, key, value, display) {
      return dddw(arrayData, key, value, display);
    },

    lmoment(date, forMat) {
      return moment(new Date(date)).format(forMat);
    },

    //查询
    query() {
      let body = {}

      console.log(`this.patientRegister.query`, this.patientRegister.query)
      if (this.patientRegister.query.customerOrgFlag) {
        if (this.patientRegister.query.CustomerOrgParentId) {
          body.customerOrgId = this.patientRegister.query.CustomerOrgParentId
        } else {
          if (this.patientRegister.query.customerOrgId) body.customerOrgId = this.patientRegister.query.customerOrgId
        }
      }

      if (this.patientRegister.query.sex) body.sexId = this.patientRegister.query.sex

      if (this.patientRegister.query.patientName) body.patientName = this.patientRegister.query.patientName

      if (this.patientRegister.query.completeFlag) body.completeFlag = this.patientRegister.query.completeFlag

      //StartDate EndDate
      if (this.patientRegister.query.dateRange) {
        body.startDate = moment(new Date(this.patientRegister.query.dateRange[0])).format("yyyy-MM-DD")
        body.endDate = moment(new Date(this.patientRegister.query.dateRange[1])).format("yyyy-MM-DD")
      }


      if (this.patientRegister.query.idCardNo) body = { idNo: this.patientRegister.query.idCardNo }

      if (this.patientRegister.query.patientNo) body = { patientNo: this.patientRegister.query.patientNo }

      if (this.patientRegister.query.patientRegisterNo) body = { patientRegisterNo: this.patientRegister.query.patientRegisterNo }


      console.log('/api/app/patientregister/getlistinfilter', body)
      postapi('/api/app/patientregister/getlistinfilter', body)
        .then((res) => {
          this.patientRegister.prList = res.data;
        });
    },

  },

  //监听事件
  watch: {
    //触发查询事件
    "patientRegister.query.times"(newVal, oldVal) {
      if (newVal != oldVal) {
        //alert('触发查询事件')
        this.query();
      }
    },

    //新增后，触发赋值
    "patientRegister.patientRegisterRd.id"(newVal, oldVal) {
      if (newVal != oldVal) {
        //console.log('patientRegister.patientRegisterRd.customerOrgId',this.patientRegister.patientRegisterRd.customerOrgId)
        objCopy(this.patientRegister.patientRegisterRd, this.form);
      }
    },
  },
};
</script>
<style scoped>
.box {
  display: flex;
}

.listBtn {
  margin-top: 5px;
  text-align: center;
}

.btnClass {
  width: 110px;
}
</style>
