<template>
  <div style="display: flex">
    
    <div :style="'width:' + (window.pageWidth - 200 - 120 - 80) + 'px;'">
      <el-table :data="dataList" border 
        width="100%" 
        :height="window.pageHeight < 600 ? 310:window.pageHeight-290" 
        row-key="id" size="small" highlight-current-row ref="dataList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40"/>
        <el-table-column prop="isMedicalStart" label="签到" width="50">
          <template slot-scope="scope">
            <el-checkbox :value="scope.row.isMedicalStart == 'Y'" />
          </template>
        </el-table-column>
        <el-table-column prop="medicalStartDate" label="签到日期" width="90">
          <template slot-scope="scope">
            <div>{{ lmoment(scope.row.medicalStartDate, "yyyy-MM-DD") }}</div>
          </template>
        </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="锁住" width="50">
          <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, "id", 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="100" />
        <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>

      </el-table>      
    </div> 
    <div style="margin-left: 10px;width:110px;">
        <div class="listBtn">
          <el-button type="success" class="btnClass" @click="sign">签到</el-button>
        </div>
        <div class="listBtn">
          <el-button type="primary" class="btnClass" @click="scanSign">扫码签到</el-button>
        </div>        
      </div>
    <el-dialog title="扫/输入条码签到" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item label="条码" label-width="100px">
          <el-input v-model="form.patientRegisterNo" @change="signByPatientRegisterNo"></el-input>
        </el-form-item>        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="signByPatientRegisterNo">确定</el-button>
      </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";

export default {
  components: {
    PatientRegisterEdit,
    Camera,
  },
  data() {
    return {
      dataList: [], //列表数据
      multipleSelection: [], //选中的数据列表
      dialogVisible: false, 
      form:{
        patientRegisterNo:''
      }   
    };
  },

  created() { },

  //挂载完成
  mounted() { },
  computed: {
    ...mapState(["window","dict", "patientRegister", "customerOrg"]),
  },
  methods: {
    ...mapActions(["getCustomerOrgGroup", "getPatientRegisterAbs"]),

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    signCore(body){
      postapi(`/api/app/patientregister/updatesigninmany`, body)
        .then((res) => {
          if (res.code != -1){
            this.$message.success("操作成功");             
          }                   
        })
        .catch(() => {
          this.$message({type: "error",message: "已取消删除",});
        });   
    },

    //选中人员签到 
    sign() {
      if (!this.multipleSelection || this.multipleSelection.length < 1 ) {
        alert("请选择体检人员，再进行此操作");
        return;
      }
      let body = []
      this.multipleSelection.forEach(e =>{
        body.push(e.id)
      });
      
      this.signCore(body)
        
    },

    scanSign(){
      this.dialogVisible = true
    },

    //按条码号查个人数据
    signByPatientRegisterNo() {
      let patientRegisterNo = this.form.patientRegisterNo
      let body = {
        sType:1,
        patientRegisterNo
      }      
      console.log(`/api/app/patientregister/getpatientregisterorpatient`,body)
      postapi('/api/app/patientregister/getpatientregisterorpatient',body)   
      .then((res) => {
        if (res.code != -1) {
          if(res.data){
            this.signCore([res.data.id])
            this.form.patientRegisterNo = ''
          }          
        }
      });
    },

    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.startDate && this.patientRegister.query.endDate) {
        body.startDate = moment(this.patientRegister.query.startDate).format("yyyy-MM-DD")
        body.endDate = moment(this.patientRegister.query.endDate).format("yyyy-MM-DD")
        if(body.startDate > body.endDate){
          this.$message.warning("起始日期不能大于截止日期，数据校验不通过！")
          return
        }
      }


      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)
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      postapi('/api/app/patientregister/getlistinfilter', body)
        .then((res) => {
          if(res.code != - 1){
            this.dataList = res.data.items;
          }
          loading.close();
        })
        .catch((err) => {
          loading.close();
        });
    },
  },

  //监听事件
  watch: {
    //触发查询事件
    "patientRegister.query.times"(newVal, oldVal) {
      if (newVal != oldVal) {
        //alert('触发查询事件')
        this.query();
      }
    },
   
  },
};
</script>
<style scoped>
.box {
  display: flex;
}

.listBtn {
  margin-top: 10px;
}
.btnClass{
  width:110px;
  text-align: center;
}
.btnClass{
  width:110px;
}
</style>
