<template>
  <div class="process-start">
    <div class="my-b-title">基本信息</div>
    <div class="form">
      <diy-el-form v-model:model-form="forms" :forms="formConfig1"/>
    </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 {onBeforeMount, ref, watch} from "vue";
import DiyElForm from "@/components/common/DiyElForm.vue";
import { processStartForm} from "@/data/all-forms";
import {
  changeQualityTaskHomeStatus,
  getQualityCheckTaskHomeTableData,
  saveQualityDetailSave
} from "@/service/data-quelity-check/quality-check-task";
import {useRootStore} from "@/stores/useStore";
import {Close, Select} from "@element-plus/icons-vue";
import {getChartQualityCheckRuleTableData} from "@/service/task-manage/task-set";
import {handleMes, showMessage} from "@/unit/element-ui/tip";

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

    },
    operationType:{

    },
    currentOpObj:{

    }
  },
  setup(props,{emit}) {
    const tableData = ref([])
    const forms = ref({

    })
    function saveS(){
      let EXEC_TIME
      if (forms.value.cycle === '0'){
        //  周期执行
        if (forms.value.frequency === '年'){
          EXEC_TIME = [forms.value.month,forms.value.day,forms.value.time].join('-')

        } else if (forms.value.frequency === '月'){
          EXEC_TIME = [forms.value.day,forms.value.time].join('-')

        } else if (forms.value.frequency === '周'){
          EXEC_TIME = [forms.value.day,forms.value.time].join('-')

        }else if (forms.value.frequency === '日'){
          EXEC_TIME = [forms.value.time].join('-')

        }else if (forms.value.frequency === '时'){
          EXEC_TIME = [forms.value.time].join('-')
        }
      }
      else if (forms.value.cycle === '1'){
        EXEC_TIME = [forms.value.singleYear,forms.value.singleMonth,forms.value.singleDay,forms.value.singleTime].join('-')

      }
      saveQualityDetailSave({
        main:{
          "UUID": forms.value.UUID,
          "CATALOG_CODE": props.currentOpObj["CATALOG_CODE"],
          "TABLE_SPACE": forms.value.TABLE_SPACE,
          "TABLE_NAME": forms.value.TABLE_NAME,
          "ORDER_NUM": forms.value.ORDER_NUM + '',
          "STATE":forms.value.STATE,
          // "EXEC_TIME": EXEC_TIME,
          "SPACE_TIME":forms.value.SPACE_TIME,
          "SPACE_NUM":forms.value.SPACE_NUM,

          "IS_RETRY": forms.value.IS_RETRY,
          "TIME_INTERVAL": forms.value.jg,

          "IS_EXEC": "0",
          EXEC_CYCLE:forms.value.cycle,
          CYCLE_CLASS:forms.value.frequency,
          EXEC_TIME:EXEC_TIME,

        },
        rules:tableData.value.map(i => {
          let oObj = {
            ...i,
            STATE:i.STATENAME
          }
          delete oObj.STATENAME
          return oObj
        })
      }).then(res => {
        handleMes(res,() => {
          emit('back')
        })
          // if (res.msg[0].success === 'true'){
          //   changeQualityTaskHomeStatus(props.currentOpObj.CATALOG_CODE,'1').then(res => {
          //     handleMes(res,() => {
          //       emit('back')
          //     },()=>{},'操作成功','操作失败')
          //   })
          // }else {
          //   showMessage({
          //     type:'error',
          //     message:res.msg[0].error || '操作失败'
          //   })
          // }
          // try {
          //
          // }catch  {
          //   showMessage({
          //     type:'error',
          //     message:'操作失败'
          //   })
          // }


      })
    }
    const formConfig1 = ref({...processStartForm()})
    onBeforeMount(() => {
      watch(() =>forms.value.cycle,(val) => {
        console.log('执行周期变化',val)
        const beforeForm =  processStartForm().formItemsArr.slice(0,4)
        let findItemCycle = formConfig1.value.formItemsArr.find(i => i.modelValue === 'cycle')
        let findAdd = findItemCycle.options.find(i => i.value === val)
        console.log('fafs',findAdd)
        if (findAdd){
          formConfig1.value.formItemsArr  =[...beforeForm,...findAdd.forms]
        }else {
          formConfig1.value.formItemsArr = beforeForm
        }
        let val2 = forms.value['frequency']
        if (val2){
          let findFrequencyIndex = formConfig1.value.formItemsArr.findIndex(i => i.modelValue === 'frequency')
          console.log('findFrequencyIndex',findFrequencyIndex)
          if (findFrequencyIndex===-1){
            return
          }
          let findAdd = formConfig1.value.formItemsArr[findFrequencyIndex].radioArr.find(i => i.value === val2)
          if (!findAdd) {
            return;
          }
          formConfig1.value.formItemsArr = [...formConfig1.value.formItemsArr.slice(0,findFrequencyIndex + 1),...findAdd.forms]
        }

      })
      watch(() => forms.value['frequency'],(val) => {
        if (val){
          let findFrequencyIndex = formConfig1.value.formItemsArr.findIndex(i => i.modelValue === 'frequency')
          console.log('findFrequencyIndex',findFrequencyIndex)
          if (findFrequencyIndex===-1){
            return
          }
          let findAdd = formConfig1.value.formItemsArr[findFrequencyIndex].radioArr.find(i => i.value === val)
          if (!findAdd) {
            return;
          }
          formConfig1.value.formItemsArr = [...formConfig1.value.formItemsArr.slice(0,findFrequencyIndex + 1),...findAdd.forms]
        }
      })

      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'?'已启用':'已禁用'
          }
        })

      })

      getQualityCheckTaskHomeTableData({
        "pageIndex": 1,
        "pageSize": 1,
        "CODE": props.currentOpObj["CATALOG_CODE"],
        "appkey": useRootStore().userInfo.ptjk_userkeycode
      }).then(res => {
        forms.value = {
          ...res.msg[0].Rows[0],
          ORDER_NUM:res.msg[0].Rows[0].ORDER_NUM + '',
          cycle:res.msg[0].Rows[0].EXEC_CYCLE,
          frequency:res.msg[0].Rows[0].CYCLE_CLASS,
          jg:res.msg[0].Rows[0].TIME_INTERVAL
        }
        const splitA = (forms.value.EXEC_TIME || '').split('-')
        if (splitA.length > 0){
          if (forms.value.cycle === '0'){
            //  周期执行
            if (forms.value.frequency === '年'){
              forms.value.month = splitA[0] || ''
              forms.value.day = splitA[1] || ''
              forms.value.time = splitA[2] || ''
            } else if (forms.value.frequency === '月'){

              forms.value.day = splitA[0] || ''
              forms.value.time = splitA[1] || ''
            } else if (forms.value.frequency === '周'){

              forms.value.day = splitA[0] || ''
              forms.value.time = splitA[1] || ''
            }else if (forms.value.frequency === '日'){
              forms.value.time = splitA[0] || ''
            }else if (forms.value.frequency === '时'){
              forms.value.time = splitA[0] || ''
              // forms.value.jg = TIME_INTERVAL
            }
          }
          else if (forms.value.cycle === '1'){
            forms.value.singleYear = splitA[0] || ''
            forms.value.singleMonth = splitA[1] || ''
            forms.value.singleDay = splitA[2] || ''
            forms.value.singleTime = splitA[3] || ''
          }
        }
      })
    })
    return {
      forms,
      processStartForm,
      formConfig1,
      saveS
    }
  }
}
</script>

<style scoped lang="scss">
.form{
  max-width: 700px;
}
.process-start{
  padding:0 20px;
}
</style>
