
import React from 'react';
import { Checkbox } from 'kts-components-antd-x3';
import Invoice from '../../../../..';

export default () => {

    /** 控制器 */
    const controller = Invoice.useInvoiceController();

    /** 列表选中的货物索引列表 */
    const selectedRowKeys = controller.useMemo(s => s.freightListState.selectedGoodIndex, []);

    /** 货物列表 */
    const goodsList = controller.useMemo(s => s.freightListState.goodsList, []);

    /** 组件模式 */
    const model = controller.useMemo(s => s.model, []);

    /** 搜索条件 */
    // const searchValue = controller.useMemo(s => s.goodsListState.searchValue, []);

    /** 是否显示选择框 */
    const isShow = React.useMemo(() => {
        if (model === 'readOnly' || model === 'prefab') return false
        return true;
    }, [model])

    /** 是否全选 */
    // const isAll = controller.useMemo(s => s.goodsListState.goodsList.length > 0 && s.goodsListState.selectedGoodIndex.length === s.goodsListState.goodsList.length, [])
    const isAll = controller.useMemo(s => {
        if (s.freightListState.selectedGoodIndex.length <= 0) return false;
        const seeGoodsIndex = s.freightListState.goodsList.map(e => e.$index);
        const selectedGoodIndex = s.freightListState.selectedGoodIndex.filter(e => seeGoodsIndex.some(t => e === t));
        return selectedGoodIndex.length === seeGoodsIndex.length
    }, [])

    const indeterminate = controller.useMemo(s => {
        const seeGoodsIndex = s.freightListState.goodsList.map(e => e.$index);
        const selectedGoodIndex = s.freightListState.selectedGoodIndex.filter(e => seeGoodsIndex.some(t => e === t));
        if (selectedGoodIndex.length === 0) return false;
        return selectedGoodIndex.length < seeGoodsIndex.length
    }, [])
    /** 点击了全选 */
    const onClickSelectAll = React.useCallback(async () => {
        if (isAll) {
            await controller.pipeline(async s => {
                const seeGoodsIndex = s.freightListState.goodsList.map(e => e.$index)

                s.freightListState.selectedGoodIndex = s.freightListState.selectedGoodIndex.filter(e => !seeGoodsIndex.some(t => e === t));
            })()
        } else {
            await controller.pipeline(async s => {
                const seeGoodsIndex = s.freightListState.goodsList.map(e => e.$index)

                s.freightListState.selectedGoodIndex = [...s.freightListState.selectedGoodIndex, ...seeGoodsIndex];
                s.freightListState.selectedGoodIndex = Array.from(new Set(s.freightListState.selectedGoodIndex));
            })()
        }
        // await sortOut(true);
    }, [controller, isAll])

    const onSelect = React.useCallback(async (record, selected) => {
        if (selected) {
            await controller.pipeline(async s => {
                s.freightListState.selectedGoodIndex = [...s.freightListState.selectedGoodIndex, record.$index];
                s.freightListState = { ...s.freightListState };
            })()
        } else {
            await controller.pipeline(async s => {
                s.freightListState.selectedGoodIndex = s.freightListState.selectedGoodIndex.filter(e => e !== record.$index);
                s.freightListState = { ...s.freightListState };
            })()
        }
        // await sortOut(selected);
    }, [controller])

    const columnTitle = React.useMemo(() => {
        return (
            <Checkbox
                onChange={onClickSelectAll}
                indeterminate={indeterminate}
                checked={isAll}
            ></Checkbox>
        )
    }, [goodsList, selectedRowKeys, onClickSelectAll, isAll, indeterminate])

    /** 选择了商品后 调整 折扣行 和 被折扣行 */
    const sortOut = React.useCallback(async (selected: boolean) => {
        await controller.wait();
        await controller.pipeline(async s => {
            s.freightListState.selectedGoodIndex.forEach($index => {
                const goods = s.freightListState.goodsMap.get($index);
                if (!goods) return;

                // 数组位置
                let t = s.freightListState.goodsList.indexOf(goods);
                // goods.lineAttribute === LineAttributeType.折扣行 ? t : t++;
                const i = s.goodsListState.goodsList[t].$index;
                if (selected) {
                    if (s.freightListState.selectedGoodIndex.indexOf(i) < 0) s.freightListState.selectedGoodIndex = [...s.freightListState.selectedGoodIndex, i];
                } else {
                    if (s.freightListState.selectedGoodIndex.indexOf(i) < 0) s.freightListState.selectedGoodIndex = s.freightListState.selectedGoodIndex.filter(e => e !== $index);
                }
            })
        })()
    }, [controller])

    // React.useEffect(() => {
    //     sortOut(true);
    // }, [sortOut, goodsList])

    if (isShow === false) return undefined;
    return {
        columnWidth: 45,
        columnTitle,
        onSelect,
        selectedRowKeys,
    }
}