<!-- 报表查询-机构分润报表-机构分润单据明细 -->
<template>
    <app-drawer :title="t('report.instprofitshare.details.title')" width="960" v-model:show="show">
        <app-table :data="dataList" :columns="tableColumns">
            <!-- 源账户 -->
            <template #accountid="{ row }">
                {{ `${row.relateduserid}/${row.accountname}/${row.accountid}` }}
            </template>
            <!-- 分配值 -->
            <template #dividevalue="{ row }">
                <span v-if="row.dividevalue">
                    {{ row.dividetype === 1 ? parsePercent(row.dividevalue) : row.dividevalue }}
                </span>
                <span v-else>{{ handleNoneValue() }}</span>
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="getDataList" />
            </template>
        </app-table>
        <template #footer>
            <el-button @click="onCancel">{{ t('operation.close') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { handleNoneValue, parsePercent } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { ReportType } from '@/constants/report'
import { useRequest } from '@/hooks/request'
import { queryDODPage } from '@/services/api/report'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import moment from 'moment'

const props = defineProps({
    queryParams: {
        type: Object as PropType<Model.ShareAmountQueryReq>,
        required: true
    },
    record: {
        type: Object as PropType<Model.ShareAmountQueryRsp>,
        required: true
    }
})

const dividetypeEnum = useEnum('dividetype') // 分润算法

const t = i18n.global.t
const show = shallowRef(true)
const qs: Partial<Model.DODPageReq> = {}

const { dataList, total, pageSize, pageIndex, run } = useRequest(queryDODPage, {
    manual: true,
    params: {
        areaaccountid: props.record.areaaccountid,
        areaid: props.record.areaid,
        areatype: props.record.areatype,
        goodsid: props.record.goodsid,
        memberuserid: props.record.memberuserid,
        pageNum: 1,
        pageSize: 10,
        parentuserid: props.record.parentuserid,
        searchtype: props.record.searchtype,
        tradefeetype: props.record.tradefeetype,
        tradeproperty: props.record.tradeproperty
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountid', label: 'report.instprofitshare.details.accountid', width: 300 },
    { field: 'feeamount', label: 'report.instprofitshare.details.feeamount' },
    { field: 'dividetype', label: 'report.instprofitshare.details.dividetype', formatValue: (val) => dividetypeEnum.getEnumTypeName(val) },
    { field: 'dividevalue', label: 'report.instprofitshare.details.dividevalue' },
    { field: 'divideamount', label: 'report.instprofitshare.details.divideamount' }
])

const getDataList = () => run(qs)

const onCancel = () => {
    show.value = false
}

onMounted(() => {
    const { cycletype, tradedate, quarter } = props.queryParams
    const date = moment(tradedate, 'YYYYMMDD')

    switch (cycletype) {
        case ReportType.Week: {
            qs.startDate = date.startOf('isoWeek').format('YYYYMMDD') // 获取本周的星期一
            qs.endDate = date.endOf('isoWeek').format('YYYYMMDD') // 获取本周的星期天
            break
        }
        case ReportType.Month: {
            qs.startDate = date.startOf('month').format('YYYYMMDD') // 获取本月的第一天
            qs.endDate = date.endOf('month').format('YYYYMMDD') // 获取本月的最后一天
            break
        }
        case ReportType.Quarter: {
            // 定义季度的起始月份（0-11，所以第三季度从7月开始）
            const startMonth = (quarter - 1) * 3
            const endMonth = startMonth + 2 // 第三季度结束于9月
            // 获取季度的第一天
            const startDate = moment([tradedate, startMonth, 1])
            // 获取季度的最后一天
            const endDate = moment([tradedate, endMonth]).endOf('month')

            qs.startDate = startDate.format('YYYYMMDD')
            qs.endDate = endDate.format('YYYYMMDD')
            break
        }
        case ReportType.Year: {
            qs.startDate = tradedate + '0101'
            qs.endDate = tradedate + '1231'
            break
        }
    }

    getDataList()
})
</script>