<template>
  <div>
    <div class="form-a">
      <diy-el-form v-model:model-form="filterForm" :span-attrs="{
      span:5
    }" :forms="currentReflectObj.filterForm">
        <template #searchBtn>
          <el-button type="primary" :icon="Search" @click="initSearch">查询</el-button>
        </template>
        <template #addBtn>
          <el-button type="primary" :icon="Plus" @click="openEditDialog('add')">新增</el-button>

        </template>

      </diy-el-form>
    </div>
    <div class="mb15">
      <span class="my-data-source">数据资源</span>
      <span class="my-total">共 <strong>{{totalCount}}</strong> 个目录项</span>
    </div>
    <my-table
      :has-operation="true"
      :is-height-equal-max-height="true"
      border
      :page-size="8"
      :headers="currentReflectObj.tableHeader"
      :table-data="tableData"


    >
      <template #SAFE_STATE="{data}">
        {{(+data['SAFE_STATE'] === 1)?'可用':'不可用'}}
      </template>
      <template #SAFE_LEVEL_NAME="{data}">
        {{store.safeLevelArr.find(i => i.value === data.SAFE_LEVEL_NAME)?.label || '-'}}
      </template>
      <template #SAFE_TYPE="{data}">
        {{(+data['SAFE_TYPE'] === 0)?'脱敏':'加密'}}
      </template>
      <template #SAFE_AUDIT_STATE="{data}">
        {{(+data['SAFE_AUDIT_STATE'] === 1)?'可用':'不可用'}}
      </template>
      <template #HJ_STATES="{data}">
        {{(+data['HJ_STATES'] === 1)?'可用':'不可用'}}
      </template>
      <template #SAFE_AUDIT_NAME="{data}">
        {{getCheckS(data.SAFE_AUDIT_NAME)}}
      </template>
      <template #operation="{data}">
        <el-button type="info" @click="openEditDialog('edit',data)">编辑</el-button>
        <el-button type="info" @click="delData(data)">删除</el-button>
      </template>
    </my-table>
    <ele-pagination ref="refEl"

                     @sizeOrPageChange="handleSizeChange"
                     class="page"
                     :default-page-size="pageSize"


                     :total-count="totalCount">

      <template #default>
        <div class="m-s">
          显示第 {{pageSize * (pageIndex -1) + 1 }} 到第 {{pageSize * pageIndex}} 条记录，总共 {{totalCount}} 条记录 共显示

        </div>
      </template>
    </ele-pagination>

   <template v-if="isEditDialogShow">
<!--     安全类型-->
     <safe-type-edit-dialog

       @editOrAddSfeType="editOrAddSafeType"
       :dialog-operation-type="dialogOperationType" :current-dialog-op-obj="currentDialogOpObj" :type="type" v-model:dialog-visible="isEditDialogShow"/>
   </template>
    <template v-if="isCheckDialogShow">
<!--      审计的弹出框-->
      <check-type-dialog
        @editOrAddCheckType="editOrAddCheckType"
        :dialog-operation-type="dialogOperationType" :current-dialog-op-obj="currentDialogOpObj"
        :table-header="baseRuleCheckDialogTableHeader"
        :base-form="checkInfoDialogBaseForm" title="配置审计类型"
        :type="type"
        v-model:dialog-visible="isCheckDialogShow"/>
    </template>
    <template v-if="isCircleDialogShow">
      <!--      环节类型的弹出框-->
      <check-type-dialog
        @editOrAddCheckType="editOrAddCircleType"
        :dialog-operation-type="dialogOperationType" :current-dialog-op-obj="currentDialogOpObj"

        :table-header="baseRuleCircleDialogTableHeader" :base-form="circleInfoDialogBaseForm" title="配置环节模式" :type="type" v-model:dialog-visible="isCircleDialogShow"/>
    </template>
  </div>
</template>

<script>
import DiyElForm from "@/components/common/DiyElForm.vue";
import {computed, ref} from "vue";
import {checkInfoDialogBaseForm, circleInfoDialogBaseForm, safeTypeFilterForm} from "@/data/all-forms";
import {Plus, Search} from "@element-plus/icons-vue";
import LabelTypeTabContent from "@/view/other/c-views/data-handle/zsk/base-rule/tabs/LabelTypeTabContent.vue";
import MyTable from "@/components/common/MyTable.vue";
import {
  baseRuleCheckDialogTableHeader,
  baseRuleCircleDialogTableHeader,
  baseRuleSafeTableHeader
} from "@/data/all-table-header";
import SafeTypeEditDialog from "@/view/other/c-views/data-handle/zsk/base-rule/tabs/SafeTypeEditDialog.vue";
import {baseUseSafeTohJType} from "@/data/all-reflect";
import CheckTypeDialog from "@/view/other/c-views/data-handle/zsk/base-rule/tabs/CheckTypeDialog.vue";
import ElePagination from "@/components/common/ElePagination.vue";
import {useClickRouterToHome, usePagination} from "@/unit/use-method/usePagination";
import {
  addSafeRuleTypeD,
  deleteSafeRuleTypeD,
  editSafeRuleTypeD,
  getSafeTypeHomeTableData
} from "@/service/zsk/base-rule/safe-type";
import {handleMes, showConfirm, showMessage} from "@/unit/element-ui/tip";
import {deleteProcessMb} from "@/service/zsk/process-mb";
import {deleteSafeRuleRankD} from "@/service/zsk/base-rule/safe-rank";
import {useRootStore} from "@/stores/useStore";
import {addCheckTypeD, editCheckTypeD} from "@/service/zsk/base-rule/check-type";
import {addCircleDialogConfigTable, editCircleDialogConfigTable} from "@/service/zsk/base-rule/circle-type";

export default {
  name: "SafeType",

  props:{
    type:{

    }
  },
  computed: {
    Plus() {
      return Plus
    },
    Search() {
      return Search
    }
  },
  components: {ElePagination, CheckTypeDialog, SafeTypeEditDialog, MyTable, LabelTypeTabContent, DiyElForm},
  setup(props,{emit}) {
    const store = useRootStore()
    function getCheckS(state) {
      let staObj = {
        '0':'低风险',
        '1':'中风险',
        '2':'高风险',
      }
      return staObj[state] || '-'
    }
    function editOrAddCircleType(form,tableData) {
      let url = addCircleDialogConfigTable
      let params = {
        ORIGION_MODE_CODE:form.ORIGION_MODE_CODE,
        ORIGION_MODE:form.ORIGION_MODE,
      }
      if (dialogOperationType.value === 'edit'){
        params = {
          ORIGION_MODE_CODE:form.ORIGION_MODE_CODE,
          ORIGION_MODE:form.ORIGION_MODE,
          id:currentDialogOpObj.value.ORIGION_ID,
          detailList:tableData.map(i => {
            return {
              ORIGION_DETAIL_CODE:i.ORIGION_DETAIL_CODE,
              ORIGION_DETAIL_TYPE:i.ORIGION_DETAIL_TYPE,
            }
          })
        }
        url = editCircleDialogConfigTable
      }
      url(params).then(res => {
       if (dialogOperationType.type === 'edit'){
         try {
           let data = res.msg[0].Rows[0].status === 'success'
           if (data){
             showMessage({
               type:'success',
               message:'操作成功'
             })
             isCircleDialogShow.value = false
             getTableList()
           }
         }catch {
           showMessage({
             type:'error',
             message:'操作失败'
           })
         }
       }else {

         handleMes(res,() => {
           isCircleDialogShow.value = false
           getTableList()
         })
       }
      })

    }
    function delData(data){
      showConfirm(() => {
        currentReflectObj.value.delUrl(data.SAFE_TYPE_ID || data.SAFE_LEVEL_ID || data.SAFE_AUDIT_ID || data.ORIGION_ID).then(res => {
          if (props.type !== 'hjlx'){
            handleMes(res,() => {
              getTableList()
            })
          }else {
            try {
              let data = res.msg[0].Rows[0].status === 'success'
              if (data){
                showMessage({
                  type:'success',
                  message:'删除成功'
                })
                isCircleDialogShow.value = false
                getTableList()
              }
            }catch {
              showMessage({
                type:'error',
                message:'删除失败'
              })
            }
          }
        })
      },'确定要删除吗','系统提示',{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
    }
    function getTableList() {
      let params = {
        "pageIndex": pageIndex.value,
        "pageSize": pageSize.value,
        "SAFE_TYPE_NAME":filterForm.value.SAFE_TYPE_NAME ,
        "SAFE_TYPE":'',
      }
      if (props.type ==='aqjb'){
        params =  {
          "pageIndex": pageIndex.value,
          "pageSize": pageSize.value,
          "SAFE_LEVEL":filterForm.value.SAFE_LEVEL || '' ,

        }
      }else if (props.type === 'sjlx'){
        params =  {
          "pageIndex": pageIndex.value,
          "pageSize": pageSize.value,
          "SAFE_AUDIT_NAME":filterForm.value.SAFE_AUDIT_NAME || '' ,

        }
      }else if (props.type === 'hjlx'){
        params =  {
          "pageIndex": pageIndex.value,
          "pageSize": pageSize.value,
          "ORIGION_MODE":filterForm.value.ORIGION_MODE || '' ,

        }
      }
      currentReflectObj.value.homeUrl && currentReflectObj.value.homeUrl(params).then(res => {
        tableData.value = res.msg[0].Rows.map(i => {
           let ovj ={
             ...i,

           }
         if (props.type ==='aqlx'){
           ovj.SAFE_LEVEL_NAME = ovj.SAFE_LEVEL
         }
         if (props.type ==='HJlx'){
           ovj.HJ_STATES = ovj.STATES
         }
           return ovj
        })
        totalCount.value = res.msg[0].TotalRowCount
      })
    }
    const {changeOperation:changeDialogOperation,operationType:dialogOperationType,currentOpObj:currentDialogOpObj} = useClickRouterToHome()
    const {pageSize,pageIndex,handleSizeChange,refEl,totalCount,tableData,initSearch} = usePagination(getTableList)
    const isEditDialogShow = ref(false)
    const isCircleDialogShow = ref(false)
    const isCheckDialogShow = ref(false)
    const currentReflectObj = computed(() => {
      return baseUseSafeTohJType.find(i => i.id === props.type) || {
        filterForm:{
          formItemsArr:[]
        },
        tableHeader:[],
        homeUrl:getSafeTypeHomeTableData
      }
    })
    function openEditDialog(type,item){
      if (props.type === 'sjlx'){
        isCheckDialogShow.value = true
      }else  if (props.type === 'hjlx'){
        isCircleDialogShow.value = true
      }else {
        isEditDialogShow.value = true
      }

     changeDialogOperation(type,item)

    }
    function editOrAddCheckType(formV,tableD) {
      let url = addCheckTypeD
      let params ={
        SAFE_AUDIT_NAME:formV.SAFE_AUDIT_NAME,
        SAFE_AUDIT_VALUES:tableD.map(i => i.zd).join(','),

      }
      if (dialogOperationType.value !== 'add'){
        params.SAFE_AUDIT_ID=formV.SAFE_AUDIT_ID
        url = editCheckTypeD
      }
      url(params).then(res => {
        handleMes(res,() => {
          isCheckDialogShow.value = false
          getTableList()
        })
      })
      // 审计类型的编辑和新增
    }
    function editOrAddSafeType(form) {
       // 安全类型的编辑和保存
       let url = currentReflectObj.value.addUrl
      let params
      if (props.type === 'aqlx'){
        params  = {
          SAFE_TYPE_NAME:form.SAFE_TYPE_NAME,
          SAFE_LEVEL:form.SAFE_LEVEL,
          SAFE_TYPE:form.SAFE_TYPE,
          SAFE_REMARKS:form.SAFE_REMARKS,
        }
        if (dialogOperationType.value !== 'add'){
          url = currentReflectObj.value.editUrl
          params.SAFE_TYPE_ID = currentDialogOpObj.value.SAFE_TYPE_ID
        }
      }else if (props.type === 'aqjb'){
        params  = {
          SAFE_LEVEL:form.SAFE_LEVEL,
          SAFE_REMARKS:form.SAFE_REMARKS,
        }
        if (dialogOperationType.value !== 'add'){
          url = currentReflectObj.value.editUrl
          params.SAFE_LEVEL_ID = currentDialogOpObj.value.SAFE_LEVEL_ID
        }
      }


       url(params).then(res => {
         handleMes(res,()=>{
           isEditDialogShow.value = false
           getTableList()
         })
       })
    }
    const filterForm = ref({

    })
    return {
      filterForm,
      safeTypeFilterForm,
      baseRuleSafeTableHeader,
      openEditDialog,
      isEditDialogShow,
      currentReflectObj,
      isCheckDialogShow,
      isCircleDialogShow,
      checkInfoDialogBaseForm,
      circleInfoDialogBaseForm,
      baseRuleCheckDialogTableHeader,
      baseRuleCircleDialogTableHeader,
      pageSize,pageIndex,handleSizeChange,refEl,totalCount,tableData,initSearch,
      changeDialogOperation,currentDialogOpObj,dialogOperationType,
      editOrAddSafeType,
      delData,
      getCheckS,
      store,
      editOrAddCheckType,
      editOrAddCircleType

    }
  }
}
</script>

<style scoped lang="scss">
.form-a{
  padding-left: 20px;
  margin-bottom: 20px;
  padding-top: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--el-border-color-light);
}
:deep{
  .el-tabs__content{
    padding: 20px;
    box-sizing: border-box;
    border: 1px solid var(--el-border-color-light);
  }
}
</style>
