<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type {IConveyTaskQuery} from "@/interface/page/convey/IConveyTaskQuery.ts";
import {ref} from "vue";
import type {IAppConveyTask} from "@/model/table/IAppConveyTask.ts";
import ConveyTaskStatusFormatter from "@/plugin/formatter/ConveyTaskStatusFormatter.ts";
import ConveyTaskTypeFormatter from "@/plugin/formatter/ConveyTaskTypeFormatter.ts";
import ConveyTaskApi from "@/api/conveyTask.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import AddConveyTask from "@/components/page/conveyTask/AddConveyTask.vue";
import ConveyTaskDetail from "@/components/page/conveyTask/ConveyTaskDetail.vue";

const querySearch = ref<IConveyTaskQuery>({}); // 查询参数
const tableData = ref<IAppConveyTask[] | undefined>([]); // 表格数据
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(0); // 总数据数
const conveyTaskTypeFormatter = new ConveyTaskTypeFormatter();
const conveyTaskStatusFormatter = new ConveyTaskStatusFormatter();
const showAddTask = ref<boolean>(false); // 显示新增任务弹窗
const detailData = ref<IAppConveyTask>({});
const showDetail = ref<boolean>(false);

// 获取数据
const query = () => {
  const loadingInstance = MessageUtils.loading("查询中...");
  ConveyTaskApi.query(querySearch.value, pageSize.value, pageIndex.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppConveyTask>>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data.data;
      totalPages.value = response.data.totalCount;
      MessageUtils.successMessage('查询成功');
      return;
    }
    MessageUtils.errMessage('查询失败，异常信息：' + response.msg);
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  });
};
// 重置查询参数
const resetInput = () => {
  querySearch.value = {};
};
// 新增数据
const add = () => {
  showAddTask.value = true;
};
// 分页页码改变
const paginationChange = () => {
  query();
};
// 修改数据
const handleEdit = (index: number, row: IAppConveyTask) => {
  detailData.value = {
    taskId: row.taskId,
    taskGroup: row.taskGroup,
    taskType: row.taskType,
    vehicleNo: row.vehicleNo,
    orderId: row.orderId,
    location: row.location,
    size: row.size,
    weight: row.weight,
    length: row.length,
    width: row.width,
    height: row.height,
    taskStatus: row.taskStatus,
    taskSource: row.taskSource,
    createPerson: row.createPerson,
    createTime: row.createTime,
    updateTime: row.updateTime,
    plcId: row.plcId,
    arriveLocation: row.arriveLocation,
    arriveTime: row.arriveTime,
    completeTime: row.completeTime,
    remark: row.remark
  };
  showDetail.value = true;
};


</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.vehicleNo" placeholder="请输入载具号" clearable></el-input>
          </el-form-item>
          <el-form-item label="任务点位：" style="width: 250px;">
            <el-input v-model="querySearch.location" placeholder="请输入任务点位" clearable></el-input>
          </el-form-item>
          <el-form-item label="到达点位：" style="width: 250px;">
            <el-input v-model="querySearch.arriveLocation" placeholder="请输入到达点位" clearable></el-input>
          </el-form-item>
          <el-form-item label="PLC任务号：" style="width: 250px;">
            <el-input v-model="querySearch.plcTaskId" 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 conveyTaskTypeFormatter.taskType" :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.taskStatus" placeholder="请输入任务状态" clearable>
              <el-option v-for="item in conveyTaskStatusFormatter.taskStatus" :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.arriveLocation" 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-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="taskType" label="任务类型" width="120" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="conveyTaskTypeFormatter.conveyTaskTypeFormatter(scope.row.taskType).type">
              {{conveyTaskTypeFormatter.conveyTaskTypeFormatter(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="orderId" label="订单号" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="location" 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="conveyTaskStatusFormatter.conveyTaskStatusFormatter(scope.row.taskStatus).type">
              {{conveyTaskStatusFormatter.conveyTaskStatusFormatter(scope.row.taskStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <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="plcId" label="plc任务号" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="arriveLocation" label="到达位置" width="120" align="center" show-overflow-tooltip/>
        <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="arriveTime" 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="size" 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="length" label="长" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="width" label="宽" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="height" label="高" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="remark" 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>
<!--  添加任务-->
  <AddConveyTask v-model="showAddTask" @reLoadingTableData="query"/>
  <!--  任务详情-->
  <ConveyTaskDetail v-model="showDetail" @reLoadingTableData="query" :form-data="detailData"/>
</template>

<style scoped>

</style>