import { action, observable } from 'mobx';
import { WeaForm, WeaTableNew, WeaLogView } from 'comsMobx';
import * as API from "../../apis/Base.js";

const { TableStore } = WeaTableNew;

const { LogStore } = WeaLogView;

export default class ListStore {
    //列表
    @observable tableStore = new TableStore();
    //日志组件
    @observable logStore = new LogStore();
    //查询表单
    @observable searchForm = new WeaForm();

    @observable status = {

        //API地址
        apiUrl: {},
        //显示高级搜索
        showSearchAd: false,
        //高级搜索表单
        conditioninfo: [],
        //列表菜单
        menuinfo: [],
        //收藏信息
        collectinfo: {},
        //标题信息
        topinfo: {},
        //按钮信息
        buttoninfo: [],
        //列信息
        colums: [],

        //日志相关参数
        logType: '',
        logSmallType: '',
        logShow: false,
        logId: '',

        // 选择行信息
        rowSelectIds: [],

        rightLoading: true
    }


    @action
    //列表页面初始化
    doInit = () => {
        this.tableStore = new TableStore();
        this.getDatas(true);
        this.getCondition();
        this.getMenu();
    }

    setState = (params = {}) => {
        Object.assign(this.status, params);
    }

    //列表数据处理
    rowSelection = {
        onSelect: (record, selected, selectedRows) => {
            this.status = {
                ...this.status,
                selectedRows: selectedRows,
                selectedIds: this.getIds(selectedRows)
            }
        },
        onSelectAll: (record, selectedRows, changeRows) => {
            this.status = {
                ...this.status,
                selectedRows: selectedRows,
                selectedIds: this.getIds(selectedRows)
            }
        }
    }

    //获取右键菜单
    getMenu = () => {
        API.getRightMenu(this.status.apiUrl).then(result => {
            if (result.status) {
                this.status = {
                    ...this.status,
                    menuinfo: result.data.rightMenus,
                    collectinfo: result.data.collectParams,
                    topinfo: result.data.top,
                    buttoninfo: result.data.button
                };
            }
        });
    }

    //获取查询条件
    getCondition = () => {
        API.getCondition(this.status.apiUrl).then(result => {
            if (result.status) {
                this.searchForm.initFormFields(result.data.condition);
                //this.searchForm.init(result.data.condition);
                this.status = { ...this.status, conditioninfo: result.data.condition };
            }
        });
    }

    //显示高级搜索
    setShowSearchAd = (showSearchAd) => {
        this.status = { ...this.status, showSearchAd: showSearchAd }
    }

    //获取列表数据
    getDatas = (isOnChange) => {
        let params = this.searchForm && this.searchForm.getFormParams() || {};
        API.getDatas(this.status.apiUrl, params).then(result => {
            if (result.status) {
                this.status = { ...this.status, rightLoading: false };
                if (result.data.sessionkey) {
                    isOnChange ? this.tableStore.getDatas(result.data.sessionkey, 1) : this.tableStore.getDatas(result.data.sessionkey);
                    this.status = { ...this.status, logType: result.data.logType, logSmallType: result.data.logSmallType }
                }
            }
        });
    }
};