import { action, observable } from "mobx";
import { WeaForm, WeaTableNew } from "comsMobx";
import { message } from "antd";
import * as API from "../apis/officeManage";
import _ from "lodash";

const { TableStore } = WeaTableNew;

export class OfficeManageStore {
  @observable tableStore = new TableStore();
  @observable topMenu = []; //顶部菜单
  @observable rightMenu = []; //右侧更多菜单
  @observable condition = []; //新增职务信息form数据
  @observable searchCondition = []; //高级搜索框form 数据
  @observable officeCondition = []; //新增职务分类form数据
  @observable isEdit = true;
  @observable isNew = true;
  @observable isPanelShow = false; //高级搜索面板
  @observable form2 = new WeaForm(); //高级搜索渲染的表单
  @observable form = new WeaForm(); //新增编辑渲染的表单
  @observable form1 = new WeaForm(); //职务分类渲染的表单
  @observable postInfoName = "";
  @observable officeClassifyId = ""; //选中职务分类的id
  @observable conditionNum = 2;
  @observable ids = ""; //选择行id
  @observable searchConditionLoading = false;
  @observable nEdialogTitle = "";
  @observable officeVisible = {};
  @observable visible = false;
  @observable dialogLoading = false;
  @observable date = "";
  @observable hasRight = '';

  @action
  setOfficeClassifyId(id) {
    this.officeClassifyId = id;
  }

  @action
  getHasRight() {
    API.getHasRight().then(res => {
      const { code, data, msg } = res;
      if (code === 200) {
        const { rightMenu, topMenu } = data;
        this.topMenu = topMenu;
        this.rightMenu = rightMenu;
      } else {
        message.error(msg);
      }
    });
  }

  @action("高级搜索表单渲染")
  getSearchCondition() {
    this.setScLoadingStatus(true);
    API.getSearchCondition().then(res => {
      this.setScLoadingStatus(false);
      const { code, data, msg } = res;
      if (code === 200) {
        res.data.conditions && this.setSearchCondition(data.conditions);
        res.data.conditions && this.form2.initFormFields(data.conditions);
      } else {
        message.error(res.msg);
      }
    });
  }

  @action("获取新增/编辑表单")
  getPostInfoForm(params) {
    this.setDialogLoadingStatus(true);
    API.getPostInfoForm(params).then(res => {
      this.setDialogLoadingStatus(false);
      const { code, data, msg } = res;
      if (code === 200) {
        data.condition && this.setCondition(data.condition);
        data.condition && this.form.initFormFields(data.condition);
      } else {
        message.error(res.msg);
      }
    });
  }

  @action("获取表格数据")
  getPostInfoTable(postId,isOnChange =  false) {
    let params;
    const {current} = this.tableStore;
    if (_.isEmpty(this.form2.getFormParams())) {
      params = {
        postId,
        postInfoName: this.postInfoName
      };
    } else {
      params = {
        ...this.form2.getFormParams(),
      };
    }
    API.getPostInfoTable(params).then(res => {
      if (res.code === 200) {
        this.setHasRight(res.data.hasRight);
        if(res.data.hasRight) {
          isOnChange ? this.tableStore.getDatas(res.data.datas, current) : this.tableStore.getDatas(res.data.datas,1);
        }
      } else {
        message.error(res.msg);
      }
    });
  }

  @action("获取左侧树")
  getTreeData() {
    return API.getTreeData();
  }

  @action("保存职务分类")
  savePost(payload) {
    return API.savePost(payload).then(response => {
      return response.json();
    });
  }

  @action("编辑职务分类")
  updatePost(payload) {
    return API.updatePost(payload).then(response => {
      return response.json();
    });
  }

  @action("删除职务分类")
  deleteByIds(payload) {
    return API.deleteByIds(payload).then(response => {
      return response.json();
    });
  }

  @action("删除职务信息")
  deletePostinfoByIds(payload) {
    return API.deletePostinfoByIds(payload).then(response => {
      return response.json();
    });
  }

  @action("获取职务分类表单")
  getPostForm(id) {
    this.setDialogLoadingStatus(true);
    API.getPostForm({ id }).then(res => {
      this.setDialogLoadingStatus(false);
      const { code, data, msg } = res;
      if (code === 200) {
        data.condition && this.setOfficeCondition(data.condition);
        data.condition && this.form1.initFormFields(data.condition);
      } else {
        message.error(res.msg);
      }
    });
  }

  @action("展开关闭搜索面板")
  setPanelStatus(bool) {
    this.isPanelShow = bool;
    bool && this.getSearchCondition();
    if (!bool) {
      this.searchConditionLoading = false;
    }
  }

  @action("新增职务信息")
  savePostInfo(payload) {
    return API.savePostInfo(payload).then(response => {
      return response.json();
    });
  }

  @action("更新职务信息")
  updatePostInfo(payload) {
    return API.updatePostInfo(payload).then(response => {
      return response.json();
    });
  }

  @action("更新禁用标识 ")
  updateForbiddenTagById(payload) {
    return API.updateForbiddenTagById(payload).then(response => {
      return response.json();
    });
  }

  setPostInfoName(val) {
    this.postInfoName = val;
  }

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

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

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

  setOfficeCondition(condition) {
    this.officeCondition = condition;
  }

  setVisible(bool) {
    this.visible = bool;
    this.formReset();
    if (!bool) this.setDialogLoadingStatus(false);
  }

  setOfficeVisible({ bool, type }) {
    this.officeVisible = { bool, type };
    this.officeFormReset();
    if (!bool) this.setDialogLoadingStatus(false);
  }

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

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

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

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

  officeFormReset() {
    this.form1 = new WeaForm();
  }

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