<template>
 <div class="h100">
   <common-layout
     ref="layoutRef"
     :get-table-list="getTableList"
     :table-attrs="{
       'has-operation':true,
       operationWidth:'300px'
     }"
     :form-attrs="{
       spanAttrs: {
         span:4
       }
     }"
     :table-header="qualityCheckTaskHomeTableHeader"
     :filter-form="qualityCheckTaskHomeFilterForm()"
   >
     <template #nextTime="{data}">
       {{getNextTime(data)}}
     </template>
     <template #STATE="{data}">
       {{getStatus(data.STATE)}}
     </template>
   <template #operation="{data}">
    <template v-if="data.STATE === '0'">
      <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
      <el-button type="info" @click="$emit('changeOperation','set',data)">配置</el-button>
    </template>
    <template v-else-if="data.STATE === '1'">
      <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
      <el-button type="info" @click="changeStatus(data,'2')">停止</el-button>
    </template>
     <template v-else-if="data.STATE === '2'">
       <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
       <el-button type="info" @click="$emit('changeOperation','set',data)">配置</el-button>
       <el-button type="info" @click="changeStatus(data,'1')">运行</el-button>
     </template>
     <template v-else-if="data.STATE === '5'">
       <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
       <el-button type="info" disabled >质检中</el-button>
     </template>
     <template v-else-if="data.STATE === '10'">
       <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
       <el-button type="info" @click="$emit('changeOperation','set',data)">配置</el-button>
       <el-button type="info" disabled >质检异常</el-button>
     </template>
     <template v-else>
       <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
     </template>
   </template>
   </common-layout>
 </div>
</template>

<script>
import CommonLayout from "@/components/CommonLayout.vue";
import {qualityCheckTaskHomeFilterForm} from "@/data/all-forms";
import {qualityCheckTaskHomeTableHeader} from "@/data/all-table-header";
import {ref} from "vue";
import {
  changeQualityTaskHomeStatus,
  getQualityCheckTaskHomeTableData
} from "@/service/data-quelity-check/quality-check-task";
import {isEmptyCheck} from "@/unit/validate";
import {getDateToString, getDateToString_ymd, getDateToStrNum, GetMonday} from "@/unit/time";
import {handleMes} from "@/unit/element-ui/tip";
import {useActiveGetTableData} from "@/unit/use-method/usePagination";

export default {
  name: "QualityCheckTaskHome",
  components: {CommonLayout},
  setup() {
    useActiveGetTableData(getTableList)
    const layoutRef = ref(null)
    function changeStatus(data,state){
      changeQualityTaskHomeStatus(data.CATALOG_CODE,state).then(res => {
        handleMes(res,() => {
          getTableList()
        })
      })
    }
    function getStatus(value){
      switch (value) {
        case "0":
          return "未配置";
        case "1":
          return "待质检";
        case "2":
          return "停止";
        case "5":
          return "质检中";
        case "10":
          return "质检异常";
        default:
            return ''
      }
    }
    function getNextTime(row) {
      if (isEmptyCheck(row["EXEC_TIME"])) {
        return "";
      }
      var temptime = row["EXEC_TIME"];
      temptime = temptime.split("-");
      if (row["EXEC_CYCLE"] == "0") {//周期执行
        var temp = "";
        if (row["EXEC_CYCLE"] == 0) {
          switch (row["CYCLE_CLASS"]) {
            case "年":
              temp = getDateToStrNum(new Date()).toString().substr(0, 4);
              if (isEmptyCheck(row["INSPECTION_DATE"])) {
                return temp + "-" + temptime[0] + "-" + temptime[1] + " " + temptime[2] + ":00:00";
              }
              if (new Date(row["INSPECTION_DATE"]).getTime() < new Date(temp + "-" + temptime[0] + "-" + temptime[1] + " " + temptime[2] + ":00:00").getTime()) {
                return temp + "-" + temptime[0] + "-" + temptime[1] + " " + temptime[2] + ":00:00";
              }
              temp = parseInt(temp) + 1;
              return temp + "-" + temptime[0] + "-" + temptime[1] + " " + temptime[2] + ":00:00";
              break;
            case "月":
              temp = getDateToStrNum(new Date()).toString().substr(0, 6);
              if (isEmptyCheck(row["INSPECTION_DATE"])) {
                return temp.substr(0, 4) + "-" + temp.substr(4, 6) + "-" + temptime[0] + " " + temptime[1] + ":00:00";
              }
              if (new Date(row["INSPECTION_DATE"]).getTime() < new Date(temp.substr(0, 4) + "-" + temp.substr(4, 6) + "-" + temptime[0] + " " + temptime[1] + ":00:00").getTime()) {
                return temp.substr(0, 4) + "-" + temp.substr(4, 6) + "-" + temptime[0] + " " + temptime[1] + ":00:00";
              }
              var d = new Date(temp.substr(0, 4) + "-" + temp.substr(4, 6) + "-" + temptime[0] + " " + temptime[1] + ":00:00")
              d.setMonth(d.getMonth() + 1);
              return getDateToString_ymd(d) + " " + temptime[1] + ":00:00";
              break;
            case "周":
              temp = getDateToStrNum(new Date()).toString().substr(0, 6);
              if (isEmptyCheck(row["INSPECTION_DATE"])) {
                return GetMonday(new Date(), temptime[0]) + " " + temptime[1] + ":00:00";
              }
              if (new Date(row["INSPECTION_DATE"]).getTime() < new Date(GetMonday(new Date(), temptime[0]) + " " + temptime[1] + ":00:00").getTime()) {
                return GetMonday(new Date(), temptime[0]) + " " + temptime[1] + ":00:00";
              }
              var dateTime = new Date(GetMonday(new Date(), temptime[0]));
              dateTime = dateTime.setDate(dateTime.getDate() + 7);
              dateTime = getDateToString_ymd(new Date(dateTime));
              return dateTime + " " + temptime[1] + ":00:00";
              break;
            case "日":
              temp = getDateToString_ymd(new Date()).toString();
              if (isEmptyCheck(row["INSPECTION_DATE"])) {
                return temp + " " + temptime[0] + ":00:00";
              }
              if (new Date(row["INSPECTION_DATE"]).getTime() < new Date(temp + " " + temptime[0] + ":00:00").getTime()) {
                return temp + " " + temptime[0] + ":00:00";
              }
              var dateTime = new Date();
              dateTime = dateTime.setDate(dateTime.getDate() + 1);
              dateTime = getDateToString_ymd(new Date(dateTime));
              return dateTime + " " + temptime[0] + ":00:00";
              break;
            case "时":
              temp = getDateToString(new Date()).toString();
              if (isEmptyCheck(row["INSPECTION_DATE"])) {
                return temp;
              }
              var curTime = new Date(row["INSPECTION_DATE"]);
              var addHour = curTime.setHours(curTime.getHours() + parseInt(temptime[0]));

              dateTime = getDateToString(new Date(curTime));
              return dateTime.toString().substr(0,16);
              break;
          }
        }
      }
      if (row["EXEC_CYCLE"] == "1") {//单次执行
        if (!isEmptyCheck(row["INSPECTION_DATE"])) {
          return row["INSPECTION_DATE"];
        }
        return temptime[0] + "-" + temptime[1] + "-" + temptime[2] + " " + temptime[3] + ":00:00";
      }
    }
    function getTableList() {
      getQualityCheckTaskHomeTableData({
        pageIndex:layoutRef.value.pageIndex,
        pageSize:layoutRef.value.pageSize,
        UNIT:layoutRef.value.form.UNIT,
        NAME:layoutRef.value.form.NAME,
        STATE:layoutRef.value.form.STATE,
      }).then(res => {
        layoutRef.value.getResData(res.msg[0].Rows||[],res.msg[0].TotalRowCount)
      })
        // layoutRef.value.getResData([{name:'卢卡斯'}],50)
    }
    return {
      qualityCheckTaskHomeFilterForm,
      getTableList,
      qualityCheckTaskHomeTableHeader,
      layoutRef,
      getNextTime,
      getStatus,
      changeStatus
    }
  }
}
</script>

<style scoped lang="scss">

</style>
