<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 { useTableHeight } from '/@/utils/useTableHeight';
import DataTable from './component/DataTable.vue';
import { DictionaryStore } from '/@/stores/dictionary';
import { useTemplateRef } from 'vue';
import { useCountryInfoStore } from '/@/stores/countryInfo';
import { useResponse } from '/@/utils/useResponse';
import * as api from './api';


const { data: staticData } = DictionaryStore();
const countryInfoStore = useCountryInfoStore();

const titleContainer: Ref<HTMLElement | null> = useTemplateRef('titleContainer');
const queryContainer: Ref<HTMLElement | null> = useTemplateRef('queryContainer');
const { tableHeight } = useTableHeight(titleContainer, queryContainer);

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

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

const formInline = reactive<any>({
  country: '',
  shop: '',
  region: '',
  delivery: '',
  status: '',
  asin: '',
  sku: '',
	platformId: '',
});
provide('query-parameter', formInline);

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

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

async function fetchShopOptions() {
  const res = await useResponse(api.getShopOptions)
  shopOptions.value = res.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 ref="titleContainer" class="text-xl font-semibold pb-5">在线商品</div>
      <!-- 查询条件 -->
      <div ref="queryContainer" 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">
                <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="5">
                <div class="flex items-center">
                  <span class="mr-2">店 铺</span>
                  <el-select v-model="formInline.shop" clearable placeholder="请选择店铺">
                    <el-option v-for="item in shopOptions" :key="item.id" :label="item.name"
                               :value="item.id" />
                  </el-select>
                </div>
              </el-col>
              <el-col :span="5">
                <div class="flex items-center">
                  <span class="mr-2">站 点</span>
                  <el-select v-model="formInline.region" clearable placeholder="请选择站点">
                    <el-option v-for="item in countryInfoStore.Region" :label="item.name" :value="item.code" />
                  </el-select>
                </div>
              </el-col>
              <el-col :span="5">
                <div class="flex items-center">
                  <span class="mr-2">配送方式</span>
                  <el-select v-model="formInline.delivery" clearable placeholder="请选择配送方式">
                    <el-option label="FBM" value="FBM" />
                    <el-option label="FBA" value="FBA" />
                  </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 label="在售" value="Active" />
                    <el-option label="不在售" value="Inactive" />
                    <el-option label="不完整" value="Incomplete" />
                  </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="6">
								<div class="flex items-center">
									<span class="mr-2">平台编号</span>
									<el-input v-model="formInline.platformId" clearable placeholder="请输入平台编号" />
								</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>
