<script lang="ts" setup>
/**
 * @Name: Table.vue
 * @Description: 商品列表表格
 * @Author: Cheney
 */

import { Delete, Download, Operation, Plus, Refresh, Upload } from '@element-plus/icons-vue';
import * as api from '../api';
import PermissionButton from '/src/components/PermissionButton/index.vue';
import EditDrawer from './EditDrawer.vue';
import ImportButton from '/src/components/ImportButton/index.vue';
import VerticalDivider from '/src/components/VerticalDivider/index.vue';
import DataTableSlot from './DataTableSlot.vue';
import { productColumns } from '../ColumnsTsx';


const { tableOptions, handlePageChange } = usePagination(fetchList);

const gridRef = ref();
const gridOptions: any = reactive({
  border: false,
  round: true,
  stripe: true,
  currentRowHighLight: true,
  height: '100%',
  toolbarConfig: {
    custom: true,
    slots: {
      buttons: 'toolbar_buttons',
      tools: 'toolbar_tools'
    }
  },
  rowConfig: {
    isHover: true
  },
  columnConfig: {
    resizable: true
  },
  pagerConfig: {
    total: tableOptions.value.total,
    page: tableOptions.value.page,
    limit: tableOptions.value.limit
  },
  loading: false,
  loadingConfig: {
    icon: 'vxe-icon-indicator roll',
    text: '正在拼命加载中...'
  },
  columns: productColumns,
  data: ''
});

const checkedList = ref<Set<number>>(new Set());

const editOpen = ref(false);
const rowData = ref({});

const dialogVisible = ref(false);

const templateType = ref();

onBeforeMount(() => {
  fetchList();
});

async function fetchList() {
  const query = {
    page: gridOptions.pagerConfig.page,
    limit: gridOptions.pagerConfig.limit
  };
  await useTableData(api.getTableData, query, gridOptions);
}

function handleRefresh() {
  fetchList();
}

async function batchOpen() {
  const ids = Array.from(checkedList.value);
  await useResponse(api.updateShopDetail, { ids, status: 1 });
  checkedList.value.clear();
  await fetchList();
}

function selectChangeEvent({ checked, row }: any) {
  if (checked) {
    checkedList.value.add(row.id); // 获取单个数据
  } else {
    checkedList.value.delete(row.id);
  }
}

function selectAllChangeEvent({ checked }: any) {
  const $grid = gridRef.value;
  if ($grid) {
    const records = $grid.getData(); // 获取所有数据
    if (checked) {
      records.forEach((item: any) => {
        checkedList.value.add(item.id);
      });
    } else {
      checkedList.value.clear();
    }
  }
}

function handleEdit(row: any) {
  editOpen.value = true;
  rowData.value = row;
  console.log('(DataTable.vue: 111)=> rowData.value', rowData.value);
}

function singleDelete(row: any) {
  // dialogVisible.value = true;
  rowData.value = row;
  console.log('(DataTable.vue: 116)=> rowData.value', rowData.value);
}

function downloadTemplate() {
  // console.log('111=> ');
}
</script>

<template>
  <vxe-grid ref="gridRef" v-bind="gridOptions"
            @checkbox-change="selectChangeEvent"
            @checkbox-all="selectAllChangeEvent">
    <template #toolbar_buttons>
      <div class="flex gap-2">
        <PermissionButton :disabled="!checkedList.size" :icon="Delete" plain round type="danger" @click="batchOpen">
          批量删除
        </PermissionButton>
        <PermissionButton :icon="Plus" plain round type="primary" @click="batchOpen">
          新 增
        </PermissionButton>
        <div class="custom-el-input">
          <el-select v-model="templateType" placeholder="Select" style="width: 190px">
            <template #prefix>
              <div class="flex items-center">
                <el-button bg size="small" style="margin-left: -7px; font-size: 14px; border-radius: 29px;" text
                           type="success"
                           @click.stop="downloadTemplate">下载
                </el-button>
                <VerticalDivider style="margin-left: 7px" />
              </div>
            </template>
            <el-option label="商品通知模板" value="item1" />
            <el-option label="商品模板" value="item2" />
            <el-option label="指导价格模板" value="item3" />
          </el-select>
        </div>
        <VerticalDivider class="px-1" style="margin-left: 7px;" />
        <ImportButton :icon="Upload" bg text>导 入</ImportButton>
      </div>
    </template>
    <template #toolbar_tools>
      <el-button circle class="toolbar-btn" @click="handleRefresh">
        <el-icon>
          <Refresh />
        </el-icon>
      </el-button>
      <el-button circle class="mr-3 toolbar-btn">
        <el-icon>
          <Download />
        </el-icon>
      </el-button>
    </template>
    <template #top>
      <div class="mb-2"></div>
    </template>
    <template #pager>
      <vxe-pager
          v-model:currentPage="gridOptions.pagerConfig.page"
          v-model:pageSize="gridOptions.pagerConfig.limit"
          :total="gridOptions.pagerConfig.total"
          class="mt-1.5"
          @page-change="handlePageChange"
      />
    </template>
    <!-- 自定义列插槽 -->
    <template v-for="col in productColumns" :key="col.field" #[`${col.field}`]="{ row }">
      <DataTableSlot :field="col.field" :row="row" @edit-row="handleEdit" @single-delete="singleDelete" />
    </template>

  </vxe-grid>
  <EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" />
</template>

<style scoped>
.toolbar-btn {
  width: 34px;
  height: 34px;
  font-size: 18px
}

:deep(.custom-el-input .el-select__wrapper) {
  border-radius: 20px;
}
</style>
