<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import {onMounted, ref} from "vue";
import type {IComposeTaskSearch} from "@/interface/page/stockTask/IComposeTaskSearch.ts";
import StockComposeTaskStatusFormatter from "@/plugin/formatter/StockComposeTaskStatusFormatter.ts"
import type {IComposeTaskTableData} from "@/model/table/IComposeTaskTableData.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StockComposeTaskApi from "@/api/stockComposeTask.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import StockComposeTaskTypeFormatter from "@/plugin/formatter/StockComposeTaskTypeFormatter.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import StockComposeStepStatusFormatter from "@/plugin/formatter/StockComposeStepStatusFormatter.ts";
import AddStockComposeTaskComponent from "@/components/page/stockComposeTask/AddStockComposeTaskComponent.vue";
import StockComposeTaskDetailComponent from "@/components/page/stockComposeTask/StockComposeTaskDetailComponent.vue";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";

const querySearch = ref<IComposeTaskSearch>({}); // 查询条件
const stockComposeTaskStatusFormatter = new StockComposeTaskStatusFormatter();
const stockComposeTaskTypeFormatter = new StockComposeTaskTypeFormatter();
const stockComposeStepStatusFormatter = new StockComposeStepStatusFormatter();
const trueOrFalseFormatter = new TrueFalseTagStyleFormatter();
const tableData = ref<IComposeTaskTableData[] | undefined>([]); // 表格数据
const detailData = ref<IComposeTaskTableData>({}); // 传到详情弹窗的数据
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const showAddTask = ref<boolean>(false); // 是否展示添加任务弹窗
const showDetailTask = ref<boolean>(false); // 是否展示任务详情的弹窗

onMounted(() => {
  query();
});

// 查询数据
const query = () => {
  const loadingInstance = MessageUtils.loading();
  StockComposeTaskApi.query(querySearch.value, pageSize.value, pageIndex.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IComposeTaskTableData>>;
    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((err) => {
    MessageUtils.errMessageBox('请求服务器失败：' + err, '错误');
  }).finally(() => {
    loadingInstance.close();
  })
}
// 重置输入框数据
const resetInput = () => {
  querySearch.value = {};
};
// 添加任务
const add = () => {
  showAddTask.value = true;
}
// 分页数据发生变化时
const paginationChange = () => {
  query();
}
// 点击行详情时
const handleEdit = (index: number, row: any) => {
  detailData.value = {
    taskId: row.taskId,
    taskGroup: row.taskGroup,
    upperTaskId: row.upperTaskId,
    taskType: row.taskType,
    origin: row.origin,
    destination: row.destination,
    taskStatus: row.taskStatus,
    stepStatus: row.stepStatus,
    canCancel: row.canCancel,
    priority: row.priority,
    vehicleNo: row.vehicleNo,
    vehicleSize: row.vehicleSize,
    weight: row.weight,
    createTime: row.createTime,
    updateTime: row.updateTime,
    startTime: row.startTime,
    completeTime: row.completeTime,
    endTime: row.endTime,
    taskSource: row.taskSource,
    createPerson: row.createPerson,
    taskMsg: row.taskMsg,
  }
  showDetailTask.value = true;
};
// 下载数据
const downLoad = () => {
  MessageUtils.confirmMessageBox('确定导出数据？','导出确认').then(() => {
    StockComposeTaskApi.downloadTask(querySearch.value).then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.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(() => {});
}


</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="上游系统任务号：" 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.taskType" placeholder="请输入任务类型" clearable>
              <el-option v-for="item in stockComposeTaskTypeFormatter.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 stockComposeTaskStatusFormatter.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="success" @click="add">新建任务</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 - 400px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="taskId" label="任务ID" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskGroup" label="任务组" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="upperTaskId" label="上游任务号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskType" label="任务类型" width="120" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="stockComposeTaskTypeFormatter.stockComposeTaskTypeFormatter(scope.row.taskType).type">
              {{stockComposeTaskTypeFormatter.stockComposeTaskTypeFormatter(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="stockComposeTaskStatusFormatter.stockComposeTaskStatusFormatter(scope.row.taskStatus).type">
              {{stockComposeTaskStatusFormatter.stockComposeTaskStatusFormatter(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="stepStatus" label="步骤状态" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" effect="plain" round :type="stockComposeStepStatusFormatter.stockComposeStepStatusFormatter(scope.row.stepStatus).type">
              {{stockComposeStepStatusFormatter.stockComposeStepStatusFormatter(scope.row.stepStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <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-column label="操作" width="80" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </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>
<!--  添加任务-->
  <AddStockComposeTaskComponent v-model="showAddTask" @reLoadingTableData="query"/>
<!--  任务详情-->
  <StockComposeTaskDetailComponent v-model="showDetailTask" @reLoadingTableData="query" :form-data="detailData"/>
</template>

<style scoped>

</style>