<template>
  <div>
      <div class="btnBox">
        <!-- 安全员、科长 -->
            <div v-if="['AQY','KZ'].includes(props.initPage.role)&&['09','21'].includes(listQuery.stage)">
             <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="确定要提交审核?"
                    @confirm="handlehz" >
                    <template #reference>
                        <el-button type="primary" >
              <span>提交审核</span>
            </el-button>
                    </template>
                  </el-popconfirm>
          
            <el-button type="primary" @click="ChooesDdld" class='mr10'>
              <span>设置评价小组</span>
            </el-button>
          </div>
        <el-button type="primary" @click="Close">
          <el-icon>
            <ArrowLeft />
          </el-icon>
          <span>返回</span>
        </el-button>
      </div>
   

    <!-- 表格 -->
    <div class="box-a">
      <div>
        <div style="display: flex;justify-content: space-between;">
          <div style="width: 40%;height:calc(100vh - 200px);">
            <MOSTY.Table :table-columns="tableColumn" :tableData="tableData1" :tableloading="tableloading"
              :isSelect="false" :isSort="false" style="width: 100%;" :actionsW='120'>
              <template #num="{ row }">
                <span>{{ row.orangePointCount + row.redPointCount + row.yellowPointCount
                  + row.bluePointCount }}</span>
              </template>
              <template #stage="{ row }">
                <dict-tag :options="GZ_D_ENT_STAGE" :value="row.stage" :tag="false" />
              </template>
              <template #actions="{ row }">
                <span class="operesee" v-if="row.stage=='21'||(row.orangePointCount + row.redPointCount + row.yellowPointCount
                  + row.bluePointCount)>0" @click="handlexg(row)">查看</span>
                  <span v-if='row.reason'  class="operesee" @click="handleyy(row)" >原因</span>
                <span v-if="row.stage!='21'&&(row.orangePointCount + row.redPointCount + row.yellowPointCount
                  + row.bluePointCount)==0&&!row.reason">--</span>
              </template>
            </MOSTY.Table>
          </div>
          <div style="flex: 1;margin-left: 10px;margin-right:10px">
            <div style="height: calc(100vh - 249px)">
  <el-table ref="mulRefs" stripe :data="tableData2" style="width: 100%;height:100%;"
              @selection-change="handleSelectionChange" :height="tableHeight" border>
              <el-table-column label="风险单元" property="objname">
              </el-table-column>
              <el-table-column label="风险描述" property="dangerDescription" show-overflow-tooltip>
              </el-table-column>
              <el-table-column label="风险等级" property="risklevel" width='150'>
                <template #default="{ row }">
                  <span v-if="row.risklevel == '4'" class="fxdjspan reds">重大风险</span>
                  <span v-else-if="row.risklevel == '3'" class="fxdjspan orgs">较大风险</span>
                  <span v-else-if="row.risklevel == '2'" class="fxdjspan blus">一般风险</span>
                  <span v-else-if="row.risklevel == '1'" class="fxdjspan gres">低风险</span>
                  <span v-else>待定</span>
                </template>
              </el-table-column>
              <el-table-column property="isPj" label="状态"  width='100'>
                <template #default="{ row }">
                  <!-- 状态 01.评价中 02.申验中 03.完成 -->
                  <span v-if="row.status == '01'" >评价中</span>
                  <span v-if="row.status == '02'">审验中</span>
                      <span v-if="row.status == '03'">完成</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width='250'>
                <template #default="{ row }">
                        <span class="operesee" @click="handlepj(row,'pj')" v-if="row.status != '03'&&['21'].includes(zbbmzt)"><el-icon>
                      <Compass />
                    </el-icon>评价</span>
                   <span class="operedit" @click="handlepj(row,'sy')" v-if="props.initPage.role=='KZ'&&row.status == '02'"><el-icon>
                <EditPen />
              </el-icon>审验</span>
                  <el-popconfirm confirm-button-text="是" cancel-button-text="否" icon-color="red" title="确定要删除?"
                    @confirm="deldetebm(row)" v-if="row.status != '03'&&['21'].includes(zbbmzt)">
                    <template #reference>
                      <span class="operdel" size="small"><el-icon>
                          <Delete />
                        </el-icon>删除</span>
                    </template>
                  </el-popconfirm>
                     <span class="operesee" @click="handlepj(row,'xq')"><el-icon>
                      <Compass />
                    </el-icon>详情</span>
                </template>
              </el-table-column>
            </el-table>
            <div class="fenye" :style="{ top: tableHeight }">
              <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="listpage.pageCurrent" :page-sizes="[2, 5, 10, 20]" :page-size="listpage.pageSize"
                layout="total, prev, pager, next, sizes, jumper" :total="total"></el-pagination>
            </div>
            </div>
          
          </div>
        </div>
      </div>

    </div>

        <!--请假原因弹窗 -->
    <el-dialog v-model="addyyShow"
      :title=" '【' + titlename + '】不能参加风险辨识的原因'"
      :close-on-click-modal="false" width="50%" :show-close="false">
      <div>
        <el-form class="mosty-from-wrap" ref="elform" label-width="130px" :model="reason" :inline="true"
          :disabled="true">
          <el-form-item prop="bz" style="width: 100%">
            <el-input v-model="reason" type="textarea" :rows="10"    />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addyyShow = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 选择人员 -->
    <ChooseUser v-model="chooseUserVisible" :deptId="deptId" :data="userids" :Single="singleShow" 
      @choosedUsers="getSonValue">
    </ChooseUser>
    <!-- 选择参与部门: -->
    <ChooseDept v-if="showJkssbm" :formInfo="fromObj" :showDdld="showJkssbm" :searchType="'03'" @closezzjg="closeJhss"
      @listdata="getSonValue" :zzlx="zzlx" />
    <!-- 评价 -->
    <div v-if="showpjpage" class="dialog-box">
      <Zxpjindex @close="changepjclose" :idlist="idlist" :tjtype='tjtype' :userId='listpage.userId'/>
    </div>
  </div>
</template>

<script setup>
// 引入
import { defineProps, watch, onMounted, getCurrentInstance, defineEmits, ref } from 'vue'
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
import ChooseDept from "@/components/MyComponents/ChooseDept/Depart.vue";
import * as MOSTY from "@/components/MyComponents/index";
import Zxpjindex from './Zxpjindex.vue';
import { ElMessage } from "element-plus";
import { getItem } from "@/utils/storage";
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
const emits = defineEmits(["close"]);
const { proxy } = getCurrentInstance();
const { GZ_D_ENT_STAGE } = proxy.$dict("GZ_D_ENT_STAGE");
const tableHeight = ref()
const titlename = ref('');
const showpjpage = ref(false)
const addyyShow=ref(false)
const pjxzobj=ref({})
const props = defineProps({
  btnshow: {
    default: true,
    type: Boolean
  },
  initPage: Object,
  title: String,
  planid: {
    type: String,
    default: null
  }
});
const deptId = ref(null)
const tjtype=ref('pj')
const userids = ref([]);
const deptlist = ref([])
const showbtn = ref(false);
const zbbmzt = ref(1)//左边部门状态
const total = ref(0)
const reason = ref(null)
const zzlx = ref(null);
const showJkssbm = ref(false);//选择部门
const proms = ref({
})
const listQuery = ref({})
const listpage = ref({
  pageCurrent: 1,
  pageSize: 10,
  planid: props.planid
})
const tableData1 = ref([])
const tableData2 = ref([])
const tableloading = ref(false)
const chooseUserVisible = ref(false);
const singleShow = ref(true); //单选多选
const tableColumn = ref([
  {
    title: "所属部门",
    prop: "entityName",
    sortable: true
  },
    {
    title: "姓名",
    prop: "username",
    sortable: true
  },
  {
    title: "风险数量",
    prop: "num",
    slot: true,
    sortable: true
  },
  {
    title: "状态",
    prop: "stage",
    sortable: true,
    slot: true
  },
  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])

const idlist = ref({
  planid: props.planid,
  rybsid:null,
})
onMounted(() => {
  proms.value.planid = props.planid
  pjxzobj.value.planid=props.planid
  tabHeightFn();
  getpageDate()
})

//辨识评价-页面数据详情
function getpageDate() {
  // orgcode:只有评价小组登录才传orgcode
  getApi({ planid: props.planid }, '/mosty-jcgl/identifyUser/showSjInfo').then(res => {
    tableData1.value = res.list
    listQuery.value.stage=res.list[0].stage
     let arr= tableData1.value.filter(item=>{
   return (item.orangePointCount + item.redPointCount + item.yellowPointCount
                  + item.bluePointCount)>0
 })
      let arr2= tableData1.value.filter(item=>{
                return item.stage=='21'
    })
   if(arr.length>0){
    handlexg(arr[0])
   }else if(arr2.length>0){
 handlexg(arr2[0])
   }

   if(res.entity){
    pjxzobj.value.userids=res.entity.teamUserids
      pjxzobj.value.usernames=res.entity.teamUsernames
   }
  })
}
//查看原因
function handleyy(row) {
   reason.value= row.reason;
    showbtn.value = false;
  titlename.value = row.username
  addyyShow.value = true

}
//修改评价小组
function ChooesDdld() {
  deptId.value =  getItem("deptId").ssdwid
  userids.value =pjxzobj.value.userids ? pjxzobj.value.userids.split(',') : []
  singleShow.value = false;
  chooseUserVisible.value = true;
}
const getSonValue = (value) => {
  if(value.length>0){
    pjxzobj.value.userids=value.map(it => { return it.id }).toString();
        pjxzobj.value.usernames=value.map(it => { return it.userName }).toString();
postApi(pjxzobj.value,'/mosty-jcgl/identifyUser/addPjxz').then(res=>{
   ElMessage.success("设置成功");
     getpageDate()
})
  }

};
//评价
function handlepj(row,type) {
  idlist.value.rybsid = row.id
 tjtype.value=type
  showpjpage.value = true
}
//关闭汇总
function changepjclose(val) {
  showpjpage.value = false;
     listpage.value.userId = val;
      getpageDate()
    getpjxqTab()
}


//提交审核（负责人提交审核）
function handlehz() {
  // 	辨识评价-各个市、省公司提交审核
  postApi({}, `/mosty-jcgl/identifyUser/tjsh/${props.planid}`).then(res => {
    ElMessage.success("提交成功");
    getpageDate();
    tableData2.value = []
  })
}

const closeJhss = (val) => {
  showJkssbm.value = val
}


//查看
function handlexg(row) {
  listpage.value.planid = row.planId;
     listpage.value.userId = row.userid;
     zbbmzt.value=row.stage
    getpjxqTab()
}
// 风险评价-查询右侧人员风险辨识情况
function getpjxqTab() {
      getApi(listpage.value, '/mosty-jcgl/identifyUser/getRybsListByUserId').then(res => {
      tableData2.value = res.records
      total.value = res.total
    })
}

// 方法
const tabHeightFn = () => {
  tableHeight.value =
    window.innerHeight - 100 - 57 - 60 + 'px'
}
//删除
function deldetebm(row) {
//  人员辨识数据删除
  postApi({ rybsid: row.id}, '/mosty-jcgl/identifyUser/ryDel').then(res => {
    ElMessage.success("删除成功");
    getpageDate()
    getpjxqTab()
  })
}
//返回
function Close() {
  emits("close");
}
//分页
const handleSizeChange = (currentSize) => {
  listpage.value.pageSize = currentSize;
  getpjxqTab()
};
const handleCurrentChange = (currentPage) => {
  listpage.value.pageCurrent = currentPage;
  getpjxqTab()
};
</script>

<style lang="scss" scoped>
.titleBox {
  border: none;
  height: 152px;
  display: flex;
  justify-content: center;
  position: relative;
  z-index: 2;
}

.el-form-item__label {
  padding: 12px 12px 0 0 !important;
}

.stebox {
  width: 90%;
  display: flex;
  justify-content: center;
  position: relative;
  align-items: center;
  margin: o auto;
}

.left {
  width: 70%;
}

.el-select {
  width: 150px;
}

.btnBox {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin: 10px;
}

.bs-title {
  color: red;
  margin: 10px;
}

.leftbox {
  width: 30%;
  margin-right: 40px;
}

.tabBox2 {
  width: 65%;
}

.fil-bbox {
  display: flex;

  .font {
    color: #000;
    font-size: 16px;
    font-weight: 600;
    margin: 10px;

    .labs {
      color: #999494;
    }
  }
}

.operates {
  cursor: pointer;
  color: yellowgreen !important;
  display: inline-block;
  width: 40px;
  height: 23px;
  border: 1px solid #000;
  margin: 0 5px;
  background-color: #fff;
}

.operated {
  cursor: pointer;
  color: #fff !important;
  display: inline-block;
  width: 40px;
  height: 23px;
  border: 1px solid #000;
  margin: 0 5px;
  background-color: #f11515;
}

.redfont {
  color: rgb(245, 34, 34) !important;
}

.box-big {
  display: flex;
  margin-top: 10px;

  .fix-box {
    display: flex;
    margin-right: 30px;
  }
}

.el-textarea {
  --el-input-placeholder-color: #4c4e52 !important;
}

.btnpas {
  display: inline-block;
  padding: 5px 10px;
  color: #fff !important;
  margin-right: 4px;
  background-color: var(--el-color-primary);
  border-radius: 5px;
  cursor: pointer;
}

.fxdjspan {
  display: inline-block !important;
  width: 100px;
  text-align: center;
  padding: 5px 0;
  border-radius: 5px;
  color: #fff !important;
}

.reds {
  background-color: #e80000;
}

.orgs {
  background-color: #ff8040;
}

.blus {
  background-color: #e2d101;
}

.gres {
  background-color: #0080ff;
}
</style>