<script setup lang="ts">

import {Lightning} from "@element-plus/icons-vue";
import {onMounted, ref} from "vue";
import type {IStackerInfoTableData} from "@/model/table/IStackerInfoTableData.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StackerApi from "@/api/stacker.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import AddStackerComponent from "@/components/page/stacker/AddStackerComponent.vue";
import StackerDetailComponent from "@/components/page/stacker/StackerDetailComponent.vue";
import StackerStandComponent from "@/components/page/stacker/StackerStandComponent.vue";
import {useI18n} from "vue-i18n";
import StackerRunningInfo from "@/components/page/stacker/StackerRunningInfo.vue";

const { t } = useI18n();
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter(); // 是否枚举格式化
const stakerNameSearch = ref<string>(""); // 堆垛机名称
const tableData = ref<IStackerInfoTableData[]>([]); // 表格数据
const showAddStackerDialog = ref<boolean>(false); // 是否展示添加弹窗
const showDetailDialog = ref<boolean>(false); // 是否展示查看细节弹窗
const detailDialogData = ref<IStackerInfoTableData>({}); // 发送到查看弹窗内的数据
const showStandDialog = ref<boolean>(false); // 是否展示站台弹窗
const standDialogData = ref<IStackerInfoTableData>({}); // 发送到站台配置抽屉内的数据
const showStackerRunningInfoDialog = ref<boolean>(false); // 是否展示运行信息弹窗

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

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  StackerApi.queryStackerInfo(stakerNameSearch.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IStackerInfoTableData[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data;
      MessageUtils.successMessage('查询成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  })
};
// 重置查询参数
const resetInput = () => {
  stakerNameSearch.value = "";
};
// 添加堆垛机
const addStacker = () => {
  showAddStackerDialog.value = true;
};
// 显示堆垛机运行信息
const showStackerRunningInfo = () => {
  showStackerRunningInfoDialog.value = true;
}
// 表格查看按钮
const handleEdit = (index: number, row: any) => {
  detailDialogData.value = {
    stackerId: row.stackerId,
    plcId: row.plcId,
    stackerName: row.stackerName,
    stackerStatus: row.stackerStatus,
    forkStatus: row.forkStatus,
    allowIn: row.allowIn,
    allowOut: row.allowOut,
    allowMove: row.allowMove,
    actionBeat: row.actionBeat,
    readStatusAddress: row.readStatusAddress,
    writeTaskAddress: row.writeTaskAddress,
    taskConfirmAddress: row.taskConfirmAddress,
    taskStatusAddress: row.taskStatusAddress,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark,
  };
  showDetailDialog.value = true;
};
// 表格站台配置按钮
const standEdit = (index: number, row: any) => {
  standDialogData.value = {
    stackerId: row.stackerId,
    plcId: row.plcId,
    stackerName: row.stackerName,
    stackerStatus: row.stackerStatus,
    forkStatus: row.forkStatus,
    allowIn: row.allowIn,
    allowOut: row.allowOut,
    allowMove: row.allowMove,
    actionBeat: row.actionBeat,
    readStatusAddress: row.readStatusAddress,
    writeTaskAddress: row.writeTaskAddress,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark,
  };
  showStandDialog.value = true;
}

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{ t('tabPage.stackerManage.title')}}</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" label-position="top" label-width="150px" inline>
          <el-form-item :label="t('tabPage.stackerManage.stackerName') + ':'" style="width: 250px;">
            <el-input v-model="stakerNameSearch" 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">{{ t('baseButton.queryOrReflush') }}</el-button>
          <el-button type="warning" @click="resetInput">{{ t('baseButton.resetInput') }}</el-button>
          <el-button type="success" @click="addStacker"><el-icon><Lightning /></el-icon>{{ t('tabPage.stackerManage.addStacker') }}</el-button>
          <el-button type="info" @click="showStackerRunningInfo">{{t('tabPage.stackerManage.stackerRunningInfo')}}</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 - 270px)">
        <el-table-column prop="stackerId" label="堆垛机编号" width="100" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="plcId" label="对应的PLC ID" align="center" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="stackerName" label="堆垛机名称" align="center" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="stackerStatus" label="堆垛机状态" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="dark" size="small" round :type="trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.stackerStatus).type">
              {{trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.stackerStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="forkStatus" label="货叉状态" align="center" width="150" show-overflow-tooltip/>
        <el-table-column prop="allowIn" label="是否允许入库" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="plain" size="small" round :type="trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowIn).type">
              {{trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowIn).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="allowOut" label="是否允许出库" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="plain" size="small" round :type="trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowOut).type">
              {{trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowOut).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="allowMove" label="是否允许移库" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="plain" size="small" round :type="trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowMove).type">
              {{trueFalseTagStyleFormatter.yesOrNoTagStyleFormatter(scope.row.allowMove).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="actionBeat" label="动作节拍" align="center" width="250" show-overflow-tooltip/>
        <el-table-column prop="readStatusAddress" label="获取堆垛机状态地址" align="center" width="200" show-overflow-tooltip/>
        <el-table-column prop="writeTaskAddress" label="写入堆垛机任务地址" align="center" width="200" show-overflow-tooltip/>
        <el-table-column prop="taskConfirmAddress" label="写入任务确认地址" align="center" width="200" show-overflow-tooltip/>
        <el-table-column prop="taskStatusAddress" label="过账地址" align="center" width="200" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" align="center" min-width="120" show-overflow-tooltip/>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">{{ t('baseButton.detail') }}</el-button>
              <el-button size="small" type="success" @click="standEdit(scope.$index, scope.row)">{{ t('tabPage.stackerManage.standConfig') }}</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
<!--  添加堆垛机-->
  <AddStackerComponent v-model="showAddStackerDialog" @reLoadingTableData="query"/>
<!--  查看按钮弹窗-->
  <StackerDetailComponent v-model="showDetailDialog" @reLoadingTableData="query" :form-data="detailDialogData"/>
<!--  站台配置-->
  <StackerStandComponent v-model="showStandDialog" @reLoadingTableData="query" :stackerData="standDialogData"/>
<!--  堆垛机运行信息-->
  <StackerRunningInfo v-model="showStackerRunningInfoDialog"/>
</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>