<template>
  <div style="display: flex;">
    <div :style="'width:' + (window.pageWidth - 200 - 110 - 25) + 'px;'">
      <el-table :data="customerOrg.customerOrgRegisterList" border
        :height="window.pageHeight < 600 ? 155 : window.pageHeight - 445" size="small" highlight-current-row
        @row-click="rowClick" ref="customerOrg.customerOrgRegisterList">
        <el-table-column prop="id" label="编号" width="300" align="center" />
        <el-table-column prop="isQuestion" width="70" align="center">          
          <template slot="header" slot-scope="scope">
            <el-tooltip content="控制体检单位【必须填写问卷调查，方可进行健康评估】" placement="top-start"><div>问卷调查</div></el-tooltip>
          </template>        
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.isQuestion" true-label="Y" false-label="N" @change="clickQuestion(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column prop="medicalTimes" label="体检次数" width="70" align="center" />
        <el-table-column prop="beginTime" label="开始日期" min-width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.beginTime.substring(0,10) }}
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="结束日期" min-width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.endTime.substring(0,10) }}
          </template>
        </el-table-column>
        <el-table-column prop="isComplete" label="完成标志" width="70" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.isComplete === "Y" ? "是" : "否" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="创建者" min-width="70" align="center" />
        <el-table-column prop="creationTime" label="创建时间" min-width="120" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.creationTime">
              {{ moment(scope.row.creationTime).format('yyyy-MM-DD HH:mm:ss') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="lastModifierName" label="修改者" min-width="70" align="center" />
        <el-table-column prop="lastModificationTime" label="修改时间" min-width="120" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.lastModificationTime">
              {{ moment(scope.row.lastModificationTime).format('yyyy-MM-DD HH:mm:ss') }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-left: 10px; margin-top: 20px">
      <div v-show="checkPagePriv(pagePriv.privs, '新增次数')" style="margin-top: 10px">
        <el-button type="primary" @click="add" class="commonbutton">新增次数</el-button>
      </div>
      <div v-show="checkPagePriv(pagePriv.privs, '体检完成')" style="margin-top: 10px">
        <el-button type="success" @click="edit" class="commonbutton">体检完成</el-button>
      </div>
      <div v-show="checkPagePriv(pagePriv.privs, '取消完成')" style="margin-top: 10px">
        <el-button type="warning" @click="cansel" class="commonbutton">取消完成</el-button>
      </div>
      <div v-show="checkPagePriv(pagePriv.privs, '删除次数')" style="margin-top: 10px">
        <el-button type="danger" @click="btnDel" class="deleteButton">删除次数</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import { mapState, mapMutations } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { getPagePriv, checkPagePriv, tcdate, objCopy, deepCopy, arrayExistObj } from "../../utlis/proFunc";

export default {
  components: {},
  data() {
    return {
      pagePriv: {
        routeUrlorPageName: 'customerOrg', //当前页面归属路由或归属页面权限名称
        privs: [] // 页面权限
      },
      customerOrgRegisterId: "", //体检次数ID
    };
  },

  created() {
    //获取用户当前页面的权限
    let userPriv = window.sessionStorage.getItem('userPriv')
    if (userPriv) this.pagePriv.privs = deepCopy(getPagePriv(this.pagePriv.routeUrlorPageName))
  },

  //挂载完成
  mounted() {
    this.getCustomerOrgRegisterList(this.dataTransOpts.tableS.customer_org_register.pid);
  },

  computed: {
    ...mapState(["customerOrg", "window", "dataTransOpts"]),
  },

  methods: {
    moment, checkPagePriv,
    //点击体检次数行
    rowClick(row) {
      this.customerOrgRegisterId = row.id;
    },

    //获取体检次数列表
    getCustomerOrgRegisterList(customerOrgPid) {
      if (customerOrgPid) {
        getapi(
          `/api/app/customerorgregister/getlistincustomerorgid?CustomerOrgId=${customerOrgPid}`
        ).then((res) => {
          if (res.code != -1) {
            this.customerOrg.customerOrgRegisterList = res.data;
          }
        });
      } else {
        this.customerOrg.customerOrgRegisterList = []
      }
    },

    // 某些体检单位，需控制【必须填写问卷调查方查进行健康评估】
    clickQuestion(row){
      //console.log('row',row)
      postapi('/api/app/CustomerOrgRegister/UpdateIsQuestion',{
        customerOrgRegisterId: row.id,
        isQuestion: row.isQuestion
      }).then(res => {
        if(res.code > -1) this.$message.success({ showClose: true, message: "操作成功！"})
      })
    },

    //设置体检次数状态
    setOrgRegisterState(IsComplete) {
      if (!this.dataTransOpts.tableS.customer_org.id  || !this.customerOrgRegisterId) {
        console.log(this.dataTransOpts.tableS.customer_org.id , this.customerOrgRegisterId);
        this.$message.warning({ showClose: true, message: "请选中要操作的体检次数"});
        return;
      }
      //console.log(`/api/app/customer-org-register/${this.customerOrgRegisterId}/state`)
      putapi(
        `/api/app/customer-org-register/${this.customerOrgRegisterId}/state?IsComplete=${IsComplete}`
      ).then((res) => {
        console.log("设置体检次数状态", res.data);
        this.getCustomerOrgRegisterList(this.dataTransOpts.tableS.customer_org.id );
        console.log("操作成功！");
      });
    },

    //体检次数 相关操作
    async add() {
      //console.log("增加次数 addTimes");
      if (!this.dataTransOpts.tableS.customer_org.id ) {
        this.$message.warning({ showClose: true, message: "单位信息未保存！"});
        return;
      }

      try {
        let result = await getapi(`/api/app/customer-org/parent/${this.dataTransOpts.tableS.customer_org.id }`)
        postapi(`/api/customerorgregister/createcustomerorgregister?CustomerOrgId=${result.data}`).then((res) => {
          if (res.code != -1) {
            this.getCustomerOrgRegisterList(result.data);
            console.log("操作成功！");
          }
        })
      } catch (error) {
        this.$message.error({ showClose: true, message: "操作失败！" + err});
      }
    },

    edit() {
      //console.log("体检完成 editCustomerOrgRegister");
      this.setOrgRegisterState("Y");
    },

    cansel() {
      //console.log("体检完成 editCustomerOrgRegister");
      this.setOrgRegisterState("N");
    },

    btnDel() {
      if (!this.dataTransOpts.tableS.customer_org.id  || !this.customerOrgRegisterId) {
        //console.log(this.dataTransOpts.tableS.customer_org.id , this.customerOrgRegisterId);
        this.$message.warning({ showClose: true, message: '请选中要操作的体检次数'})
        return;
      }

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning",
      }).then(() => {
        //console.log('{patientRegisterIds}',{patientRegisterIds})
        return deletapi(`/api/app/customer-org-register/${this.customerOrgRegisterId}`);
      }).then((res) => {
        if (res.code != -1) {
          console.log("删除 操作成功");
          let lfind = arrayExistObj(this.customerOrg.customerOrgRegisterList, 'id', this.customerOrgRegisterId)
          if (lfind > - 1) this.customerOrg.customerOrgRegisterList.splice(lfind, 1)
          this.customerOrgRegisterId = ''
        }
      }).catch((err) => {
        //
      });
    }

  },
  //监听事件
  watch: {
    // 体检次数父ID未切换换时 也可以强制刷新数据
    "dataTransOpts.refresh.customer_org_register.M": {
      // immediate: true,
      handler(newVal, oldVal) {
        console.log(`watch 体检次数 newVal:${newVal} oldVal:${oldVal} customerOrgPid: ${this.dataTransOpts.tableS.customer_org.parent_id}`);
        this.getCustomerOrgRegisterList(this.dataTransOpts.tableS.customer_org.parent_id);
      }
    },
  },
};
</script>
<style scoped>
.box {
  display: flex;
}

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