<template>
  <!-- 履约模板查询 -->
  <mtp-table-scroll>
    <template #header>
      <div class="tltRight">
        <BtnList :btnList="headBtns" @click="openComponent" />
      </div>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :loading="loading" :customRow="Rowclick" :rowKey="(record, index) => index" :data-source="tableList">
        <!-- 状态 -->
        <template #handle="{ record }">
          <BtnList :btnList="tableBtns" :record="record" class="btn-list-sticky" @click="openComponent" />
        </template>
        <template #createtime="{ record }">
          <span>{{ formatTime(record.createtime) }}</span>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 明细 -->
  <Description v-if="visible" @close="closeDrawer" :tabList="tabList">
    <a-table :columns="detailColumns" class="topTable" :pagination="false" :rowKey="(record,index)=>index" :data-source="getDetailTalbeList()" :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }">
      <template #stepvalue="{ text }">
        <span>{{ text ? (text * 100).toFixed(0) : text }}</span>
      </template>
    </a-table>
  </Description>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, defineComponent, BtnList, defineAsyncComponent } from '@/common/export/commonTable';
import { queryQueryPermancePlanTmp } from '@/services/go/wrtrade';
import { QueryPermancePlanTmpReq, QueryPermancePlanTmpRsp } from '@/services/go/wrtrade/interface';
import { handleComposeTable_detail } from '@/common/setup/table/compose';
import { ComposeTableDetailParam } from '@/common/setup/table/interface';
import { formatTime } from '@/common/methods';
import { expandIcon } from '@/common/setup/table/clolumn';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { getColumns } from './setup';
import { getUserId } from '@/services/bus/user';
import { getTableButton } from '@/common/setup/table/button';
import { handleModalComponent } from '@/common/setup/asyncComponent';
import Description from '@/common/components/description/index.vue';
import { Ref } from 'vue';

const useButton = () => {
  // 表格操作按钮列表
  const btnList = getTableButton();
  // 表格 头部 上面按钮
  const headBtns = btnList.filter((e) => e.code === 'add');
  const tableBtns = btnList.filter((e) => e.code !== 'add');
  return { headBtns, tableBtns };
};

const useStepDetail = (selectedRow: Ref<QueryPermancePlanTmpRsp | undefined>) => {
  const temp = [
    { title: '履约步骤', key: 'steptypename' },
    { title: '步骤值(%)', key: 'stepvalue' },
    { title: '距离上一步天数', key: 'stepdays' },
  ];
  const columns = temp.map((el) => {
    const obj = {
      dataIndex: el.key,
      align: 'center',
      width: 200,
      slots: { customRender: el.key },
    };
    return { ...el, ...obj };
  });
  function getDetailTalbeList() {
    return selectedRow.value ? selectedRow.value.LstStep : [];
  }
  return { detailColumns: columns, getDetailTalbeList };
};

export default defineComponent({
  components: {
    MtpTableScroll,
    BtnList,
    Description,
    add: defineAsyncComponent(() => import('@/common/components/permanceTemp/addPermanceTemp.vue')),
    modify: defineAsyncComponent(() => import('@/common/components/permanceTemp/addPermanceTemp.vue')),
    delete: defineAsyncComponent(() => import('./delete.vue')),
  },
  setup() {
    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QueryPermancePlanTmpRsp>();
    const loadData = () => {
      const param: QueryPermancePlanTmpReq = {
        userid: getUserId(),
        tmptype: '0,2,3',
      };
      queryTable(queryQueryPermancePlanTmp, param);
    };

    loadData();
    // 表头
    const { columns } = getColumns();
    const param: ComposeTableDetailParam = {
      queryFn: loadData, // 查询表格数据
      menuType: EnumRouterName.search_performance_template_sub, // 当前tab页对应的code
    };

    const {
      visible,
      closeDrawer, // 控制 drawer 组件是否显示
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格事件
      tabList,
    } = handleComposeTable_detail<QueryPermancePlanTmpRsp>(param);

    return {
      loading,
      tableList,
      ...useButton(),
      columns,
      ...handleModalComponent(loadData, selectedRow),
      ...useStepDetail(selectedRow),

      visible,
      closeDrawer, // 控制 drawer 组件是否显示
      expandedRowKeys,
      selectedRow,
      Rowclick, // 表格事件
      tabList,

      formatTime,
      expandIcon,
    }
  }
})
</script>