import api from "@/axios";
import {AssessmentRecordPagerVo, DeductedDetailRes} from "@/types/views/unitManage/police/assessmentRecord.ts";
import {ColumnsType} from "ant-design-vue/es/table";
import {Modal, Table} from "ant-design-vue";

export const deductedDetail = async (assessmentRecord: AssessmentRecordPagerVo) => {
    const {data} = await api.get<DeductedDetailRes[]>('/assessmentRecord/deductedDetail', {assessmentRecordId: assessmentRecord.snowFlakeId})
    const groupRowSpan: Record<string, { firstIndex: number, count: number }> = {}
    const itemRowSpan: Record<string, { firstIndex: number, count: number }> = {}

    data.forEach((item, index) => {
        //如果第一次没有值
        if (item.ckGroupId) {
            if (!groupRowSpan[item.ckGroupId]) {
                groupRowSpan[item.ckGroupId] = {count: 1, firstIndex: index}
            } else {
                groupRowSpan[item.ckGroupId].count++;
                data[index].groupRowSpan = 0
            }
        }

        if (item.ckItemId) {
            if (!itemRowSpan[item.ckItemId]) {
                itemRowSpan[item.ckItemId] = {count: 1, firstIndex: index}
            } else {
                itemRowSpan[item.ckItemId].count++;
                data[index].itemRowSpan = 0
            }
        }
    })

    Object.values(groupRowSpan).forEach(({count, firstIndex}) => {
        data[firstIndex].groupRowSpan = count;
    })

    Object.values(itemRowSpan).forEach(({count, firstIndex}) => {
        data[firstIndex].itemRowSpan = count;
    })

    const ckProjectDetailTableColumns: ColumnsType<DeductedDetailRes> = [
        {
            dataIndex: 'groupName',
            title: '考核分组',
            customCell: (_record) => {
                return {
                    rowspan: _record.groupRowSpan
                }
            },
            customRender: ({record: _record}) => {
                return <div>
                    <p>{_record.groupName}({_record.groupTotalScore})</p>
                    <p>{_record.groupRemark}</p>
                </div>
            }
        }, {
            dataIndex: 'itemName',
            title: '考核项',
            customCell: (_record) => {
                return {
                    rowspan: _record.itemRowSpan
                }
            },
            customRender: ({record: _record}) => {
                if (!_record.ckItemId) {
                    return '/'
                }
                return <div>
                    <p>{_record.itemName}({_record.itemType?.label})
                    </p>
                </div>
            }
        }, {
            dataIndex: 'standardName',
            title: '标准',
            customRender: ({record: _record}) => {
                if (!_record.ckStandardId) {
                    return '/'
                }
                return <div>
                    <p style={{color: _record.isSelected ? 'red' : ''}}>{_record.standardName}扣{_record.deductionPoints}分</p>
                </div>
            }
        }
    ]

    Modal.info({
        title: `【${assessmentRecord.enterprisesUnitName}/${assessmentRecord.ckProjectName}】扣分详情`,
        icon: ' ',
        width: '80%',
        centered: true,
        content: () => <div style={{height: '80vh', overflow: 'auto'}}>
            <Table
                size="small"
                bordered
                pagination={false}
                class="margin-top-xs"
                columns={ckProjectDetailTableColumns}
                data-source={data}
            ></Table>
        </div>
    })

}
