<template>
  <div class="filterTable">
    <Filter @update="update" :showUser="false" :selectList="selectList">
      <template #default="{ reset }">
        <a-button class="selectBtn" @click="resetAction(reset)">重置</a-button>
      </template>
    </Filter>
  </div>
</template>

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

export default defineComponent({
  name: 'inventory-filter-table',
  components: {
    Filter
  },
  setup(props, context) {
    // 现货品种列表
    const deliveryGoodsList: Ermcp3DeliveryGoodsDetail[] = [];
    const { selectList, search, reset } = useFilter<Ermcp3AreaSpotPLReport>(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 }];

          deliveryGoodsList.forEach((item) => {
            if (item.deliverygoodsid === value) {
              // 商品列表
              item.wdlst.forEach((e) => {
                selectList.value[1].options.push({
                  label: e.wrstandardname,
                  value: e.wrstandardid,
                })
              })
            }
          })
          search();
        },
      },
      {
        key: 'wrstandardid',
        alias: 'wrstandardname',
        selectedValue: 0,
        options: [
          { label: '全部商品', value: 0 },
        ],
        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
          })
        })
      })
    })

    function resetAction(parentReset: () => void) {
      parentReset();
      reset();
    }

    function update(value: TypeAndTime) {
      context.emit('update', value);
    }

    return {
      selectList,
      update,
      resetAction,
    }
  },
})
</script>