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

export default () => {

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

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

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

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

    /** 是否全选 */
    // 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.goodsListState.selectedGoodIndex.length <= 0) return false;
        const seeGoodsIndex = controller.getGoodsSearch(s.goodsListState.goodsList, s.goodsListState.searchValue, s.goodsListState.isTaxIncluded).map(e => e.$index);
        const selectedGoodIndex = s.goodsListState.selectedGoodIndex.filter(e => seeGoodsIndex.some(t => e === t));
        return selectedGoodIndex.length === seeGoodsIndex.length
    }, [])

    const indeterminate = controller.useMemo(s => {
        const seeGoodsIndex = controller.getGoodsSearch(s.goodsListState.goodsList, s.goodsListState.searchValue, s.goodsListState.isTaxIncluded).map(e => e.$index);
        const selectedGoodIndex = s.goodsListState.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 = controller.getGoodsSearch(s.goodsListState.goodsList, s.goodsListState.searchValue, s.goodsListState.isTaxIncluded).map(e => e.$index)

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

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

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

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

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

                if (!goods || (goods.lineAttribute !== LineAttributeType.折扣行 && goods.lineAttribute !== LineAttributeType.被折扣行)) return;

                // 数组位置
                let t = s.goodsListState.goodsList.indexOf(goods);
                goods.lineAttribute === LineAttributeType.折扣行 ? t-- : t++;
                const i = s.goodsListState.goodsList[t].$index;

                if (selected) {
                    if (s.goodsListState.selectedGoodIndex.indexOf(i) < 0) s.goodsListState.selectedGoodIndex = [...s.goodsListState.selectedGoodIndex, i];
                } else {
                    if (s.goodsListState.selectedGoodIndex.indexOf(i) < 0) s.goodsListState.selectedGoodIndex = s.goodsListState.selectedGoodIndex.filter(e => e !== $index);
                }
            })
        })()
    }, [controller])

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

    return {
        columnWidth: 45,
        columnTitle,
        onSelect,
        selectedRowKeys,
        getCheckboxProps: (record: any) => {
            // console.log('===> getCheckboxProps', record, record.level)
            return {
                disabled: record.level > 0,
                style: record.level > 0 ? { display: 'none' } : undefined
            }
        },
    }
}