<template>
  <div class="filterTable">
    <FilterOption :selectList="selectList" />
    <slot></slot>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { initData } from '@/common/export/table';
import { Ermcp3SpotGoodsPrice } from '@/services/go/ermcp/qhj/interface';
import { GetDeliveryGoodsDetail } from "@/services/go/ermcp/goodsInfo";
import { Ermcp3DeliveryGoodsDetail } from '@/services/go/ermcp/goodsInfo/interface/deliverygoodsdetail'
import { useFilter } from '@/common/components/filter/setup';
import FilterOption from '@/common/components/filter/index.vue';

export default defineComponent({
  name: 'spotmarket_price-filter-table',
  components: {
    FilterOption
  },
  setup(props, context) {
    // 现货品种列表
    const deliveryGoodsList: Ermcp3DeliveryGoodsDetail[] = [];
    const { selectList, search } = useFilter<Ermcp3SpotGoodsPrice>(context);

    selectList.value = [
      {
        key: 'deliverygoodsid',
        alias: 'deliverygoodsname',
        selectedValue: 0,
        options: [
          { label: '全部现货品种', value: 0 },
        ],
        onChange: (value: string | number) => {
          selectList.value[1].selectedValue = 0;
          selectList.value[1].options = [{ label: '全部商品', value: 0 }];

          selectList.value[2].selectedValue = '';
          selectList.value[2].options = [{ label: '全部品牌', value: '' }];

          deliveryGoodsList.forEach((item) => {
            if (item.deliverygoodsid === value) {
              // 商品列表
              item.wdlst.forEach((e) => {
                selectList.value[1].options.push({
                  label: e.wrstandardname,
                  value: e.wrstandardid,
                })
              })
              // 品牌列表
              item.bdlst.forEach((e) => {
                if (e.isvalid) {
                  selectList.value[2].options.push({
                    label: e.brandname,
                    value: e.brandname,
                  })
                }
              })
            }
          })
          search();
        },
      },
      {
        key: 'wrstandardid',
        alias: 'wrstandardname',
        selectedValue: 0,
        options: [
          { label: '全部商品', value: 0 },
        ],
        onChange: search,
      },
      {
        key: 'brandname',
        selectedValue: '',
        options: [
          { label: '全部品牌', value: '' },
        ],
        onChange: search,
      },
    ]

    // 数据初始化
    initData(() => {
      GetDeliveryGoodsDetail().then((res) => {
        deliveryGoodsList.push(...res.filter((item) => item.isvalid));
        deliveryGoodsList.forEach((item) => {
          selectList.value[0].options.push({
            label: item.deliverygoodsname,
            value: item.deliverygoodsid
          })
        })
      })
    })

    return {
      selectList,
    }
  }
});
</script>