import {
    observable,
    action,
    computed,
} from 'mobx';
import * as API from '../apis/query.js';
import { WeaTableNew } from 'comsMobx';
import { TopStore, SearchStore, TableListStore } from "../public/valhalla/stores/index.js";
import {
    jumpToHrmCard,
    jumpToHrmSubCompany,
    jumpToHrmDept,
    jumpToHrmPost
} from '../util/pulic-func';
import * as PublicFunc from '../util/pulic-func';
const { TableStore } = WeaTableNew;

export class HrmQuery {
    @observable topStore = new TopStore(this);
    @observable searchStore = new SearchStore(this);
    @observable tableListStore = new TableListStore(this);
    @computed get rightMenuStates() {
        const { tableStore: { selectedRowKeys } } = this.tableListStore
        return [selectedRowKeys.length == 0]
    }

    init = () => {
        this.topStore.init({
            api:API.getRightMenu,
            title: "查询人员",
            hideMenu:['reSearch','doExportSetting','doLog','doSortSetting']
        });

        this.searchStore.init({
            api:API.getHrmSearchCondition,
            advanceHeight:200,
            linkageFieldName:'resourcename',
            _handleSearch: this.handleSearch
        });

        this.tableListStore.init({
            api: API.getSearchList,
            _reRenderColumns: this.reRenderColumns,
            _onOperatesClick: this.onOperatesClick,
        });

        this.topStore.fetchRightMenu()

        this.searchStore.fetchSearchForm({
            superid:this.superid,
            mouldid: 'default_3',
            tabkey:'default_4'
        }).then(() => {
            this.getSearchList();
        })

    }

    reRenderColumns = (columns) => {
        columns.forEach(c => {
            if (c.dataIndex == 'lastname') {
                c.render = function(text, record) {
                    return <a onClick={()=>jumpToHrmCard(record.id)}>{record.lastname}</a>
                }
            }
            if (c.dataIndex == 'managerid') {
                c.render = function(text, record) {
                    return <a onClick={()=>jumpToHrmCard(record.managerid)}>{record.manageridspan}</a>
                }
            }
            if (c.dataIndex == 'subcompanyid1') {
                c.render = function(text, record) {
                    return <a onClick={()=>jumpToHrmSubCompany(record.subcompanyid1)}>{record.subcompanyid1span}</a>
                }
            }
            if (c.dataIndex == 'departmentid') {
                c.render = function(text, record) {
                    return <a onClick={()=>jumpToHrmDept(record.departmentid)}>{record.departmentidspan}</a>
                }
            }
            if (c.dataIndex == 'jobtitle') {
                c.render = function(text, record) {
                    return <a onClick={()=>jumpToHrmPost(record.jobtitle)}>{record.jobtitlespan}</a>
                }
            }
        })
    }

    onOperatesClick = (record, rowIndex, operate) => {
        const { index } = operate;

        const fn = ['sendEmessage', 'sendMail', 'sendSmsMessage', 'doAddWorkPlanByHrm', 'jsHrmResourceSystemView'];
        PublicFunc[fn[parseInt(index)]](record.id);
    }

    handleSearch = () => {
        this.getSearchList();
    }

    doSearch = () => {
        this.handleSearch()
    }

    sendEmessage = () => {
        const { tableStore:{selectedRowKeys} } = this.tableListStore
        PublicFunc.sendEmessage(selectedRowKeys.toString())
    }

    definedColumn = () => {
        const { tableStore } = this.tableListStore;
        tableStore.setColSetVisible(true);
        tableStore.tableColSet(true);
    }

    exportExcel = () => {
         const { tableStore:{exportAll} } = this.tableListStore;
         exportAll()
    }

    getSearchList = (v) => {
        const {form} = this.searchStore
        const params = {
            superid: this.superid,
            searchname: v,
            tabkey: "default_4",
            ...form.getFormParams()
        }
        this.tableListStore.fetchTableList(params);
    }

    setSuperid = (id) => {
        this.superid = id;
    }
    
}