<template>
  <div>
    <div>
      <el-breadcrumb separator="/" class="my-breadcrumb">
        <el-breadcrumb-item>数据清洗 </el-breadcrumb-item>
        <el-breadcrumb-item>清洗任务</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="back" @click="$emit('changeOperation','',{})"><span class="fa fa-reply cur-po " >返回</span></div>

    <div class="detail-main">
      <div class="block">
        <div class="my-b-title">基本信息</div>
        <div class="form"><diy-el-form v-model:model-form="forms" :forms="dataTaskDetailBaseForm"/></div>
      </div>
      <div class="block">
        <div class="my-b-title">清洗项信息</div>
        <div class="my-search-div">
          <el-input class="search-input"/>
        </div>
        <my-table
          :filter-meothod="row=>(row.CLEANING_RULE_UUID ||'').includes(keyWord)"
          :is-height-equal-max-height="true"
          :page-size="8"
          :has-operation="true"

          border
          :table-data="tableData"
          :headers="dataTaskSetTableHeader"

        >
          <template #CLEANING_RULE_UUID="{data}">
            {{getClearName(data.CLEANING_RULE_UUID)}}
          </template>
          <template #DEPLOY_STATUS="{data}">
            {{getStatus(data.DEPLOY_STATUS)}}
          </template>
          <template #operation="{data}">
            <template v-if="data.DEPLOY_STATUS === '0'">
              <el-button type="info" @click="changeState(data,'1')">启用</el-button>
            </template>
            <template v-else-if="data.DEPLOY_STATUS === '1'">
              <el-button type="danger" @click="changeState(data,'0')">禁用</el-button>
            </template>
            <template v-else-if="data.DEPLOY_STATUS === '2'">
              <el-button type="info" disabled>运行中</el-button>
            </template>
          </template>
        </my-table>
        <div class="m-s">
          显示第 {{1 }} 到第 {{totalCount}} 条记录，总共 {{totalCount}} 条记录

        </div>
      </div>
      <div class="block">
        <div class="my-b-title">任务说明</div>
        <div class=""><diy-el-form v-model:model-form="forms" :forms="dataTaskDetailBaseForm2"/></div>
      </div>
      <div class="detail-operation-btn" v-if="operationType !== 'detail'">
        <el-button :icon="Refresh" @click="circleYx">运行</el-button>
        <el-button :icon="Select" @click="saveS">保存</el-button>
        <el-button :icon="Close" @click="$emit('changeOperation','')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {dataTaskDetailBaseForm, dataTaskDetailBaseForm2} from "@/data/all-forms";
import DiyElForm from "@/components/common/DiyElForm.vue";
import {onBeforeMount, ref} from "vue";
import MyTable from "@/components/common/MyTable.vue";
import {dataTaskSetTableHeader} from "@/data/all-table-header";
import {
  addClearTaskDetailD,
  changeClearTaskDetailStatus, fetClearDetailInfo,
  getClearTaskDetailTableData,
  updateClearTaskDetailD
} from "@/service/data-clear";
import {getClearRuleDetailClearSelect, getClearRuleDetailTableData} from "@/service/zsk/clear-rule";
import {elAlert, handleMes} from "@/unit/element-ui/tip";
import {Close, Refresh, Select} from "@element-plus/icons-vue";

export default {
  name: "DataTaskSet",
  computed: {
    Close() {
      return Close
    },
    Select() {
      return Select
    },
    Refresh() {
      return Refresh
    }
  },
  components: {MyTable, DiyElForm},
  props:{
   currentObj: {

   } ,
    operationType:{

    }
  },
  setup(props,{emit}) {
    function circleYx(){
      if (!(tableData.value.length > 0)){
        elAlert('请先配置清洗规则')
        return
      }
      let url = addClearTaskDetailD
    let params = {
      "uuid":props.currentObj.CATALOG_CODE,    //$("#QX_UUID").val()
      "taskStatus": "1",
      "remark":forms.value.REMARK,
      "inspMainUuid":  props.currentObj.UUID,
      "exeDate": '1',
      "spaceNum": '',
      "isRetry": '0',
      "spaceTime": '',
      "cycleClass":  '',
      "execCycle": forms.value.FREQUENT,
    }
      if ([0,1,2].includes(+props.currentObj.CLEANING_TASK_STATUS)){
        url = updateClearTaskDetailD
      }
      url(params).then(res => {
        handleMes(res,() => {
          emit('changeOperation','')
        })
      })
    }
   function saveS() {
     if (!(tableData.value.length > 0)){
       elAlert('请先配置清洗规则')
     }
      let url = addClearTaskDetailD
     let params ={
       "uuid":props.currentObj.CATALOG_CODE,    //$("#QX_UUID").val()
       "taskStatus": "0",
       "remark": forms.value.REMARK,
       "inspMainUuid": props.currentObj.UUID,
       "exeDate": '1',
       "spaceNum": '',
       "isRetry": "0",
       "spaceTime": '',
       "cycleClass":  '',
       "execCycle": forms.value.FREQUENT,
     }
      if ([0,1,2].includes(+props.currentObj.CLEANING_TASK_STATUS)){
        url = updateClearTaskDetailD
      }
      url(params).then(res => {
         handleMes(res,() => {
              emit('changeOperation','')
         })
      })

   }
    const allClearSelectArr = ref([])
    onBeforeMount(() => {
      getClearRuleDetailClearSelect().then(res => {
        allClearSelectArr.value = res.msg[0].Rows
      })
      fetClearDetailInfo(props.currentObj.CATALOG_CODE).then(res => {
        if (res.msg[0]?.Rows[0]){
          console.log('有数据')
          forms.value = {
            ...forms.value,
            ...res.msg[0]?.Rows[0],
            REMARK:res.msg[0]?.Rows[0]?.REMARK
          }
        }

      })
    })
    function getClearName(state) {
      return allClearSelectArr.value.find(i => i.CLEANING_RULE_UUID === state)?.CLEANNAME
    }
    function changeState(data,state){
      let findT = allClearSelectArr.value.find(i => i.CLEANING_RULE_UUID === state)
      let params  = {
        deployStatus:state,
        cleaningDeployUUID:data.CLEANING_DEPLOY_UUID,
        cleaningRule:data?.CLEANING_RULE || '',
        cleaningRuleUUID:data?.CLEANING_RULE_UUID || '',
      }

      changeClearTaskDetailStatus({
        deployStatus:state,
        cleaningDeployUUID:data.CLEANING_DEPLOY_UUID,
        cleaningRule:data?.CLEANING_RULE || '',
        cleaningRuleUUID:data?.CLEANING_RULE_UUID || '',

      }).then(res => {
        handleMes(res,() => {
          getTableList()
        })
      })
    }
    function getStatus(status){
      switch (status) {
        case '0':
         return '未启用'
        case '1':
          return '已启用'
        case '2':
          return '正在运行'
      }
    }
    const totalCount = ref(0)
    const keyWord = ref('')
    const tableData = ref([])
    const forms = ref({

    })
    function getTableList() {
      getClearRuleDetailTableData({
        "pageIndex": 1,
        "pageSize": 20000,
        "cid": props.currentObj.CATALOG_CODE
      }).then(res=>{
        tableData.value = res.msg[0].Rows
        totalCount.value = res.msg[0].TotalRowCount
      })
    }
    onBeforeMount(() => {
       if (props.operationType!=='add'){
         forms.value = {
           ...props.currentObj
         }
       }
      getTableList()
    })


    return {
      dataTaskDetailBaseForm,
      dataTaskDetailBaseForm2,
      forms,
      dataTaskSetTableHeader,
      tableData,
      totalCount,
      keyWord,
      getStatus,
      changeState,
      allClearSelectArr,
      getClearName,
      saveS,
      circleYx
    }
  }
}
</script>

<style scoped lang="scss">
.detail-main{
  .form{
    max-width: 600px;
  }
  padding: 0 20px;
}
</style>
