/**
 * @author jh
 */

import { observable, action } from 'mobx';
import { message, Button, Modal } from 'antd';

import { ListStore } from './listStore';
import * as API from '../../apis/userDefaultSettingApi';

import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export default class UserDefaultSettingStore extends ListStore {
    @observable showColl = false;
    @observable selectedRows =[];
    @observable conditions = [];
    @observable visiableAdd = false;// 新建短语的弹窗的显隐
    date = new Date();
    weaworkflow_userdefault_store = null;

    constructor() {
    	super();
        this.getConditionInfo = this.getConditionInfo.bind(this);
        this.onSave = this.onSave.bind(this);
        this.getRightMenu = this.getRightMenu.bind(this);
        this.onRightMenuClick = this.onRightMenuClick.bind(this);
        this.getButtons = this.getButtons.bind(this);
        this.closeColl = this.closeColl.bind(this);
        this.updateSelectedRows = this.updateSelectedRows.bind(this);
        this.onDel = this.onDel.bind(this);
        this.changeDisplay = this.changeDisplay.bind(this);
    }

    /**
     * 控制显隐
     */
    @action
    changeDisplay(para) {
        API.doChangeStatus(para).then((res) => {
            const { success = '0', msg = '' } = res;
            message.success(msg);
            if (success == 1) {
                this.getConditionInfo();
            }
        });
    }

    /**
     * 获取页面Condition
     * @param para
     */
    @action
    getConditionInfo(para = {}) {
        API.getConditionInfo(para).then((reVal) => {
            this.resetTable();
            this.form.setCondition([reVal.conditions[0]]);
            this.conditions = reVal.conditions;
            this.tableStore.getDatas(reVal.tableSession, para.current || 1);
            this.dataKey = reVal.tableSession;
        });
    }

    /**
     * //设置新建短语弹窗的显隐
     * @param  selectedRow
     */

    @action
    setVisiableAdd = (val) => {
        this.visiableAdd = val;
    }
    /**
     * 更新table选中行
     * @param selectedRow
     */
    @action
    updateSelectedRows(selectedRow) {
        this.selectedRows = selectedRow;
    }

    @action
    set_weaworkflow_userdefault_store = (val) => {
        this.weaworkflow_userdefault_store = val;
    }

    /**
     * 删除
     * @param id
     */
    onDel(phraseId) {
        phraseId && this.selectedRows.push(phraseId);
        const para = {
            ids: this.selectedRows.join(','),
        };
        const thisCxt = this;
        Modal.confirm({
            title: getLabel(131329, '信息确认'),
            content: this.selectedRows.length > 1 ? getLabel(33435, '  确定要删除选中的记录吗？ ') : getLabel(15097, ' 确定要删除吗？'),
            onOk() {
                API.deleteData(para).then((reVal) => {
                    message.success(reVal.msg);
                    thisCxt.getConditionInfo();
                    // 清空已选行
                    thisCxt.selectedRows = [];
                });
            },
            onCancel() {},
            okText: getLabel(33703, '确认'),
            cancelText: getLabel(31129, '取消'),
        });
    }

    /**
     * 保存显示设置
     */
    @action
    onSave() {
        const para = this.form.getFormParams();
        this.form.validateForm().then((f) => {
            if (f.isValid) {
                API.doSaveUserDefault(para).then((reVal) => {
                    message.success(reVal.msg);
                });
            } else {
                f.showErrors();
                this.date = new Date();
            }
        });
    }

    @action
    getButtons() {
        let btns = [];
        btns.push(<Button type="primary" onClick={this.onSave}>{getLabel(30986, '保存')}</Button>);
        return btns;
    }

    // 右键菜单项
    @action
    getRightMenu() {
        return [{
            key: 'save',
            icon: <i className="icon-workflow-Right-menu-Preservation" />,
            content: getLabel(30986, '保存'),
            onClick: () => {
                this.onSave();
            },
        }, {
            key: 'displayCol',
            icon: <i className="icon-workflow-Right-menu-Custom" />,
            content: getLabel(32535, '显示列定制'),
            onClick: () => {
                this.tableStore.setColSetVisible(true);
                this.tableStore.tableColSet(true);
            },
        }, {
            key: 'coll',
            icon: <i className="icon-coms-Collection" />,
            content: getLabel(28111, '收藏'),
            onClick: (key) => {
                this.showColl = true;
            },
        }];
    }

    @action
    onRightMenuClick(key) {
        switch (key) {
            case '0':
                this.onSave();
                break;
            case '1':
                this.tableStore.setColSetVisible(true);
                this.tableStore.tableColSet(true);
                break;
            case '2':
                this.showColl = true;
                break;
        }
    }
    @action
    closeColl() {
        this.showColl = false;
    }
}
