import { request } from '@vtx/utils';
import moment from 'moment';
import { useRequest } from 'ahooks';

export const toFixd2 = num => {
    const newNum = Number(num);
    return newNum ? newNum.toFixed(2) : '';
};
const BaseData = {
    useBaseInfo: (id, date) => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_GET_ROAD_BY_ID',
                        parameters: {
                            id,
                        },
                    }),
                }),
            { refreshDeps: [id] },
        );
        const baseInfo = baseInfoRes?.data;
        const info = [
            {
                label: '管理单位',
                key: 'deptName',
                value: baseInfo?.manage_unit_name,
            },
            {
                label: '作业单位',
                key: 'useUnitName',
                value: baseInfo?.company_name,
            },
            {
                label: '行政区划',
                key: 'divisionName',
                value: baseInfo?.division_name,
            },
            {
                label: '路段长度(km)',
                key: 'manageStaffName',
                value: toFixd2(baseInfo?.road_space),
            },
            {
                label: '路段类型',
                key: 'contact',
                value: baseInfo?.road_type_name,
            },
            {
                label: '路段宽度(m)',
                key: 'contactWay',
                value: toFixd2(baseInfo?.road_width),
            },
            {
                label: '所属标段',
                key: 'contactWay',
                value: baseInfo?.bid_section_name,
            },
        ];

        const { data: workRuleRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_GET_RULE_LIST_BY_ROAD_ID',
                        parameters: { roadId: id, ruleDate: date },
                    }),
                }),
            { refreshDeps: [id, date] },
        );
        const workRule = workRuleRes?.data || [];

        const { data: roadSummaryRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_GET_ROAD_WORK_SUMMARY',
                        parameters: {
                            roadId: id,
                            runDate: date,
                        },
                    }),
                }),
            { refreshDeps: [id, date] },
        );
        const roadSummary = (roadSummaryRes?.data || []).filter(
            item => item.work_mileage || item.work_times,
        );

        const { data: workCarRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_GET_ROAD_CAR_WORK_SUMMARY',
                        parameters: {
                            roadId: id,
                            runDate: date,
                        },
                    }),
                }),
            { refreshDeps: [id, date] },
        );
        const workCar = workCarRes?.data || [];

        return { info, workRule, roadSummary, workCar };
    },

    useStatistics: (id, start, end, workType) => {
        const { data: workTotalRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_GET_ROAD_WORK_TOTAL_SUMMARY',
                        parameters: {
                            roadId: id,
                            startDate: start,
                            endDate: end,
                            workType: workType === '全部' ? '' : workType,
                        },
                    }),
                }),
            { refreshDeps: [id, start, end, workType] },
        );
        const workTotal = workTotalRes?.data || {};

        const { data: otherRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZYQS_CAR_WORK_STATISTICS',
                        parameters: {
                            roadId: id,
                            startDate: start,
                            endDate: end,
                            workType: workType === '全部' ? '' : workType,
                        },
                    }),
                }),
            { refreshDeps: [id, start, end, workType] },
        );
        const other = otherRes?.data || [];
        const days = moment(end).diff(moment(start), 'd') + 1;
        const workMile = {
            xAxis: [],
            planMiles: [],
            workMiles: [],
        };
        const workCompleteRate = {
            xAxis: [],
            rates: [],
        };
        const workSummary = {
            a: 0,
            b: 0,
            c: 0,
            d: 0,
        };
        [...new Array(days)].forEach((_, index) => {
            const date = moment(start)
                .add(index, 'd')
                .format('YYYY-MM-DD');
            workMile.xAxis.push(date);
            workCompleteRate.xAxis.push(date);
            const data = other.find(e => e.work_date === date);
            if (data) {
                workMile.planMiles.push(toFixd2(data.plan_mileage));
                workMile.workMiles.push(toFixd2(data.work_mileage));
                workCompleteRate.rates.push(data.rate);
                if (data.rate < 50) {
                    workSummary.a += 1;
                }
                if (data.rate >= 50 && data.rate < 80) {
                    workSummary.b += 1;
                }
                if (data.rate >= 80 && data.rate < 100) {
                    workSummary.c += 1;
                }
                if (data.rate === 100) {
                    workSummary.d += 1;
                }
            } else {
                workSummary.a += 1;
                workSummary.d += 1;
                workMile.planMiles.push(0);
                workMile.workMiles.push(0);
                workCompleteRate.rates.push(0);
            }
        });

        const { data: workTypeDataRes } = useRequest(
            () =>
                request.post('/cloud/management/rest/param/getByParamTypeCode', {
                    data: {
                        parameters: JSON.stringify({ paramTypeCode: 'param_zyqs_workingType' }),
                    },
                }),
            {
                refreshDeps: [],
            },
        );
        const workTypeData = [{ parmCode: '', parmName: '全部' }, ...(workTypeDataRes?.data || [])];
        return { workTotal, workTypeData, workMile, workSummary, workCompleteRate };
    },
};

export { BaseData };
