<template>
  <div class="L-main" id="L-size-main">
    <div class="content-search" id="L-header">
      <div class="search-title">查询条件</div>
      <el-form :inline="true" ref="queryFormRef" :model="formInline" size="small" class="demo-form-inline">
        <el-row>
          <el-col :span="7">
            <el-form-item label="系统名称：" prop="xtmc">
              <el-input v-model="formInline.xtmc" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="系统类型：" prop="xtlx">
              <el-select v-model="formInline.xtlx" placeholder="请选择系统类型">
                <el-option
                  v-for="dict in zc_xtlx"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="系统状态：" prop="xtzt">
              <el-select v-model="formInline.xtzt" placeholder="请选择系统状态">
                <el-option
                  v-for="dict in zc_xtzt"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3" style="text-align: right;">
            <el-form-item class="unit-form">
              <el-button size="mini" @click="resetQuery">重置</el-button>
              <el-button size="mini" type="primary" @click="handleQuery">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <main>
      <div class="search-title">
        <span class="search-title-span">资产列表</span>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
        </el-row>
      </div>
      <section>
        <el-table v-loading="loading" :data="tableData" :height="tabHeader" :max-height="tabHeader" :row-class-name="tableRowClassName">
          <el-table-column type="index" width="50" label="序号" align="center"/>
          <el-table-column label="系统名称" key="xtmc" width="200px" prop="xtmc" align="center" />
          <el-table-column label="系统类型" key="xtlx" prop="xtlx" align="center">
            <template #default="scope">
              <dict-tag :options="zc_xtlx" :value="scope.row.xtlx"/>
            </template>
          </el-table-column>
          <el-table-column label="单位名称" key="dwmc" prop="dwmc" align="center" />
          <el-table-column label="新增时间" key="createTime" prop="createTime" align="center" />
          <el-table-column label="系统状态" key="status" prop="status" class-name="table-status" align="center">
            <template #default="scope">
              <span v-if="scope.row.xtzt == 1" style="color: #16B771;">
                已安装
              </span>
              <span v-else-if="scope.row.xtzt == 2" style="color: #F58A0C;">
                测试中
              </span>
              <span v-else-if="scope.row.xtzt == 3" style="color: #1485EF;">
                使用中
              </span>
              <span v-else-if="scope.row.xtzt == 4" style="color: #29A07A;">
                维护中
              </span>
              <span v-else-if="scope.row.xtzt == 5" style="color: #F50C0C;">
                关停
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop="userId" class-name="table-operation" align="center">
            <template #default="scope">
              <div style="display: flex;align-items: center;justify-content: center;">
                <div style="display: flex;align-items: center;cursor: pointer;" @click="goInfo(scope.row,1)">
                  <img src="@/assets/images/icon-ck@2x.png" alt="" style="width: 20px;margin-right: 5px;">
                  <span class="look-info" style="color: #1485EF;">查看</span>
                </div>
                <div v-if="!scope.row.auditState || scope.row.auditState == 2 || scope.row.xtzt == '5'" style="display: flex;align-items: center;cursor: pointer;margin-left: 10px;" @click="goInfo(scope.row,2)">
                  <img src="@/assets/images/edit.png" alt="" style="width: 15px;margin-right: 5px;">
                  <span class="look-info" style="color: #1485EF;margin-right: 15px;">编辑</span>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </section>
      <my-pagination 
        id="L-pagination"
        :total="total"
        :page="formInline.current"
        :limit="formInline.size"
        @pagination="getPagination"
        :current-page.sync="formInline.current" 
      ></my-pagination>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import myPagination from "@/views/components/Pagination/index.vue";
import { assetUnit } from "@/api/auditPagesApi/index";

const router = useRouter();
const route = useRoute();

// Dictionaries - these should be provided via props or composable
const zc_xtzt = ref([]);
const zc_xtlx = ref([]);

const queryFormRef = ref(null);
const formInline = ref({
  xtmc: "",
  xtlx: "",
  xtzt: "",
  current: 1,
  size: 10,
});
const total = ref(0);
const tableData = ref([]);
const loading = ref(false);
const tabHeader = ref(undefined);

const getList = async () => {
  try {
    loading.value = true;
    const res = await assetUnit('get', formInline.value);
    tableData.value = res.data.records;
    total.value = res.data.total;
  } catch (error) {
    console.error(error);
  } finally {
    loading.value = false;
  }
};

const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

const handleQuery = () => {
  formInline.value.current = 1;
  getList();
};

const getPagination = (pages) => {
  formInline.value.current = pages.page;
  formInline.value.size = pages.limit;
  getList();
};

const handleExport = () => {
  // Implement export functionality
  console.log('Export functionality to be implemented');
};

const goInfo = (row, id) => {
  router.push({
    name: 'myAssetsAuth',
    query: {
      pageType: id === 1 ? "look" : "change",
      type: 0,
      id: row.id,
      queryData: formInline.value,
      name: "MyWebAssets"
    }
  });
};

const tableRowClassName = ({ row, rowIndex }) => {
  return rowIndex % 2 !== 0 ? 'evenNumber-row' : '';
};

const cancalDebounce = () => {
  const element = document.getElementById('L-size-main');
  const header = document.getElementById('L-header');
  const pagination = document.getElementById('L-pagination');
  
  if (element && header && pagination) {
    const elementHeight = element.offsetHeight;
    const headerHeight = header.offsetHeight;
    const paginationtHeight = pagination.offsetHeight;
    tabHeader.value = elementHeight - headerHeight - paginationtHeight - 120;
  }
};

onMounted(() => {
  if (Object.keys(route.params).length !== 0) {
    formInline.value = route.params;
  } else {
    formInline.value.current = 1;
    formInline.value.size = 20;
  }
  
  getList();
  cancalDebounce();
  window.addEventListener('resize', cancalDebounce);
});

onUnmounted(() => {
  window.removeEventListener('resize', cancalDebounce);
});
</script>