<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";
import {menuStore} from "@/stores/menu.ts";
import AppPermission from "@/components/manage/AppPermission.vue";
import {useI18n} from "vue-i18n";

const { t } = useI18n();
const menuStoreInstance = menuStore();
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 = () => {
  if(!menuStoreInstance.checkOperationPermission('stockComposeTask:query')) {
    return;
  }
  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(t('baseTip.success'));
      return;
    }
    MessageUtils.warningMessageBox(response.msg, t('baseTip.warning'));
  }).catch((err) => {
    MessageUtils.errMessageBox(t('baseTip.requestFailed') + ":" + err, t('baseTip.error'));
  }).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(t('customMessage.confirmExport'),t('baseTip.confirmTip')).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(t('customMessage.pleaseWait'));
      link.click();
      document.body.removeChild(link);
    }).catch(() => {})
  }).catch(() => {});
}


</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{t('tabPage.stockComposeTask.title')}}</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="t('tabPage.stockComposeTask.taskId')" style="width: 250px;">
            <el-input v-model="querySearch.taskId" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.taskId')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.taskGroup')" style="width: 250px;">
            <el-input v-model="querySearch.taskGroup" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.taskGroup')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.upperSystemTaskId')" style="width: 250px;">
            <el-input v-model="querySearch.upperTaskId" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.upperSystemTaskId')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.taskType')" style="width: 250px;">
            <el-select v-model="querySearch.taskType" :placeholder="t('baseTip.pleaseSelect', {msg: t('tabPage.stockComposeTask.taskType')})" 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="t('tabPage.stockComposeTask.vehicleNo')" style="width: 250px;">
            <el-input v-model="querySearch.vehicleNo" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.vehicleNo')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.origin')" style="width: 250px;">
            <el-input v-model="querySearch.origin" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.origin')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.destination')" style="width: 250px;">
            <el-input v-model="querySearch.destination" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.destination')})" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.taskStatus')" style="width: 250px;">
            <el-select v-model="querySearch.taskStatus" :placeholder="t('baseTip.pleaseSelect', {msg: t('tabPage.stockComposeTask.taskStatus')})" 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="t('tabPage.stockComposeTask.createTime')" style="width: 250px;">
            <el-date-picker
                v-model="querySearch.createTimeRange"
                type="datetimerange"
                range-separator="-"
                :start-placeholder="t('customMessage.startTime')"
                :end-placeholder="t('customMessage.endTime')"
            />
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.endTime')" style="width: 250px;">
            <el-date-picker
                v-model="querySearch.endTimeRange"
                type="datetimerange"
                range-separator="-"
                :start-placeholder="t('customMessage.startTime')"
                :end-placeholder="t('customMessage.endTime')"
            />
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <app-permission permission="stockComposeTask:query"><el-button type="primary" @click="query">{{t('baseButton.queryOrReflush')}}</el-button></app-permission>
          <el-button type="warning" @click="resetInput">{{t('baseButton.resetInput')}}</el-button>
          <app-permission permission="stockComposeTask:add"><el-button type="success" @click="add">{{t('baseButton.addData')}}</el-button></app-permission>
          <app-permission permission="stockComposeTask:export"><el-button type="info" @click="downLoad">{{t('baseButton.export')}}</el-button></app-permission>
        </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="t('baseColName.id')" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="taskId" :label="t('tabPage.stockComposeTask.taskId')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskGroup" :label="t('tabPage.stockComposeTask.taskGroup')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="upperTaskId" :label="t('tabPage.stockComposeTask.upperSystemTaskId')" width="180" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskType" :label="t('tabPage.stockComposeTask.taskType')" 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="t('tabPage.stockComposeTask.vehicleNo')" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskStatus" :label="t('tabPage.stockComposeTask.taskStatus')" 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="t('tabPage.stockComposeTask.origin')" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="destination" :label="t('tabPage.stockComposeTask.destination')" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="stepStatus" :label="t('tabPage.stockComposeTask.stepStatus')" width="150" 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="t('tabPage.stockComposeTask.priority')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="vehicleSize" :label="t('tabPage.stockComposeTask.vehicleSize')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="weight" :label="t('tabPage.stockComposeTask.weight')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="canCancel" :label="t('tabPage.stockComposeTask.canCancel')" 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="t('tabPage.stockComposeTask.createTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="updateTime" :label="t('tabPage.stockComposeTask.updateTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="startTime" :label="t('tabPage.stockComposeTask.startTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="completeTime" :label="t('tabPage.stockComposeTask.completeTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="endTime" :label="t('tabPage.stockComposeTask.endTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="taskSource" :label="t('tabPage.stockComposeTask.taskSource')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="createPerson" :label="t('tabPage.stockComposeTask.createPerson')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="taskMsg" :label="t('tabPage.stockComposeTask.taskMsg')" width="250" align="center" show-overflow-tooltip />
        <el-table-column :label="t('baseColName.action')" width="80" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">{{t('baseButton.detail')}}</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>