import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as API from '../apis/quickSearch';
import {
    WeaForm
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';
import { WeaLocaleProvider } from 'ecCom';

const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;
const formatMultiLang = WeaLocaleProvider.formatMultiLang;


export class QuickSearchStore {

    @observable quickSearchForm = new WeaForm();
    @observable qcSelectedRowKeys = [];
    @observable quickSearchData = {
        datas: [],//表格记录record
        setting: {},
        options: [],
    };
    @observable quickSearchDetailVisable = false;
    @observable qcData = { qcType: '', cid: '', fieldid: '' };//快速查询明细type
    @observable quickSearchDetailData = [];
    @observable precision = 0;
    @observable qcDetailRowKeys = [];
    quickSearchRefObj = {};

    /**** ==============quickSearchForm ===============*/
    @observable quickSearchFields = [
        {
            domkey: ['isquicksearch'],
            conditionType: 'SWITCH',
            label: getLabel('547592', "快捷搜索条件"),
            labelcol: 5,
            fieldcol: 19,
            viewAttr: 3,
        }
    ]


    @action("初始化页面") initList = () => {
        this.quickSearchForm.initFormFields([{ items: this.quickSearchFields.slice() }]);
        this.getQuickSearchInfo();
    }

    @action clearQuciSearchData = () => {
        this.quickSearchData.setting = {};
        this.quickSearchData.datas = [];
        this.quickSearchData.options = [];
    }

    @action("数据获取") getQuickSearchInfo = () => {
        this.clearQuciSearchData();
        API.getQuickSearchInfo().then(action(({ code, data, msg }) => {
            if (code == 200) {
                this.quickSearchData.setting = data.setting;
                this.quickSearchData.datas = data.datas.map(d => {
                    return { ...d, visible: false }
                });
                this.quickSearchData.groupidOptions = data.groupidOptions;
                this.quickSearchData.options = data.options;
                this.quickSearchForm.updateFields({
                    isquicksearch: data.setting.isquicksearch,
                });
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        }));
    }



    @action("保存页面") saveQuickSearchInfo = () => {
        let datas = this.quickSearchData.datas;
        datas.map((data, index) => {
            data.orderid = index
        });

        let formparm = this.quickSearchForm.getFormParams();
        if (!this.checkQuickSearchData()) {
            message.error(getLabel('547628', "条件字段不能重复"));
            return;
        }
        let id = this.quickSearchData.setting.id;
        if (id == null) {
            return;
        }
        API.saveQuickSearchInfo({
            id: id,
            data: JSON.stringify(datas), ...formparm
        }).then(({ code, data, msg }) => {
            if (code == 200) {
                message.success(getLabel(83551, '保存成功!'));
                this.qcSelectedRowKeys = [];
                this.getQuickSearchInfo();
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        });
        this.quickSearchData.setting.id = null;
    }


    checkQuickSearchData = () => {
        let map = new Map();
        let result = true;
        this.quickSearchData.datas.map(data => {
            if (map.has(data.fieldid)) {
                result = false;
            }
            map.set(data.fieldid, data.customname);
        });
        return result;
    }

    @action setQuickSearchData = (datas) => {
        this.quickSearchData.datas = datas;
    }

    /**============================quickdetail===================== */
    @action("设置") openQuickSearchDetail = (record) => {
        this.quickSearchDetailVisable = true;
        this.qcData.qcType = record.type;
        this.qcData.cid = record.cid;
        this.qcData.fieldid = record.fieldid;
        this.getQuickSearchDetailInfo(record.cid);
    }

    @action getQuickSearchDetailInfo = (cid) => {
        API.getQuickSearchDetailInfo({ cid: cid }).then(({ code, data, msg }) => {
            if (code == 200) {
                this.quickSearchDetailData = data.datas;
                this.precision = data.precision;
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        });
    }
    @action("明细保存") saveQuickSearchDetailInfo = () => {
        let datas = this.quickSearchDetailData;
        if (!this.checkQuickSearchDetailData()) {
            return;
        }
        API.saveQuickSearchDetailInfo({
            cid: this.qcData.cid,
            data: JSON.stringify(datas)
        }).then(({ code, data, msg }) => {
            if (code == 200) {
                message.success(getLabel(83551, '保存成功!'));
                this.qcDetailRowKeys = [];
                this.getQuickSearchDetailInfo(this.qcData.cid);
            } else {
                message.error(`[${getLabel(127353, "错误") + code}]:${msg}`);
            }
        });
    }

    @action setQuickSearchDetailData = (datas) => {
        this.quickSearchDetailData = datas;
    }

    checkQuickSearchDetailData = () => {
        let result = true;
        for (let i = 0; i < this.quickSearchDetailData.length; i++) {
            let data = this.quickSearchDetailData[i];
            if (_.trim(formatMultiLang(data.customname)) == '') {
                this.quickSearchRefObj[`error_customname_${i}`].showError();
                result = false;
                break;
            }
            let minnum = data.minnum + '';
            let maxnum = data.maxnum + '';
            if ((_.trim(formatMultiLang(minnum)) == '' || minnum == undefined) && (_.trim(formatMultiLang(maxnum)) == '' || maxnum == undefined)) {
                this.quickSearchRefObj[`error_condition_${i}`].showError();
                result = false;
                return;
            }
            if ((minnum != '' && minnum != undefined)
                && (maxnum != '' && maxnum != undefined)
                && (parseFloat(minnum) > parseFloat(maxnum))) {
                message.error(getLabel('547629', "请填写正确的区间"));
                result = false;
                return;
            }
        }
        return result;
    }


    /**===========================buttons=========================== */
    @observable rightMenu = [
        {
            "isBatch": "0",
            "isTop": "1",
            "menuFun": "save",
            "menuIcon": "icon-coms-Preservation",
            "menuName": getLabel('547360', "保存"),
            "type": "BTN_save"
        },
    ];
    @observable topMenu = [
        {
            "isBatch": "1",
            "isTop": "1",
            "menuFun": "save",
            "menuIcon": "icon-coms-Preservation",
            "menuName": getLabel('547360', "保存"),
            "type": "BTN_save"
        }
    ]

}