<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="buttons" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <template #createtime="{ record }">
          <span>{{ formatTime(record.createtime) }}</span>
        </template>
        <!-- 点价价格  x+c 交收模式，不显示价格-->
        <template #pdeliveryprice="{ record }">
          <span>{{ record.pgoodsid ? record.pdeliveryprice : '--' }}</span>
        </template>
        <!-- 点价货款  x+c 交收模式，不显示点价货款-->
        <template #p1goodsremainamount="{ record }">
          <span>{{ record.pgoodsid ? record.p1goodsremainamount : '--' }}</span>
        </template>
        <!-- 类型 -->
        <template #buyorsell="{ text }">
          <span>{{ getBuyOrSellName(text) }}</span>
        </template>
        <!-- 状态 -->
        <template #deliverystatus="{ record }">
          <a>{{ getDeliveryStatusName(record.deliverystatus) }}</a>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import { PropType } from 'vue';
import { DynamicComponent } from '@/@next/hooks/common/interface'
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, BtnList, defineComponent } from '@/common/export/commonTable';
import { queryWrDeliveryDetail } from '@/services/go/wrtrade';
import { WrDeliveryDetail } from '@/services/go/wrtrade/interface';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { getBuildTypeName, getDeliveryStatusName } from '@/common/constants/enumsName';
import { formatTime } from '@/common/methods';
import { expandIcon } from '@/common/setup/table/clolumn';
import { getBuyOrSellName } from '@/common/constants/enumsName';

const useColumns = () => {
  const columns = [
    {
      key: '0th',
      dataIndex: 'xgoodsname',
      title: '订单合约',
      align: 'center',
      slots: {
        customRender: 'xgoodsname',
      },
      width: 120,
    },
    {
      key: '1th',
      dataIndex: 'buyorsell',
      title: '类型',
      align: 'center',
      slots: {
        customRender: 'buyorsell',
      },
      width: 120,
    },
    {
      key: '2th',
      dataIndex: 'xdeliverycloseqty',
      title: '合同数量',
      align: 'center',
      slots: {
        customRender: 'xdeliverycloseqty',
      },
      width: 120,
    },
    {
      key: '3th',
      dataIndex: 'xgoodsamount',
      title: '合约金额',
      align: 'center',
      slots: {
        customRender: 'xgoodsamount',
      },
      width: 120,
    },
    {
      key: '4th',
      dataIndex: 'deliveryqty',
      title: '点选数量',
      align: 'center',
      slots: {
        customRender: 'deliveryqty',
      },
      width: 120,
    },
    {
      key: '5th',
      dataIndex: 'pdeliveryprice',
      title: '点价价格',
      align: 'center',
      slots: {
        customRender: 'pdeliveryprice',
      },
      width: 120,
    },
    {
      key: '6th',
      dataIndex: 'p1goodsremainamount',
      title: '点价货款',
      align: 'center',
      slots: {
        customRender: 'p1goodsremainamount',
      },
      width: 120,
    },
    {
      key: '7th',
      dataIndex: 'deliverypricemove',
      title: '升贴水',
      align: 'center',
      slots: {
        customRender: 'deliverypricemove',
      },
      width: 120,
    },
    {
      key: '8th',
      dataIndex: 'deliverytotalamount',
      title: '交收金额',
      align: 'center',
      slots: {
        customRender: 'deliverytotalamount',
      },
      width: 120,
    },
    {
      key: '9th',
      dataIndex: 'averageprice',
      title: '交收均价',
      align: 'center',
      slots: {
        customRender: 'averageprice',
      },
      width: 120,
    },
    {
      key: '10th',
      dataIndex: 'deliverytime',
      title: '申请时间',
      align: 'center',
      slots: {
        customRender: 'deliverytime',
      },
      width: 200,
    },
    {
      key: '11th',
      dataIndex: 'deliverystatus',
      title: '状态',
      align: 'center',
      slots: {
        customRender: 'deliverystatus',
      },
      width: 120,
    },
  ];
  return { columns };
};

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTableScroll,
    BtnList,
  },
  setup(props) {
    const { buttons } = props.parentComponent;
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<WrDeliveryDetail>(true);

    // 获取列表数据
    const queryTableAction = () => {
      queryTable(queryWrDeliveryDetail);
    }

    // 表格通用逻辑
    const param: ComposeOrderTableParam = {
      queryFn: queryTableAction,
      // tableName: 'table_pcweb_spot_trade_bottom_commodity_contract_settlement',
    }

    return {
      ...handleComposeOrderTable<WrDeliveryDetail>(param),
      ...useColumns(),
      loading,
      tableList,
      buttons,
      formatTime,
      expandIcon,
      getBuildTypeName,
      getDeliveryStatusName,
      getBuyOrSellName,
    }
  }
})
</script>