<script setup lang="ts">

import SystemConfig from "@/constant/SystemConfig.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import {userStore} from "@/stores/user.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import {onMounted, ref} from "vue";
import type {IStockScanSearch} from "@/interface/page/scan/IStockScanSearch.ts";
import {StockScanTypeFormatter} from "@/plugin/formatter/StockScanTypeFormatter.ts";
import type {IAppStockScan} from "@/model/table/IAppStockScan.ts";
import StockScanApi from "@/api/stockScan.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 AddStockScan from "@/components/page/stockScan/AddStockScan.vue";
import StockScanDetail from "@/components/page/stockScan/StockScanDetail.vue";

const userStoreInstance = userStore();
const trueFalseForMatter = new TrueFalseTagStyleFormatter(); // 是否格式化
const stockScanTypeFormatter = new StockScanTypeFormatter();
const pageSize = ref<number>(50); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const scanSearch = ref<IStockScanSearch>({}); // 搜索条件
const tableData = ref<IAppStockScan[] | undefined>([]); // 表格数据
const showAdd = ref<boolean>(false); // 是否展示新增窗口
const showEdit = ref<boolean>(false);
const editFormData = ref<IAppStockScan>({});

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

// 查询数据
const query = () => {
  const loadingInstance = MessageUtils.loading("查询中...");
  StockScanApi.query(scanSearch.value, pageSize.value, pageIndex.value).then(res => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppStockScan>>;
    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 = () => {
  scanSearch.value = {};
};
// 编辑
const handleEdit = (index: number, row: any) => {
  editFormData.value = {
    scanId: row.scanId,
    scanName: row.scanName,
    scanStatus: row.scanStatus,
    scanType: row.scanType,
    scanMethod: row.scanMethod,
    param1: row.param1,
    param2: row.param2,
    param3: row.param3,
    plcId: row.plcId,
    readStatusAddress: row.readStatusAddress,
    writeTaskAddress: row.writeTaskAddress,
    ledNo: row.ledNo,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark,
  };
  showEdit.value = true;
};
// 添加点位
const addScan = () => {
  showAdd.value = true;
};


// 分页数据发生变化时
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="scanSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="扫码点位：" style="width: 250px;">
            <el-input v-model="scanSearch.scanId" placeholder="要查询的输送线点位" clearable></el-input>
          </el-form-item>
          <el-form-item label="扫码名称：" style="width: 250px;">
            <el-input v-model="scanSearch.scanName" placeholder="要查询的业务点位ID" clearable></el-input>
          </el-form-item>
          <el-form-item label="扫码类型：" style="width: 250px;">
            <el-select v-model="scanSearch.scanType" style="width: 250px" clearable>
              <el-option v-for="item in stockScanTypeFormatter.scanType" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="扫码状态：" style="width: 250px;">
            <el-select v-model="scanSearch.scanStatus" style="width: 250px" clearable>
              <el-option v-for="item in trueFalseForMatter.canUseTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="扫码执行方法：" style="width: 250px;">
            <el-input v-model="scanSearch.scanMethod" placeholder="扫码执行方法" clearable></el-input>
          </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="addScan">添加扫码</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="scanId" label="扫码点位" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="scanName" label="扫码名称" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="scanStatus" label="扫码状态" width="150" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="trueFalseForMatter.canUseTagStyleFormatter(scope.row.scanStatus).type">
              {{trueFalseForMatter.canUseTagStyleFormatter(scope.row.scanStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="scanType" label="扫码类型" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="stockScanTypeFormatter.stockScanTypeFormatter(scope.row.scanType).type">
              {{stockScanTypeFormatter.stockScanTypeFormatter(scope.row.scanType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="scanMethod" label="扫码方法" width="200" align="center" show-overflow-tooltip/>
        <el-table-column prop="param1" label="参数1" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="param2" label="参数2" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="param3" label="参数3" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="plcId" label="管辖的PLC" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="readStatusAddress" label="读取状态地址" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="writeTaskAddress" label="写入任务地址" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="ledNo" label="LED屏编号" 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="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>
  </div>
<!--  添加点位-->
<AddStockScan v-model="showAdd" @reLoadingTableData="query"/>
<!--  编辑点位-->
  <StockScanDetail v-model="showEdit" @reLoadingTableData="query" :form-data="editFormData"/>
</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>