<script setup lang="ts">

import {ref} from "vue";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import ConveyStandApi from "@/api/conveyStand.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 type {IConveyCheckStandSearch} from "@/interface/page/convey/IConveyCheckStandSearch.ts";
import type {IAppConveyCheckStand} from "@/model/table/IAppConveyCheckStand.ts";
import AddCheckStand from "@/components/page/conveyStand/AddCheckStand.vue";
import CheckStandDetail from "@/components/page/conveyStand/CheckStandDetail.vue";

const searchParams = ref<IConveyCheckStandSearch>({});
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const tableData = ref<IAppConveyCheckStand[] | undefined>([]);
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(0); // 总数据数
const showAdd = ref<boolean>(false); // 是否展示添加窗口
const showDetail = ref<boolean>(false); // 是否展示详情窗口
const detailData = ref<IAppConveyCheckStand>({});

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading('查询中...');
  ConveyStandApi.queryCheckStand(searchParams.value, pageSize.value, pageIndex.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppConveyCheckStand>>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data.data;
      totalPages.value = response.data.totalCount;
      MessageUtils.successMessage('查询成功');
    } else {
      MessageUtils.warningMessageBox(response.msg);
    }
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  });
};
// 重置查询参数
const resetInput = () => {
  searchParams.value = {};
};
// 添加站台
const addStand = () => {
  showAdd.value = true;
};
// 查看
const handleEdit = (index: number, row: any) => {
  detailData.value = {
    standId: row.standId,
    standName: row.standName,
    plcId: row.plcId,
    areaId: row.areaId,
    standStatus: row.standStatus,
    router: row.router,
    readArriveAddress: row.readArriveAddress,
    writeTaskAddress: row.writeTaskAddress,
    remark: row.remark
  };
  showDetail.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="searchParams" label-position="top" label-width="150px" inline>
          <el-form-item label="站台号：" style="width: 250px;">
            <el-input v-model="searchParams.standId" placeholder="支持模糊查询" clearable></el-input>
          </el-form-item>
          <el-form-item label="区域号：" style="width: 250px;">
            <el-input v-model="searchParams.areaId"  placeholder="支持模糊查询" clearable></el-input>
          </el-form-item>
          <el-form-item label="站台状态：" style="width: 250px;">
            <el-select v-model="searchParams.standStatus"  placeholder="请选择状态" clearable>
              <el-option v-for="item in trueFalseTagStyleFormatter.canUseTagStyle" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </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 type="success" @click="addStand">添加站台</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="standId" label="站台号" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="standName" label="站台名称" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="plcId" label="管辖的PLC" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="areaId" label="区域号" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="standStatus" label="站台状态" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.standStatus).type">
              {{trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.standStatus).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="router" label="路向" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="readArriveAddress" label="读取到达的地址" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="writeTaskAddress" label="写入任务的地址" width="180" align="center" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" min-width="150" align="left" 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>
<!--添加复核站台-->
  <AddCheckStand v-model="showAdd" @reLoadingTableData="query"/>
  <!--复核站台详情-->
  <CheckStandDetail v-model="showDetail" :formData="detailData" @reLoadingTableData="query"/>
</template>

<style scoped>

</style>