<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import {ref} from "vue";
import type ISingleTaskSearch from "@/interface/page/stockTask/ISingleTaskSearch.ts";
import StockSingleTaskExecuteMachineFormatter from "@/plugin/formatter/StockSingleTaskExecuteMachineFormatter.ts";
import StockSingleTaskStatusFormatter from "@/plugin/formatter/StockSingleTaskStatusFormatter.ts";
import StockSingleTaskTypeFormatter from "@/plugin/formatter/StockSingleTaskTypeFormatter.ts";
import type ISingleTaskTableData from "@/model/table/ISingleTaskTableData.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StockSingleTaskApi from "@/api/stockSingleTask.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import AddStockSingleTaskComponent from "@/components/page/stockSingleTask/AddStockSingleTaskComponent.vue";
import StockSingleTaskDetailComponent from "@/components/page/stockSingleTask/StockSingleTaskDetailComponent.vue";

const stockSingleTaskExecuteMachineFormatter = new StockSingleTaskExecuteMachineFormatter();
const stockSingleTaskStatusFormatter = new StockSingleTaskStatusFormatter();
const stockSingleTaskTypeFormatter = new StockSingleTaskTypeFormatter();
const trueOrFalseFormatter = new TrueFalseTagStyleFormatter();
const querySearch = ref<ISingleTaskSearch>({}); // 搜索条件
const tableData = ref<ISingleTaskTableData[] | undefined>([]); // 表格数据
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  StockSingleTaskApi.queryBak(querySearch.value, pageSize.value, pageIndex.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<ISingleTaskTableData>>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data.data;
      totalPages.value = response.data.totalCount;
      MessageUtils.successMessage('查询成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  })
};
// 重置查询条件
const resetInput = () => {
  querySearch.value = {};
};
// 下载
const downLoad = () => {
  MessageUtils.confirmMessageBox('确定导出数据？','导出确认').then(() => {
    StockSingleTaskApi.downloadTaskBak(querySearch.value).then(res => {
      const url = window.URL.createObjectURL(new Blob([res.data]));
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', '单项式任务历史任务下载.xlsx');
      document.body.appendChild(link);
      MessageUtils.successMessage('您的下载将尽快开始，请稍后');
      link.click();
      document.body.removeChild(link);
    }).catch(() => {});
  }).catch(() => {});
};
// 分页变化时
const paginationChange = () => {
  query();
};

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>单项式设备任务查询管理【历史任务】</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="querySearch" label-position="top" label-width="150px" inline>
          <el-form-item label="任务号：" style="width: 250px;">
            <el-input v-model="querySearch.taskId" placeholder="请输入任务号" clearable></el-input>
          </el-form-item>
          <el-form-item label="任务组：" style="width: 250px;">
            <el-input v-model="querySearch.taskGroup" placeholder="请输入任务组" clearable></el-input>
          </el-form-item>
          <el-form-item label="plc任务号：" style="width: 250px;">
            <el-input v-model="querySearch.plcTaskId" placeholder="请输入plc任务号" clearable></el-input>
          </el-form-item>
          <el-form-item label="上游系统任务号：" style="width: 250px;">
            <el-input v-model="querySearch.upperTaskId" placeholder="请输入上游系统任务号" clearable></el-input>
          </el-form-item>
          <el-form-item label="执行设备：" style="width: 250px;">
            <el-select v-model="querySearch.executeMachine" placeholder="请输入执行设备" clearable>
              <el-option v-for="item in stockSingleTaskExecuteMachineFormatter.stockSingleTaskExecuteMachineTagStyle" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="任务类型：" style="width: 250px;">
            <el-select v-model="querySearch.taskType" placeholder="请输入任务类型" clearable>
              <el-option v-for="item in stockSingleTaskTypeFormatter.taskTypeTagStyle" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="载具号：" style="width: 250px;">
            <el-input v-model="querySearch.vehicleNo" placeholder="请输入载具号" clearable></el-input>
          </el-form-item>
          <el-form-item label="起点：" style="width: 250px;">
            <el-input v-model="querySearch.origin" placeholder="请输入起点" clearable></el-input>
          </el-form-item>
          <el-form-item label="终点：" style="width: 250px;">
            <el-input v-model="querySearch.destination" placeholder="请输入终点" clearable></el-input>
          </el-form-item>
          <el-form-item label="任务状态：" style="width: 250px;">
            <el-select v-model="querySearch.taskStatus" placeholder="请输入任务状态" clearable>
              <el-option v-for="item in stockSingleTaskStatusFormatter.taskStatusTagStyle" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="任务创建时间范围：" style="width: 250px;">
            <el-date-picker
                v-model="querySearch.createTimeRange"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item label="任务结束时间范围：" style="width: 250px;">
            <el-date-picker
                v-model="querySearch.endTimeRange"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
            />
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <el-button type="primary" @click="query">查询/刷新</el-button>
          <el-button type="warning" @click="resetInput">重置查询参数</el-button>
          <el-button type="info" @click="downLoad">导出数据</el-button>
        </el-button-group>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 470px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="taskId" label="任务号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskGroup" label="任务组" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="plcTaskId" label="plc任务号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="upperTaskId" label="上游任务号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="executeMachine" label="执行设备" width="120" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="stockSingleTaskExecuteMachineFormatter.stockSingleTaskExecuteMachineFormatter(scope.row.executeMachine).type">
              {{stockSingleTaskExecuteMachineFormatter.stockSingleTaskExecuteMachineFormatter(scope.row.executeMachine).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskType" label="任务类型" width="120" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="stockSingleTaskTypeFormatter.stockSingleTaskTypeFormatter(scope.row.taskType).type">
              {{stockSingleTaskTypeFormatter.stockSingleTaskTypeFormatter(scope.row.taskType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="vehicleNo" label="载具编号" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskStatus" label="任务状态" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" effect="plain" round :type="stockSingleTaskStatusFormatter.stockSingleTaskStatusFormatter(scope.row.taskStatus).type">
              {{stockSingleTaskStatusFormatter.stockSingleTaskStatusFormatter(scope.row.taskStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="origin" label="任务起点" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="destination" label="任务目的地" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="composeDestination" label="最终目的地" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="priority" label="优先级" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="vehicleSize" label="载具尺寸" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="weight" label="载具重量" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="canCancel" label="是否可取消" width="100" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="trueOrFalseFormatter.yesOrNoTagStyleFormatter(scope.row.canCancel).type">
              {{trueOrFalseFormatter.yesOrNoTagStyleFormatter(scope.row.canCancel).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="updateTime" label="上次更新时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="startTime" label="任务开始时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="completeTime" label="任务完成时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="endTime" label="任务结束时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="taskSource" label="任务来源" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="createPerson" label="创建人" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskMsg" label="任务信息" width="250" align="center" show-overflow-tooltip />
      </el-table>
      <el-pagination style="margin: 10px"
                     background
                     v-model:current-page="pageIndex"
                     v-model:page-size="pageSize"
                     :page-sizes="[10, 100, 200, 300]"
                     size="small"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalPages"
                     @size-change="paginationChange"
                     @current-change="paginationChange"
      />
    </el-row>
  </div>
</template>

<style scoped>
.searchCard {
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.dataTable {
  margin: 10px 10px 0 auto;
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.searchForm {
  margin: 5px 10px 0 10px;
  width: 100%;
}
.searchForm :deep(.el-form-item__label){
  font-size: 13px;
}
</style>