<script setup lang="ts">

import {Lightning} from "@element-plus/icons-vue";
import {onMounted, ref} from "vue";
import type {IPlcSearch} from "@/interface/page/plc/IPlcSearch.ts";
import type {IPlcTableVo} from "@/model/table/IPlcTableVo.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import {PlcApi} from "@/api/plc.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import PlcTypeFormatter from "@/plugin/formatter/PlcTypeFormatter.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import AddPlcComponent from "@/components/page/plc/AddPlcComponent.vue";
import PlcDetailComponent from "@/components/page/plc/PlcDetailComponent.vue";

const plcSearch = ref<IPlcSearch>({ plcType: 1}); // 查询的数据
const tableData = ref<IPlcTableVo[]>([]); // 表格数据
const plcTypeFormatter = new PlcTypeFormatter(); // plc型号格式化
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter(); // 是否枚举格式化
const showAddDialog = ref<boolean>(false); // 是否展示添加的弹窗
const showDetailDialog = ref<boolean>(false); // 是否展示查看弹窗
const detailDialogData = ref<IPlcTableVo>({}); // 查看弹窗的数据

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

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  PlcApi.queryPlc(plcSearch.value).then((result) => {
    const responseString = JSON.stringify(result.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IPlcTableVo[]>;
    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 = () => {
  plcSearch.value = { plcType: 1};
};
// 添加 PLC
const addPlc = () => {
  showAddDialog.value = true;
};
// 行查看按钮
const handleEdit = (index: number, row: any) => {
  detailDialogData.value = {
    plcId: row.plcId,
    plcName: row.plcName,
    plcStatus: row.plcStatus,
    plcType: row.plcType,
    plcVersion: row.plcVersion,
    ip: row.ip,
    port: row.port,
    slot: row.slot,
    rack: row.rack,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark,
  };
  showDetailDialog.value = true;
}

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>PLC 管理</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="plcSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="PLC 名称：" style="width: 250px;">
            <el-input v-model="plcSearch.plcName" placeholder="请输入 PLC 名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="PLC 型号：" style="width: 250px;">
            <el-select v-model="plcSearch.plcType" placeholder="请选择 PLC 型号" style="width: 250px" clearable>
              <el-option :key="1" label="西门子" :value="1"/>
            </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="addPlc"><el-icon><Lightning /></el-icon>添加PLC</el-button>
<!--          <el-button type="info" @click="">重载PLC配置</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="plcId" label="PLC ID" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="plcName" label="PLC 名称" align="center" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="plcType" label="PLC 型号" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" :type="plcTypeFormatter.plcTypeFormatter(scope.row.plcType).type">
              {{plcTypeFormatter.plcTypeFormatter(scope.row.plcType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="plcVersion" label="PLC 版本" align="center" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="plcStatus" label="PLC 状态" width="120" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="dark" size="small" round :type="trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.plcStatus).type">
              {{trueFalseTagStyleFormatter.canUseTagStyleFormatter(scope.row.plcStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="IP 地址" align="center" width="150" show-overflow-tooltip/>
        <el-table-column prop="port" label="端口号" align="center" width="120" show-overflow-tooltip/>
        <el-table-column prop="slot" label="插槽" align="center" width="120" show-overflow-tooltip/>
        <el-table-column prop="rack" label="机架" align="center" width="120" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" align="center" min-width="120" show-overflow-tooltip/>
        <el-table-column label="操作" width="80" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
<!--  添加plc弹窗-->
  <AddPlcComponent v-model="showAddDialog" @reLoadingTableData="query"/>
<!--  查看弹窗-->
  <PlcDetailComponent v-model="showDetailDialog" @reLoadingTableData="query" :form-data="detailDialogData"/>
</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>