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

import VerticalDivider from '/src/components/VerticalDivider/index.vue';
import { RefreshLeft, Search } from '@element-plus/icons-vue';
import { DictionaryStore } from '/@/stores/dictionary';
import { useResponse } from '/@/utils/useResponse';
import { useTemplateRef } from 'vue';
import * as api from './api';
import { useCustomHeight } from '/@/utils/useCustomHeight';
import DataTable from './component/DataTable.vue';


defineOptions({
  name: 'ProductList'
});

const { data: staticData } = DictionaryStore();

const baseHeight = {
  headerHeight: 50,
  dividerHeight: 32,
  toolbarHeight: 51,
  padding: 40
};
const heightObj = { titleContainer: 48, queryContainer: 80, ...baseHeight };
const { tableHeight } = useCustomHeight(heightObj);

const tableRef: Ref<any> = useTemplateRef('table');

const btnLoading = ref(false);
const resetLoading = ref(false);

const formInline = reactive<any>({
  country: '',
  brand: '',
  group: '',
  status: '',
  asin: '',
  sku: '',
  shop: '',
  isCompetitors: '',
  platformNumber: ''
});
provide('query-parameter', formInline);

const groupOptions = <any>ref([]);
const brandsOptions = <any>ref([]);
const shopOptions = <any>ref([]);

provide('groupOptions', groupOptions);
provide('brandsOptions', brandsOptions);
provide('shopOptions', shopOptions);

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

async function fetchOptions() {
  groupOptions.value = (await useResponse(api.getGroupOptions)).data;
  brandsOptions.value = (await useResponse(api.getBrandsOptions)).data;
  shopOptions.value = (await useResponse(api.getShopOptions)).data;
}

async function handleQuery() {
  btnLoading.value = true;
  await tableRef.value?.fetchList(true);
  btnLoading.value = false;
}

async function resetParameter() {
  for (const key in formInline) {
    formInline[key] = '';
  }
  resetLoading.value = true;
  await tableRef.value?.fetchList(true);
  resetLoading.value = false;
}
</script>

<template>
  <div class="p-5">
    <el-card class="h-full" style="color: rgba(0, 0, 0, 0.88);">
      <div class="text-xl font-semibold pb-5">商品列表</div>
      <!-- 查询条件 -->
      <div class="flex justify-between">
        <div class="flex flex-1">
          <div class="w-full whitespace-nowrap">
            <el-row :gutter="20" style="margin-bottom: 16px;">
							<el-col :span="4" class="flex">
								<div class="flex items-center">
									<span class="mr-2">店 铺</span>
									<el-input v-model="formInline.shop" clearable placeholder="请输入店铺" />
								</div>
							</el-col>
							<el-col :span="4" class="flex">
								<div class="flex items-center">
									<span class="mr-2">平台编号</span>
									<el-input v-model="formInline.platformNumber" clearable placeholder="请输入平台编号" />
								</div>
							</el-col>
              <el-col :span="4">
                <div class="flex items-center">
                  <span class="mr-2">国 家</span>
                  <el-select v-model="formInline.country" clearable placeholder="请选择国家">
                    <el-option v-for="item in staticData.country_code" :key="item.value" :label="item.label"
                               :value="item.value" />
                  </el-select>
                </div>
              </el-col>
              <el-col :span="4">
                <div class="flex items-center">
                  <span class="mr-2">品 牌</span>
                  <el-select v-model="formInline.brand" clearable placeholder="请选择品牌">
                    <el-option v-for="item in brandsOptions" :label="item.brand" :value="item.brand" />
                  </el-select>
                </div>
              </el-col>
              <el-col :span="4">
                <div class="flex items-center">
                  <span class="mr-2">分 组</span>
                  <el-select v-model="formInline.group" clearable placeholder="请选择分组">
                    <el-option v-for="item in groupOptions" :label="item.tag" :value="item.tag" />
                  </el-select>
                </div>
              </el-col>
              <el-col :span="4">
                <div class="flex items-center">
                  <span class="mr-2">状 态</span>
                  <el-select v-model="formInline.status" clearable placeholder="请选择状态">
                    <el-option v-for="item in staticData.goods_status" :key="item.value" :label="item.label"
                               :value="item.value" />
                  </el-select>
                </div>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="6">
                <div class="flex items-center">
                  <span class="mr-2">ASIN</span>
                  <el-input v-model="formInline.asin" clearable placeholder="请输入ASIN" />
                </div>
              </el-col>
              <el-col :span="6">
                <div class="flex items-center">
                  <span class="mr-2">SKU</span>
                  <el-input v-model="formInline.sku" clearable placeholder="请输入SKU" />
                </div>
              </el-col>
							<el-col :span="4" class="flex">
								<div class="flex items-center">
									<span class="mr-2">竞 品</span>
									<el-select v-model="formInline.isCompetitors" clearable placeholder="请选择是否竞品">
										<el-option label="是" value="1" />
										<el-option label="否" value="0" />
									</el-select>
								</div>
							</el-col>
            </el-row>
          </div>
        </div>
        <VerticalDivider />
        <div class="flex flex-col gap-1.5 items-end">
          <el-button :icon="Search" :loading="btnLoading" class="mb-4" type="primary" @click="handleQuery">
            查 询
          </el-button>
          <el-button :icon="RefreshLeft" :loading="resetLoading" color="#ECECF1C9" style="width: 88px; color: #3c3c3c;"
                     @click="resetParameter">
            重 置
          </el-button>
        </div>
      </div>
      <el-divider ref="dividerContainer" style="margin: 20px 0 12px 0;" />
      <div :style="{ height: tableHeight + 'px' }">
        <DataTable ref="table" />
      </div>
    </el-card>
  </div>
</template>

<style scoped>

</style>
