import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/officeaddress';
import {
    WeaForm
} from 'comsMobx';
import {
    WeaTableNew
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';

const toJS = mobx.toJS;
const {
    TableStore
} = WeaTableNew;

export class HrmOfficeAddress {
    @observable nEdialogTitle = '';
    @observable isPanelShow = false;
    @observable conditionNum = 2;
    @observable condition = [];
    @observable searchCondition = [];
    @observable form = new WeaForm();
    @observable form1 = new WeaForm();
    @observable form2 = new WeaForm();
    @observable tableStore = new TableStore();
    @observable visible = false;
    @observable height = 510;
    @observable ids = '';
    @observable excelfile = '';
    @observable cityId = '';
    @observable countryid = '';
    @observable locationid = '';
    @observable isNew = true;
    @observable hasRight = [];
    @observable topMenu = []
    @observable rightMenu = [];
    @observable dialogLoading = true;
    @observable searchConditionLoading = true;
    @observable isImportResultFinished = false;
    @observable date = '';
    @observable importResultdatas = {};
    @observable pid = '';
    @observable locationName = '';
    @observable isEdit = true;

    @action
    getTableInfo() {
        let params;
        this.tableStore = new TableStore();
        if (this.isEmptyObject(this.form2.getFormParams())) {
            params = { ...this.form2.getFormParams(),
                name: this.locationName
            };
        } else {
            params = { ...this.form2.getFormParams()
            };
        }

        Api.getSearchList(params).then(res => {
            if (res.api_status) {
                res.sessionkey && this.tableStore.getDatas(res.sessionkey, 1);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    getHasRight() {
        Api.getHasRight().then(res => {
            if (res.api_status) {
                this.setTopMenu(res.topMenu);
                this.setRightMenu(res.rightMenu);
                this.setEditStatus(res.isEdit);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    getSearchCondition() {
        Api.getAdvanceSearchCondition().then(res => {
            if (res.api_status) {
                this.setScLoadingStatus(false);
                res.conditions && this.setSearchCondition(res.conditions);
                res.conditions && this.form2.initFormFields(res.conditions);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    getForm() {
        let params = this.isNew ? {} : {
            locationid: this.locationid
        }
        this.setDialogLoadingStatus(true);
        Api.getLocationForm(params).then(res => {
            if (res.api_status) {
                this.setDialogLoadingStatus(false);
                res.condition && this.setCondition(res.condition);
                res.condition && this.form.initFormFields(res.condition);
                res.countryid && this.setCountryId(res.countryid);
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })

    }

    save() {
        let params = { ...this.form.getFormParams(),
            locationcity: this.cityId,
            countryid: this.countryid
        };

        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.add(params).then(res => {
                    if (res.api_status) {
                        if (res.message) {
                            if (res.sign == '1') {
                                message.success(res.message);
                            } else {
                                message.warning(res.message);
                            }
                        }
                        this.getTableInfo();
                        this.setVisible(false);
                        this.cityIdReset();
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });

    }

    edit() {
        let params;
        if (this.cityId) {
            params = { ...this.form.getFormParams(),
                locationcity: this.cityId,
                countryid: this.countryid,
                id: this.locationid
            };
        } else {
            params = { ...this.form.getFormParams(),
                countryid: this.countryid,
                id: this.locationid
            };
        }


        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.edit(params).then(res => {
                    if (res.api_status) {
                        if (res.message) {
                            if (res.sign == '1') {
                                message.success(res.message);
                            } else {
                                message.warning(res.message);
                            }
                        }
                        this.getTableInfo();
                        this.setVisible(false);
                    } else {
                        message.warning(data.message);
                    }
                }, error => {
                    message.warning(error.message);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });

    }

    delete() {
        let params = {
            ids: this.ids
        };
        Api.deleteTableData(params).then(res => {
            if (res.api_status) {
                message.success(i18n.message.deleteSuccess());
                this.getTableInfo();
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })

    }

    getCountryDetail() {
        let params = {
            cityid: this.cityId
        };

        Api.getCountryDetail(params).then(res => {
            if (res.api_status) {
                res.countryid && this.setCountryId(res.countryid);
                this.form.updateFields({
                    countryname: {
                        value: res.countryname
                    }
                });
            } else {
                message.warning(data.message);
            }
        }, error => {
            message.warning(error.message);
        })
    }

    updateFields(val) {
        this.form2.updateFields({
            name: {
                value: val
            }
        });
    }

    setNeDialogTitle(title) {
        this.nEdialogTitle = title;
    }

    setPanelStatus(bool) {
        this.isPanelShow = bool;
        bool && this.getSearchCondition();
        if (!bool) {
            this.scLoadingReset();
            // this.conditionReset();
        }
    }

    isEmptyObject(obj) {
        for (let key in obj) {
            return false;
        }
        return true;
    }

    setCondition(condition) {
        this.condition = condition;
    }

    setConditionNum(num) {
        this.conditionNum = num;
    }

    setHeight(height) {
        this.height = height;
    }

    setVisible(bool) {
        this.visible = bool;
        this.formReset();
        !bool && this.dialogLoadingReset();
    }

    setVisibleForImportResult(bool) {
        this.visible3 = bool;
    }

    setIds(ids) {
        this.ids = ids;
    }

    setCityId(cityId) {
        this.cityId = cityId;
    }

    setCountryId(countryid) {
        this.countryid = countryid;
    }

    setLocationId(locationid) {
        this.locationid = locationid;
    }

    setIsNew(bool) {
        this.isNew = bool;
    }

    setHasRight(hasRight) {
        this.hasRight = hasRight;
    }

    setTopMenu(topMenu) {
        this.topMenu = topMenu;
    }

    setRightMenu(rightMenu) {
        this.rightMenu = rightMenu;
    }

    setDialogLoadingStatus(bool) {
        this.dialogLoading = bool;
    }

    setDate(date) {
        this.date = date;
    }

    setPid(id) {
        this.pid = id;
    }

    setScLoadingStatus(bool) {
        this.searchConditionLoading = bool;
    }

    setLocationName(locationName) {
        this.locationName = locationName;
    }

    setSearchCondition(condition) {
        this.searchCondition = condition;
    }

    setEditStatus(bool) {
        this.isEdit = bool;
    }

    conditionReset() {
        this.condition = [];
    }

    formReset() {
        this.form = new WeaForm();
    }

    dialogLoadingReset() {
        this.dialogLoading = true;
    }

    scLoadingReset() {
        this.searchConditionLoading = true;
    }

    cityIdReset() {
        this.cityId = '';
    }

    locationNameReset() {
        this.locationName = '';
    }

    mobxDatasReset() {
        this.locationNameReset();
        this.form2 = new WeaForm();
        this.setPanelStatus(false);
    }

}