<script setup lang="ts">

import {Lightning} from "@element-plus/icons-vue";
import type {ISiemensDbSearch} from "@/interface/page/siemensDbManage/ISiemensDbSearch.ts";
import {ref} from "vue";
import type {IAppBaseDb} from "@/model/table/IAppBaseDb.ts";
import {useI18n} from "vue-i18n";
import MessageUtils from "@/utils/MessageUtils.ts";
import SiemensDbApi from "@/api/siemensDb.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import AddSiemensDb from "@/components/page/siemensDb/AddSiemensDb.vue";
import EditSiemensDb from "@/components/page/siemensDb/EditSiemensDb.vue";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const { t } = useI18n();
const searchParams = ref<ISiemensDbSearch>({ plcId: 0 }); // 查询参数
const tableData = ref<IAppBaseDb[]>([]); // 表格数据
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const showAddDialog = ref(false); // 添加记录的弹窗
const showEditDialog = ref(false); // 修改记录的弹窗
const editDialogData = ref<IAppBaseDb>({}); // 修改记录的弹窗数据

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading("正在查询");
  SiemensDbApi.getAppBaseDb(pageIndex.value, pageSize.value, searchParams.value).then((result) => {
    const responseString = JSON.stringify(result.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppBaseDb>>;
    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 paginationChange = () => {
  query();
};
// 重置查询参数
const resetInput = () => {
  searchParams.value = { plcId: 0 };
};
// 添加记录
const addRecord = () => {
  showAddDialog.value = true;
};
// 查看
const handleEdit = (row: any) => {
  editDialogData.value = {
    dbName: row.dbName,
    plcId: row.plcId,
    dbAddress: row.dbAddress,
    remark: row.remark
  };
  showEditDialog.value = true;
};
// 重新加载DB
const reloadDb = () => {
  MessageUtils.confirmMessageBox("确定重新加载DB数据?", "提示", "确认", "取消").then(() => {
    const loadingInstance = MessageUtils.loading("正在重新加载DB数据");
    SiemensDbApi.reloadDbAddress().then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("重新加载成功");
        return;
      }
      MessageUtils.warningMessageBox(response.msg);
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(() => {})
}

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{ t('tabPage.siemensDbManage.title')}}</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="t('tabPage.siemensDbManage.dbName') + '：'" style="width: 250px;">
            <el-input v-model="searchParams.dbName" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.siemensDbManage.dbAddress') + '：'" style="width: 250px;">
            <el-input v-model="searchParams.dbAddress" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.siemensDbManage.plcId') + '：'" style="width: 250px;">
            <el-input-number v-model="searchParams.plcId" :min="0" :max="30000"/>
          </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="addRecord"><el-icon><Lightning /></el-icon>{{t('baseButton.addRecord')}}</el-button>
          <el-button type="info" @click="reloadDb">{{t('tabPage.siemensDbManage.reloadDb')}}</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 type="index" label="序号" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="dbName" :label="t('tabPage.siemensDbManage.dbName')" min-width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="plcId" :label="t('tabPage.siemensDbManage.plcId')" align="center" width="150" show-overflow-tooltip/>
        <el-table-column prop="dbAddress" :label="t('tabPage.siemensDbManage.dbAddress')" align="center" min-width="150" show-overflow-tooltip/>
        <el-table-column prop="remark" :label="t('baseColName.remark')" align="center" min-width="120" show-overflow-tooltip/>
        <el-table-column :label="t('baseColName.action')" width="120" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.row)">{{t('baseButton.detail')}}</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>
<!--  添加西门子基础DB表-->
  <AddSiemensDb v-model="showAddDialog" @reLoadingTableData="query"/>
  <!--  修改西门子基础DB表-->
  <EditSiemensDb v-model="showEditDialog" :form-data="editDialogData" @reLoadingTableData="query"/>
</template>

<style scoped>

</style>