<script setup lang="ts">

import type IStackerLocationSearch from "@/interface/page/stackerLocation/IStackerLocationSearch.ts";
import {onMounted, ref} from "vue";
import SystemConfig from "@/constant/SystemConfig.ts";
import {userStore} from "@/stores/user.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type IStackerLocationTableData from "@/model/table/IStackerLocationTableData.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StackerLocationApi from "@/api/stackerLocation.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 StackerLocationStatusFormatter from "@/plugin/formatter/StackerLocationStatusFormatter.ts";
import AddStackerLocationComponent from "@/components/page/stackerLocation/AddStackerLocationComponent.vue";
import StackerLocationDetailComponent from "@/components/page/stackerLocation/StackerLocationDetailComponent.vue";
import StackerLocationMap from "@/components/page/stackerLocation/StackerLocationMap.vue";

const locationSearch = ref<IStackerLocationSearch>({}); // 查询参数
const userStoreInstance = userStore(); //
const tableData = ref<IStackerLocationTableData[] | undefined>([]); // 表格数据
const pageSize = ref<number>(50); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const stackerLocationStatusFormatter = new StackerLocationStatusFormatter();
const showAddStackerLocationDialog = ref<boolean>(false);
const showStackerLocationDetailDialog = ref<boolean>(false);
const detailDialogData = ref<IStackerLocationTableData>({}); // 传到详情弹窗的数据
const showStackerLocationMapDialog = ref<boolean>(false);

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

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  StackerLocationApi.queryStackerLocation(locationSearch.value, pageSize.value, pageIndex.value).then(res => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IStackerLocationTableData>>;
    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 = () => {
  locationSearch.value = {};
};
// 添加
const addLocations = () => {
  showAddStackerLocationDialog.value = true;
};
// 导出数据
const downLoadData = () => {
  MessageUtils.confirmMessageBox('确定导出数据？','导出确认').then(() => {
    StackerLocationApi.downloadStackerLocation(locationSearch.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);
      link.click();
      MessageUtils.successMessage('您的下载将尽快开始，请稍后');
      document.body.removeChild(link);
    }).catch(() => {})
  }).catch(() => {})
}
// 查看按钮
const handleEdit = (index: number, row: any) => {
  detailDialogData.value = {
    locationId: row.locationId,
    locationStatus: row.locationStatus,
    businessLocation: row.businessLocation,
    locationType: row.locationType,
    laneId: row.laneId,
    machineId: row.machineId,
    lRow: row.lRow,
    lLine: row.lLine,
    lLayer: row.lLayer,
    lDepth: row.lDepth,
    locationTag: row.locationTag,
    vehicleNo: row.vehicleNo,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark
  };
  showStackerLocationDetailDialog.value = true;
};
// 分页数据发生变化时
const paginationChange = () => {
  query();
};
// 点位图
const handleMap = () => {
  showStackerLocationMapDialog.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="locationSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="货位ID：" style="width: 250px;">
            <el-input v-model="locationSearch.locationId" clearable></el-input>
          </el-form-item>
          <el-form-item label="业务货位：" style="width: 250px;">
            <el-input v-model="locationSearch.businessLocation" clearable></el-input>
          </el-form-item>
          <el-form-item label="载具号：" style="width: 250px;">
            <el-input v-model="locationSearch.vehicleNo" clearable></el-input>
          </el-form-item>
          <el-form-item label="货位状态：" style="width: 250px;">
            <el-select v-model="locationSearch.locationStatus" style="width: 250px" clearable>
              <el-option v-for="item in stackerLocationStatusFormatter.stackerLocationStatusTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </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 v-if="userStoreInstance.getUserInfo().userId === SystemConfig.DEVELOPER_ID" type="success" @click="addLocations">添加库位</el-button>
          <el-button type="info" @click="downLoadData">导出数据</el-button>
          <el-button type="primary" @click="handleMap">点位图</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 - 320px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="locationId" label="货位号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="locationStatus" label="货位状态" width="150" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="stackerLocationStatusFormatter.stackerLocationStatusTagStyleFormatter(scope.row.locationStatus).type">
              {{stackerLocationStatusFormatter.stackerLocationStatusTagStyleFormatter(scope.row.locationStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="businessLocation" label="业务库位号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="locationType" label="货位类型" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="laneId" label="巷道号" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="machineId" label="设备号" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="lRow" label="排" width="80" align="center" show-overflow-tooltip />
        <el-table-column prop="lLine" label="列" width="80" align="center" show-overflow-tooltip/>
        <el-table-column prop="lLayer" label="层" width="80" align="center" show-overflow-tooltip />
        <el-table-column prop="lDepth" label="深" width="80" align="center" show-overflow-tooltip />
        <el-table-column prop="locationTag" label="货位标签" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="vehicleNo" label="载具号" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="updateTime" label="上次更新时间" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" 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="[50, 100, 200, 300]"
                     size="small"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalPages"
                     @size-change="paginationChange"
                     @current-change="paginationChange"
      />
    </el-row>
<!--    添加货位对话框-->
    <AddStackerLocationComponent v-model="showAddStackerLocationDialog" @reLoadingTableData="query"/>
<!--货位信息详情对话框-->
    <StackerLocationDetailComponent v-model="showStackerLocationDetailDialog" @reLoadingTableData="query" :form-data="detailDialogData"/>
<!--    点位图详情-->
    <StackerLocationMap v-model="showStackerLocationMapDialog" />
  </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>