import * as formaInvoiceService from '../../../services/cooperate/finance/formaInvoice';
import { numberUtils } from '@utils';
import { message } from 'antd';

export default {
    namespace: 'invoiceModal',

    state: {
        formInfo: {},
        itemList: [],
        total: 0,
    },
    effects: {
        * query({ payload }, { call, put, select }) {
            const { itemCode, taxRate, accountId, formData } = yield select(({ formaInvoice }) => formaInvoice);
            if (payload) {
                payload.itemCode = itemCode;
                payload.taxRate = taxRate;
                payload.accountId = accountId;
            }
            const result = yield call(formaInvoiceService.queryList, payload);
            if (result.success) {
                // 对账单list
                const rows = Object.assign([], result.data.rows);
                rows.forEach(m => {
                  m.invNetAmt = m.invNetAmt ? m.invNetAmt : m.netAmt;
                  m.invTaxAmt = m.invTaxAmt ? m.invTaxAmt : m.taxAmt;
                  m.invAmt = m.invNetAmt + m.invTaxAmt;
                });
                yield put({
                    type: 'updateState',
                    payload: {
                        formInfo: formData,
                        itemList: rows,
                        total: result.data.total,
                    },
                });
            } else {
                throw result.message;
            }
        },

        * cancelRows({ payload }, { call, put, select }) {
            const { index, row } = payload;
            yield put({ type: `toggleEditable`, payload: { index } });
            const { itemList } = yield select(({ invoiceModal }) => invoiceModal);
            const data = yield call(formaInvoiceService.getItem, row.id);

            if (data.success) {
                const newList = itemList.slice(0);
                newList[index] = data.data;

                yield put({
                    type: 'updateState',
                    payload: {
                        itemList: newList
                    }
                });

            }
        },

        * updateRows({ payload }, { call, put, select }) {
            const { itemList } = yield select(({ invoiceModal }) => invoiceModal);
            const { formData } = yield select(({ formaInvoice }) => formaInvoice);
            const index = payload.index;
            const row = itemList[index];
            //构造发票ID，对账单ID
            row['accountId']=formData.checkId;
            row['invId']=formData.id;
            if ((!row.invNetAmt || row.invNetAmt === 0) && row.invTaxAmt > 0) {
                message.error('实际开票(未税)为0时，税额不能大于0');
                return;
            }
            const data = yield call(formaInvoiceService.updateRows, row);
            if (data.success) {
                // yield put({
                //     type: 'formaInvoice/updateState',
                //     payload: {
                //         formData: { ...formData, actTaxAmt: parseFloat(data.data.actTaxAmt).toFixed(2), actNetAmt: parseFloat(data.data.actNetAmt).toFixed(2), actInvAmt: parseFloat(data.data.actInvAmt).toFixed(2) }
                //     }
                // });
                data.data.invAmt = parseFloat(data.data.invAmt).toFixed(2);
                data.data.invAmtStr = numberUtils.round_half_up_format(data.data.invAmt, 2);
                data.data.taxAmt = parseFloat(data.data.taxAmt).toFixed(2);
                data.data.taxAmtStr = numberUtils.round_half_up_format(data.data.taxAmt, 2);
                data.data.netAmt = parseFloat(data.data.netAmt).toFixed(2);
                data.data.netAmtStr = numberUtils.round_half_up_format(data.data.netAmt, 2);
                data.data.actTaxAmt = parseFloat(data.data.actTaxAmt).toFixed(2);
                data.data.actTaxAmtStr = numberUtils.round_half_up_format(data.data.actTaxAmt, 2);
                data.data.actNetAmt = parseFloat(data.data.actNetAmt).toFixed(2);
                data.data.actNetAmtStr = numberUtils.round_half_up_format(data.data.actNetAmt, 2);
                data.data.actInvAmt = parseFloat(data.data.actInvAmt).toFixed(2);
                data.data.actInvAmtStr = numberUtils.round_half_up_format(data.data.actInvAmt, 2);
                const formInfo = formaInvoiceService.countFormGapAmt(data.data);
                yield put({ type: 'updateState' , payload: { formInfo: formInfo }});
                yield put({ type: `toggleEditable`, payload: { index } });
            } else {
                throw data;
            }
        },
    },

    reducers: {
        updateState(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },

        toggleEditable(state, action) {
            const { index } = action.payload;
            const newList = state.itemList.slice(0);

            if (newList[index].editable === true) {  // editable === true
                newList[index].editable = false;
            } else if (newList[index].editable === false) {
                newList[index].editable = true;
            } else {
                newList[index].editable = true;
            }

            return {
                ...state,
                itemList: newList,
            };
        },

        updateCell(state, action) {
            const { index, dataIndex, newValue } = action.payload;
            const newList = state.itemList.slice(0);
            newList[index][dataIndex] = newValue ? newValue : 0;

            if (dataIndex === 'invNetAmt') {
              newList[index]['invAmt'] = newList[index]['invTaxAmt'] + newList[index][dataIndex];
            }
            if (dataIndex === 'invTaxAmt') {
              newList[index]['invAmt'] = newList[index]['invNetAmt'] + newList[index][dataIndex];
            }

            return {
                ...state,
                itemList: newList,
            };
        },
    },

};
