<script setup lang="ts">
import {useI18n} from "vue-i18n";
import {ref} from "vue";
import EtagLocationTypeFormatter from "@/plugin/formatter/EtagLocationTypeFormatter.ts";
import type {IEtagLocationQuery} from "@/interface/page/etagLocation/IEtagLocationQuery.ts";
import type {IAppEtagLocation} from "@/model/table/IAppEtagLocation.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import AddEtagLocation from "@/components/page/etagLocation/AddEtagLocation.vue";
import EditEtagLocation from "@/components/page/etagLocation/EditEtagLocation.vue";

const { t } = useI18n();
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(1); // 总数据数
const etagLocationTypeFormatter = new EtagLocationTypeFormatter();
const querySearch = ref<IEtagLocationQuery>({}); // 查询参数
const tableData = ref<IAppEtagLocation[]>([]); // 表格数据
const showAddDialog = ref<boolean>(false); // 添加对话框显示
const showEditDialog = ref<boolean>(false); // 修改对话框显示
const editData = ref<IAppEtagLocation>({}); // 修改数据

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading();
  EtagBaseApi.getEtagLocationInfo(pageIndex.value, pageSize.value, querySearch.value).then(res => {
    const responseData = res.data;
    const response = JSON.parse(JSON.stringify(responseData)) as AppServeDataResponse<PageDataResponse<IAppEtagLocation>>;
    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 add = () => {
  showAddDialog.value = true;
};

const paginationChange = () => {
  query();
};

const edit = (row: any) => {
  editData.value = {
    recordId: row.recordId,
    location: row.location,
    tagName: row.tagName,
    locationType: row.locationType,
    stand: row.stand,
    remark: row.remark
  };
  showEditDialog.value = true;
};

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{t('tabPage.etagLocation.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.etagLocation.tagName')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.tagName" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.location')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.location" placeholder="" clearable></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.locationType')+ ':'" style="width: 250px;">
            <el-select v-model="querySearch.locationType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagLocationTypeFormatter.etagLocationType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.stand')+ ':'" style="width: 250px;">
            <el-input v-model="querySearch.stand" 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="recordId" :label="t('tabPage.etagLocation.recordId')" width="200" align="center" show-overflow-tooltip/>
        <el-table-column prop="location" :label="t('tabPage.etagLocation.location')" width="120" align="center" show-overflow-tooltip/>
        <el-table-column prop="tagName" :label="t('tabPage.etagLocation.tagName')" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="locationType" :label="t('tabPage.etagLocation.locationType')" width="240" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag :type="etagLocationTypeFormatter.etagLocationTypeFormatter(scope.row.locationType).type">
              {{etagLocationTypeFormatter.etagLocationTypeFormatter(scope.row.locationType).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="stand" :label="t('tabPage.etagLocation.stand')" 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>
<!--    添加数据-->
    <AddEtagLocation v-model="showAddDialog" @reLoadingTableData="query" />
<!--    修改数据-->
    <EditEtagLocation v-model="showEditDialog" :formData="editData" @reLoadingTableData="query" />
  </div>
</template>

<style scoped>

</style>