<script setup lang="ts">
import { ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { getCabinetCellOperationList, type CabinetCellOperationDTO, type SearchCabinetCellOperationQuery } from "@/api/cabinet/cell-operation";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import { useRoute } from 'vue-router'
import { watch } from 'vue';

defineOptions({
  name: "CellOperation"
});

const formRef = ref();
const tableRef = ref();

const searchFormParams = ref<SearchCabinetCellOperationQuery>({
  cellId: null,
  goodsId: null,
  operationType: null,
  status: null
});

const pagination = ref({
  pageSize: 10,
  currentPage: 1,
  total: 0
});

const loading = ref(false);
const dataList = ref<CabinetCellOperationDTO[]>([]);

const getList = async () => {
  try {
    loading.value = true;
    const { data } = await getCabinetCellOperationList({
      ...searchFormParams.value,
      pageSize: pagination.value.pageSize,
      pageNum: pagination.value.currentPage
    });
    dataList.value = data.rows;
    pagination.value.total = data.total;
  } finally {
    loading.value = false;
  }
};

const onSearch = () => {
  pagination.value.currentPage = 1;
  getList();
};

const resetForm = () => {
  formRef.value.resetFields();
  onSearch();
};

const onSizeChange = (val: number) => {
  pagination.value.pageSize = val;
  getList();
};

const onCurrentChange = (val: number) => {
  pagination.value.currentPage = val;
  getList();
};

const route = useRoute();

watch(() => route.query.cellId,
  (newVal) => {
    searchFormParams.value.cellId = newVal ? Number(newVal) : null;
    getList();
  },
  {
    immediate: true
  });
getList();
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :model="searchFormParams"
      class="search-form bg-bg_color flex w-[99/100] pl-[22px] pt-[12px]">
      <el-form-item prop="cellId">
        <el-input @keydown.enter.prevent="onSearch" v-model="searchFormParams.cellId" placeholder="请输入格口ID" clearable
          class="!w-[200px]" />
      </el-form-item>
      <el-form-item prop="operationType">
        <el-select v-model="searchFormParams.operationType" placeholder="请选择类型" clearable class="!w-[180px]">
          <el-option label="用户操作" :value="1" />
          <el-option label="管理员操作" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item prop="status">
        <el-select v-model="searchFormParams.status" placeholder="请选择状态" clearable class="!w-[180px]">
          <el-option label="正常" :value="1" />
          <el-option label="操作失败" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon(Search)" @click="onSearch" class="mr-2">
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm" class="mr-2">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="table-container">
      <el-table ref="tableRef" v-loading="loading" :data="dataList" row-key="operationId" border>
        <el-table-column label="操作ID" prop="operationId" width="120" />
        <el-table-column label="格口ID" prop="cellId" width="120" />
        <el-table-column label="操作人" prop="name" width="120" />
        <el-table-column label="手机号" prop="mobile" width="120" />
        <el-table-column label="商品名称" prop="goodsName" />
        <el-table-column label="操作类型" prop="operationType" width="120">
          <template #default="{ row }">
            <el-tag :type="row.operationType === 1 ? 'success' : 'warning'">
              {{ { 1: '用户', 2: '管理员' }[row.operationType] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作状态" prop="status" width="120">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ { 1: '正常', 2: '操作失败' }[row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" prop="createTimeStr" width="180" />
      </el-table>
      <el-pagination v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
        @size-change="onSizeChange" @current-change="onCurrentChange" class="pagination" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
    margin-right: 12px;
  }
}
.table-container {
  margin-top: 8px;
  background-color: var(--el-bg-color);
  border-radius: 4px;
  padding: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;

  .el-table {
    flex: 1;
    overflow: hidden;
  }

  .pagination {
    margin-top: 10px;
  }
}
</style>