<template>
  <!-- 商品订单 - 议价单 -->
  <mtp-table-scroll>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :expandIcon="expandIcon" :expandIconAsCell="false" rowKey="key" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template v-if="buttons.length" #expandedRowRender="{ record }">
          <BtnList :btnList="handleBtnList(record, buttons)" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <template #applytime="{ text }">
          <span>{{ formatTime(text) }}</span>
        </template>
        <template #transferamount="{ record , text}">
          <div>
            <!-- 审核拒绝 -->
            <a-tooltip placement="bottom" v-if="record.applystatus === 4" overlayClassName="toolTipTableColumn">
              <template #title>
                <span>{{ getScfContractStatusName(text) }}</span>
              </template>
              <span :class="record.applystatus === 4 ? 'red' : 'white'">{{ record.transferamount }}</span>
              <ExclamationCircleOutlined class="ml10 red" v-if="record.applystatus === 4" />
            </a-tooltip>
            <span v-else>{{ record.transferamount }}</span>
          </div>
        </template>
        <template #buyorsell="{ text }">
          <span>{{ getBuyOrSellName(text) }}</span>
        </template>
        <template #applystatus="{ text }">
          <span>{{ getScfContractStatusName(text) }}</span>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" :tableList="tableList" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { PropType, onBeforeUnmount } from 'vue';
import { DynamicComponent } from '@/@next/hooks/common/interface'
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { BtnListType } from '@/common/components/btnList/interface';
import { ApplyType } from '@/common/constants/enumCommon';
import { getBuyOrSellName, getScfContractStatusName } from '@/common/constants/enumsName';
import { BtnList, defineAsyncComponent, defineComponent, queryTableList } from '@/common/export/commonTable';
import { formatTime } from '@/common/methods';
import { handleDeliveryRelation } from '@/common/setup/deliveryRelation';
import { expandIcon } from '@/common/setup/table/clolumn';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { getAccoutIdList } from '@/services/bus/account';
import { QueryTradePositionRsp } from '@/services/go/ermcp/order/interface';
import { queryTjmdTransferApply } from '@/services/go/Tjmd';
import { QueryTjmdTransferApplyReq, QueryTjmdTransferApplyRsp } from '@/services/go/Tjmd/interface';
import Bus from '@/utils/eventBus/index';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { ref } from 'vue';
import { columns } from './setup';

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTableScroll,
    ExclamationCircleOutlined,
    BtnList,
    submit: defineAsyncComponent(() => import('./components/commodity_contract_bargain_submit_order/index.vue')), // 同意
    cancel: defineAsyncComponent(() => import('./components/commodity_contract_bargain_cancel_order/index.vue')), // 撤单
    refuse: defineAsyncComponent(() => import('./components/commodity_contract_bargain_refuse_order/index.vue')), // 拒绝
  },
  setup(props) {
    const { buttons } = props.parentComponent;
    // 类型 1-我的申请 2-对方申请
    const margainType = ref<ApplyType>(ApplyType.my);
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryTjmdTransferApplyRsp>();

    // 交割商品
    handleDeliveryRelation();

    // 获取列表数据
    const queryTableAction = () => {
      const param: QueryTjmdTransferApplyReq = {
        applytype: margainType.value,
        accountids: getAccoutIdList(),
      };
      queryTable(queryTjmdTransferApply, param);
    }

    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: () => queryTableAction(),
    }

    function handleBtnList(record: QueryTjmdTransferApplyRsp, btnList: BtnListType[]) {
      if (record.applystatus === 1) {
        if (margainType.value === ApplyType.my) {
          // 我的申请
          return btnList.filter((e) => e.code === 'cancel');
        } else {
          // 对方的申请
          return btnList.filter((e) => e.code !== 'cancel');
        }
      } else {
        return [];
      }
    }

    // 切换 我的申请和对方申请
    const stopNotify = Bus.$on('bargain', (value) => {
      margainType.value = value as ApplyType;
      queryTableAction();
    })

    onBeforeUnmount(() => {
      stopNotify();
    })

    return {
      ...handleComposeOrderTable<QueryTradePositionRsp>(param),
      columns,
      loading,
      tableList,
      buttons,
      formatTime,
      expandIcon,
      handleBtnList,
      getBuyOrSellName,
      getScfContractStatusName,
    }
  }
})
</script>
