<script lang="ts" setup>
/**
 * @Name: Table.vue
 * @Description: 市场店铺表格
 * @Author: Cheney
 */

import { Refresh } from '@element-plus/icons-vue';
import { usePagination } from '/@/utils/usePagination';
import { useTableData } from '/@/utils/useTableData';
import { MarketStoreColumns } from '/@/views/store-manage/Columns';
import DataTableSlot from './DataTableSlot.vue';
import * as api from '../api';
import StoreAuthorization from './storeAuthorization.vue';
import EditDrawer from './EditDrawer.vue';


interface Parameter {
  country: string,
  shop: string,
  region: string,
	platformId: string,
}

const queryParameter: Parameter | undefined = inject('query-parameter');
const { tableOptions, handlePageChange } = usePagination(fetchList);

const gridRef = ref();
const gridOptions: any = reactive({
  id: 'market-store-table',
  keepSource: true,
  size: 'small',
  border: false,
  round: true,
  stripe: true,
  currentRowHighLight: true,
  height: '100%',
  customConfig: {
    storage: {
      visible: true
    }
  },
  toolbarConfig: {
    size: 'large',
    // 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: '',
  data: ''
});

onBeforeMount(() => {
  gridOptions.pagerConfig.limit = 100;
})

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

async function fetchList(isQuery = false) {
  if (isQuery) {
    gridOptions.pagerConfig.page = 1;
  }
  
  gridOptions.data = [];
  gridOptions.columns = [];

  const query = {
    country_code: queryParameter?.country,
    region: queryParameter?.region,
    shop: queryParameter?.shop,
		platform_number: queryParameter?.platformId,
  };

  await useTableData(api.getTableData, query, gridOptions);
  if (gridOptions && gridOptions.data?.length) await gridRef.value.loadColumn(MarketStoreColumns);
  gridOptions.showHeader = Boolean(gridOptions.data?.length);
}

function handleRefresh() {
  fetchList();
}

defineExpose({ fetchList });

const authorizeDialogVisible = ref(false);
const authorizePrefill = ref<{ accountName: string; platformCode: string; countryCode: string } | null>(null);
const isReauthorize = ref(false);

function handleAddStore() {
	authorizePrefill.value = null;
	isReauthorize.value = false;
	authorizeDialogVisible.value = true;
}

function handleReauthorize(row: any) {
	authorizePrefill.value = {
		accountName: row.name ?? '',
		platformCode: row.platform_number ?? '',
		countryCode: row.country_code ?? '',
	};
	isReauthorize.value = true;
	authorizeDialogVisible.value = true;
}

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

function handleEdit(row: any) {
	editOpen.value = true;
	rowData.value = row;
}

</script>

<template>
  <vxe-grid ref="gridRef" v-bind="gridOptions">
		<!-- 左侧按钮区 -->
		<template #toolbar_buttons>
			<el-button type="primary" @click="handleAddStore">新增店铺授权</el-button>
		</template>
    <!-- 工具栏右侧 -->
    <template #toolbar_tools>
      <el-button circle class="toolbar-btn" @click="handleRefresh">
        <el-icon>
          <Refresh />
        </el-icon>
      </el-button>
    </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 #empty>
      <el-empty description="暂无数据" />
    </template>
    <!-- 自定义列插槽 -->
    <template v-for="col in MarketStoreColumns" #[`${col.field}`]="{ row }">
      <DataTableSlot :key="row.id" :field="col.field" :row="row" @edit-row="handleEdit" @reauthorize="handleReauthorize"/>
    </template>
  </vxe-grid>
	<EditDrawer v-if="editOpen" v-model="editOpen" :row-data="rowData" @refresh="handleRefresh" />
	<StoreAuthorization v-model="authorizeDialogVisible" :prefill-data="authorizePrefill" :reauthorize="isReauthorize"/>
</template>

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

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