<template>
  <div class="chart-quality-rule">
    <div class="my-b-title">质检项名称</div>
    <div class="my-search-div">
      <el-input v-model="kewWord" class="search-input"/>
    </div>
    <div class="table-a">
      <my-table
        :filter-meothod="row => (row.NAME||'').includes(kewWord)"

        border
        :has-operation="true"
        :headers="chartQualityCheckRuleTableHeader"
        :table-data="tableData"
        :page-size="8"
      >

        <template #QUALITY_RULE="{data}">
          {{getRule(data.QUALITY_RULE,data)}}
        </template>
        <template #IS_RELATION="{data}">
          {{getIsRe(data.IS_RELATION)}}
        </template>
        <template #operation="{data}">
          <el-button type="info" @click="changeState(data,'启用')" :disabled="!(operationType !== 'detail' && currentOpObj.STATE!=='2')" v-if="data.STATENAME!=='启用'">启用</el-button>
          <el-button type="danger" @click="changeState(data,'禁用')" :disabled="!(operationType !== 'detail' && currentOpObj.STATE!=='2')" v-if="data.STATENAME==='启用'">禁用</el-button>

        </template>
      </my-table>
    </div>
    <div  class="m-s">
      显示第 1 到第 {{totalCount}} 条记录，总共 {{totalCount}} 条记录
    </div>
    <div class="detail-operation-btn" v-if="operationType!=='detail'">
      <el-button :icon="Select" @click="saveS">保存</el-button>
      <el-button :icon="Close" @click="$emit('back')">返回</el-button>
    </div>
  </div>
</template>

<script>
import MyTable from "@/components/common/MyTable.vue";
import {onBeforeMount, ref} from "vue";
import {chartQualityCheckRuleTableHeader} from "@/data/all-table-header";
import {getChartQualityCheckRuleTableData} from "@/service/task-manage/task-set";
import {useRootStore} from "@/stores/useStore";
import {isEmptyCheck} from "@/unit/validate";
import {handleMes, showMessage} from "@/unit/element-ui/tip";
import {Close, Select} from "@element-plus/icons-vue";
import {
  changeQualityTaskHomeStatus,
  getQualityCheckTaskHomeTableData,
  saveQualityDetailSave
} from "@/service/data-quelity-check/quality-check-task";

export default {
  name: "ChartQualityCheckRule",
  computed: {
    Close() {
      return Close
    },
    Select() {
      return Select
    }
  },
  components: {MyTable},
  props:{
    settingType:{

    },
    operationType:{

    },
    currentOpObj:{

    }
  },
  setup(props,{emit}){

    function saveS(){
      saveQualityDetailSave({
        main:{
          "UUID": infoForms.value.UUID,
          "CATALOG_CODE": props.currentOpObj["CATALOG_CODE"],
          "TABLE_SPACE": infoForms.value.TABLE_SPACE,
          "TABLE_NAME": infoForms.value.TABLE_NAME,
          "ORDER_NUM": infoForms.value.ORDER_NUM,


          "EXEC_CYCLE":infoForms.value.EXEC_CYCLE,
          STATE:props.currentOpObj.STATE,
          "CYCLE_CLASS":infoForms.value.CYCLE_CLASS,

          // "EXEC_TIME": EXEC_TIME,
          "SPACE_TIME":infoForms.value.SPACE_TIME,
          "SPACE_NUM":infoForms.value.SPACE_NUM,

          "IS_RETRY": infoForms.value.IS_RETRY,
          "TIME_INTERVAL": infoForms.value.TIME_INTERVAL,

          "IS_EXEC": "0"
        },
        rules:tableData.value.map(i => {
          return {
            ...i,
            STATE:i.STATENAME
          }
        })
      }).then(res => {

        handleMes(res,() => {
          emit('back')

        })
      })
    }
    const kewWord = ref('')
    const totalCount = ref(0)
    const infoForms = ref({})
    const tableData = ref([])
   function changeState(data,state){
     data.QYZT = '已' + state


     data.STATENAME = state
     showMessage({
       type:'success',
       message:data.QYZT
     })
   }
    function getConfigName(type, pcode) {
      let  json_configs
      //json_configs
      if (json_configs == null) {
        let para_configs = useRootStore().paraArr
        for (var i = 0 ; i < para_configs.length ; i++) {
          if (json_configs == null || isEmptyCheck(json_configs[para_configs[i]["PARA_CODE"]])) {
            if (json_configs == null) {
              json_configs = {};
            }
            var code = para_configs[i]["CODE"];
            var name = para_configs[i]["NAME"];
            json_configs[para_configs[i]["PARA_CODE"]] = {};
            json_configs[para_configs[i]["PARA_CODE"]][code] = name;
          } else {
            var code = para_configs[i]["CODE"];
            var name = para_configs[i]["NAME"];
            json_configs[para_configs[i]["PARA_CODE"]][code] = name;
          }
        }
      }
      return json_configs[type][pcode] == null || json_configs[type][pcode] == "" ? pcode : json_configs[type][pcode];
    }
    function getRule (value,row) {
      if (value != '[]' && value != '' && value != undefined) {
        var rule_value = JSON.parse(value);
        var rule_string = '';
        switch (row.CODE) {
          case "C0001":
            rule_string = '是否非空：'
            rule_string += rule_value[0].NOT_NULL == '1' ? '是' : '否';
            return rule_string;
          case "C0002":

            rule_string = getConfigName('90', rule_value[0].CHACK_TYPE) + ":" + rule_value[0].CHACK_VALUE + ";";
            return rule_string;
          case "C0003":
            rule_string = '是否唯一：'
            rule_string += rule_value[0].ONLY == '1' ? '是' : '否';
            return rule_string;
          case "C0004":
            getConfigName('92', rule_value[0].DATA_FORMAT);;
            rule_string += getConfigName('92', rule_value[0].DATA_FORMAT) + ";";
            return rule_string;
          case "C0005":
            getConfigName('90', rule_value[0].CHACK_TYPE);
            getConfigName('91', rule_value[0].DATA_TYPE);;
            rule_string += getConfigName('91', rule_value[0].DATA_TYPE) + ":" + getConfigName('90', rule_value[0].CHACK_TYPE) + ":" + rule_value[0].VALUE + ";";
            return rule_string;
          case "C0006":

            return rule_string;
          case "C0007":
            for (var i = 0; i < rule_value.length; i++) {
              rule_string +=  rule_value[i].VALUE + ";";
            }
            if (rule_string.length > 13) {
              return "<div title='" + rule_string + "' href='#' style='cursor:pointer;text-decoration: none;'>" + rule_string.trim().substr(0, 13) + "..." + "</div>";
            } else {
              return rule_string;
            }
          case "C0009":
            rule_string = '数据字典'
            return rule_string;
          case "C0010":
            rule_string = '文件验证';
            return rule_string;
          case "C0011":
            rule_string = '截取验证';
            return rule_string;
          case "C0013":
            rule_string = '判断节点是否合法';
            return rule_string;
        }
      }
    }
    function getIsRe(value) {
      switch (value) {
        case "0":
          return "否";
        case "1":
          return "是";
      }
    }
    onBeforeMount(() => {
      getQualityCheckTaskHomeTableData({
        "pageIndex": 1,
        "pageSize": 1,
        "CODE": props.currentOpObj["CATALOG_CODE"],
        "appkey": useRootStore().userInfo.ptjk_userkeycode
      }).then(res => {
        infoForms.value = {
          ...res.msg[0].Rows[0]
        }
      })
      getChartQualityCheckRuleTableData({
        "pageIndex": 1,
        "pageSize": 20000,
        "CODE": props.currentOpObj.CATALOG_CODE,
        "FIELD_NAME": "",
        "TYPE_CODE": "",
        "IS_RELATION": "",
        "STATE": "",
        "appkey": "F6D5D1F6E6A74F069716DA8B8AE5F115"
      }).then(res => {
        tableData.value = (res.msg[0].Rows||[]).map(i => {
          return {
            ...i,
            STATENAME:i.STATE === '1'?'启用':'禁用',
            QYZT:i.STATE === '1'?'已启用':'已禁用'
          }
        })
        totalCount.value = res.msg[0].TotalRowCount
      })
    })
    return {
      kewWord,
      chartQualityCheckRuleTableHeader,
      totalCount,tableData,
      getIsRe,
      getRule,
      changeState,
      saveS,
      infoForms
    }
  }
}
</script>

<style scoped lang="scss">
.chart-quality-rule{
  padding:0 20px;
}
</style>
