<script setup lang="ts">

import {ref} from "vue";
import {useI18n} from "vue-i18n";
import type {IEtagTagQuery} from "@/interface/page/etagTag/IEtagTagQuery.ts";
import type {IAppEtagTag} from "@/model/table/IAppEtagTag.ts";
import EtagTagTypeFormatter from "@/plugin/formatter/EtagTagTypeFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.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 AddEtagTag from "@/components/page/etagTag/AddEtagTag.vue";
import EditEtagTag from "@/components/page/etagTag/EditEtagTag.vue";
import EtagTagBusinessTypeFormatter from "@/plugin/formatter/EtagTagBusinessTypeFormatter.ts";

const { t } = useI18n();
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const querySearch = ref<IEtagTagQuery>({}); // 查询参数
const tableData = ref<IAppEtagTag[]>([]); // 表格数据
const etagTagTypeFormatter = new EtagTagTypeFormatter();
const tagBusinessTypeFormatter = new EtagTagBusinessTypeFormatter(); // 标签业务类型格式化
const showAddDialog = ref<boolean>(false); // 添加标签对话框显示状态
const showEditDialog = ref<boolean>(false); // 修改标签对话框显示状态
const editDialogData = ref<IAppEtagTag>({}); // 修改标签对话框数据

// 查询电子标签小标签数据
const query = () => {
  const loadingInstance = MessageUtils.loading("正在查询");
  EtagBaseApi.getEtagTagInfo(pageIndex.value, pageSize.value, querySearch.value).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<PageDataResponse<IAppEtagTag>>;
    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 = () => {
  querySearch.value = {};
};
const edit = (row: any) => {
  editDialogData.value = {
    tagName: row.tagName,
    controllerId: row.controllerId,
    tagId: row.tagId,
    tagType: row.tagType,
    businessType: row.businessType,
    tunnelNo: row.tunnelNo,
    areaId: row.areaId,
    remark: row.remark
  };
  showEditDialog.value = true;
};
const add = () => {
  showAddDialog.value = true;
};

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{t('tabPage.etagTag.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.etagTag.tagName')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.tagName" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tagId')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.tagId" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tagType')+ ':'" style="width: 250px;">
            <el-select v-model="querySearch.tagType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagTagTypeFormatter.etagTagType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.businessType') + '：'">
            <el-select v-model="querySearch.businessType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in tagBusinessTypeFormatter.tagBusinessType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.controllerId')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.controllerId" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tunnelNo')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.tunnelNo" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.areaId')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.areaId" 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 - 400px)">
        <el-table-column type="index" width="80" :label="t('baseColName.id')" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="tagName" :label="t('tabPage.etagTag.tagName')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="controllerId" :label="t('tabPage.etagTag.controllerId')" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="tagId" :label="t('tabPage.etagTag.tagId')" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="tagType" :label="t('tabPage.etagTag.tagType')" width="240" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag :type="etagTagTypeFormatter.etagTagTypeFormatter(scope.row.tagType).type">{{etagTagTypeFormatter.etagTagTypeFormatter(scope.row.tagType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="businessType" :label="t('tabPage.etagTag.businessType')" width="240" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag :type="tagBusinessTypeFormatter.tagBusinessTypeFormatter(scope.row.businessType).type">
              {{tagBusinessTypeFormatter.tagBusinessTypeFormatter(scope.row.businessType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="tunnelNo" :label="t('tabPage.etagTag.tunnelNo')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="areaId" :label="t('tabPage.etagTag.areaId')" width="150" align="center" 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-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>
<!--  添加小标签-->
  <AddEtagTag v-model="showAddDialog" @reLoadingTableData="query" />
  <!--  修改小标签-->
  <EditEtagTag v-model="showEditDialog" :form-data="editDialogData" @reLoadingTableData="query" />

</template>

<style scoped>

</style>