<script setup lang="ts">

import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type {IEtagControllerQuery} from "@/interface/page/etagController/IEtagControllerQuery.ts";
import {ref} from "vue";
import type {IAppEtagController} from "@/model/table/IAppEtagController.ts";
import {useI18n} from "vue-i18n";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import AddEtagController from "@/components/page/etagController/AddEtagController.vue";
import EditEtagController from "@/components/page/etagController/EditEtagController.vue";
import DebugEtagController from "@/components/page/etagController/DebugEtagController.vue";

const { t } = useI18n();
const querySearch = ref<IEtagControllerQuery>({}); // 查询参数
const tableData = ref<IAppEtagController[]>([]); // 表格数据
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const showAddDialog = ref<boolean>(false); // 添加对话框显示状态
const showEditDialog = ref<boolean>(false); // 修改对话框显示状态
const editDialogData = ref<IAppEtagController>({}); // 修改对话框数据
const showDebugDialog = ref<boolean>(false); // 调试对话框显示状态
const debugDialogData = ref<IAppEtagController>({});

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading('请稍后');
  EtagBaseApi.getEtagControllerInfo(pageIndex.value, pageSize.value, querySearch.value).then(res => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppEtagController>>;
    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 = () => {
  querySearch.value = {};
};
// 分页切换
const paginationChange = () => {
  query();
};
// 详情
const edit = (row: IAppEtagController) => {
  editDialogData.value = {
    controllerId: row.controllerId,
    controllerName: row.controllerName,
    controllerType: row.controllerType,
    controllerStatus: row.controllerStatus,
    ip: row.ip,
    port: row.port,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark
  }
  showEditDialog.value = true;
};
// 新增
const add = () => {
  showAddDialog.value = true;
};
// 调试控制器
const debug = (row: IAppEtagController) => {
  debugDialogData.value = {
    controllerId: row.controllerId,
    controllerName: row.controllerName,
    controllerType: row.controllerType,
    controllerStatus: row.controllerStatus,
    ip: row.ip,
    port: row.port,
    createTime: row.createTime,
    updateTime: row.updateTime,
    remark: row.remark
  }
  showDebugDialog.value = true;
}


</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{t('tabPage.etagController.title')}}</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="querySearch" label-position="top" label-width="150px" inline>
          <el-form-item :label="t('tabPage.etagController.controllerName')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.controllerName" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.controllerIp')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.ip" placeholder="" clearable></el-input>
          </el-form-item>
        </el-form>
        <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="success" @click="add">{{t('baseButton.addRecord')}}</el-button>
            <el-button type="warning" @click="resetInput">{{t('baseButton.resetInput')}}</el-button>
          </el-button-group>
        </el-row>
      </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="t('baseColName.id')" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="controllerId" :label="t('tabPage.etagController.controllerId')" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="controllerName" :label="t('tabPage.etagController.controllerName')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="controllerType" :label="t('tabPage.etagController.type')" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="controllerStatus" :label="t('tabPage.etagController.status')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="ip" :label="t('tabPage.etagController.ip')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="port" :label="t('tabPage.etagController.port')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="createTime" :label="t('baseColName.createTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="updateTime" :label="t('baseColName.updateTime')" width="180" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="remark" :label="t('baseColName.remark')" min-width="200" align="center" show-overflow-tooltip />
        <el-table-column :label="t('baseColName.action')" fixed="right" width="120" align="center">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="edit(scope.row)">{{ t('baseButton.edit')}}</el-button>
<!--              <el-button size="small" type="warning" @click="debug(scope.row)">{{ t('baseButton.debug')}}</el-button>-->
            </el-button-group>
          </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>
<!--  添加信息-->
  <AddEtagController v-model="showAddDialog" @reLoadingTableData="query" />
<!--  编辑信息-->
  <EditEtagController v-model="showEditDialog" :formData="editDialogData" @reLoadingTableData="query" />
<!--  调试控制器-->
  <DebugEtagController v-model="showDebugDialog" :formData="debugDialogData" />
</template>

<style scoped>

</style>