import {
  observable,
  action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/company'; // 引入API接口文件
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 CompanyStore {
  @observable topMenu = []
  @observable rightMenu = [];
  @observable dataSource = [];
  @observable columns = [];
  @observable deptDataSource = [];
  @observable deptColumns = [];
  @observable tableStore = new TableStore();
  @observable loading = true;
  @observable dialogLoading = true;
  @observable isEdit = true;
  @observable nEdialogTitle = '';
  @observable searchCondition = [];
  @observable condition = [];
  @observable isPanelShow = false; //高级搜索面板
  @observable form = new WeaForm();
  @observable form1 = new WeaForm(); //新增主表表单
  @observable companyName = '';
  @observable conditionNum = 4;
  @observable ids = ''; //选择行id
  @observable id = ''; //页面跳转参数id
  @observable searchConditionLoading = true;
  @observable visible = false;
  @observable newVisible = false; //新增弹窗
  @observable companyId = '';
  @observable selectedRowKeys = [];
  @observable date = '';
  @observable init = true; //是否首次加载
  @observable total = '';
  @observable current = 1;
  @observable pageSize = 10;
  @observable sortParams = [];
  @observable hasRight = '';
  saveAndSetting = false;

  @observable form2 = new WeaForm(); //转移表单
  @observable transferCondition = [];




  @action("列表树") getTableInfo() {
    this.setLoading(true);
    let params = {
      current: this.current,
      pageSize: this.pageSize,
      sortParams: this.sortParams
    }
    if (this.isEmptyObject(this.form.getFormParams())) {
      params = {
        ...params,
        subCompanyDesc: this.companyName
      };
    } else {
      params = {
        ...params,
        ...this.form.getFormParams(),
      };
    }
    Api.getSearchList(params).then(response => {
      return response.json()
    }).then(res => {
      if (res.code === 200) {
        this.setHasRight(res.data.hasRight);
        res.data.pageInfo.columns && this.setColumns(res.data.pageInfo.columns);
        this.setTotal(res.data.pageInfo.total);
        res.data.pageInfo.list && this.setDataSource(res.data.pageInfo.list);
        this.setLoading(false);
        this.setInit(false);
      } else {
        message.warning(res.msg);
      }
    }).catch(error => {
      message.warning(error.msg);
    })
  }

  @action("联查部门") getDeptTable(id) {
    let params = {
      parentComp: id
    }
    Api.getDeptListByPid(params).then(response => {
      return response.json()
    }).then(res => {
      if (res.code === 200) {
        res.data.list && this.setDeptDataSource(res.data.list);
        res.data.columns && this.setDeptColumns(res.data.columns);
        this.setDialogLoadingStatus(false);
      } else {
        message.warning(res.msg);
      }
    }).catch(error => {
      message.warning(error.msg);
    })
  }


  @action("删除") delete() {
    let params = {
      ids: this.ids
    };
    Api.deleteTableData(params).then(response => {
      return response.json()
    }).then(data => {
      if (data.code === 200) {
        message.success(i18n.message.deleteSuccess());
        this.setSelectedRowKeys([]);
        this.getTableInfo();
      } else {
        message.warning(data.msg);
      }
    }).catch(error => {
      message.warning(error.msg);
    })
  }

  updateForbiddenTag(checked, id) {
    let params = {
      canceled: checked,
      id: id
    }
    Api.updateForbiddenTag(params).then(response => {
      return response.json()
    }).then(data => {
      if (data.code === 200) {
        message.success(data.msg);
        this.getTableInfo();
      } else {
        message.warning(data.msg);
      }
    }).catch(error => {
      message.warning(error.msg);
    })
  }

  getSearchCondition() {
    this.setScLoadingStatus(true);
    Api.getAdvanceSearchCondition().then(res => {
      if (res.code === 200) {
        this.setScLoadingStatus(false);
        res.data.conditions && this.setSearchCondition(res.data.conditions);
        res.data.conditions && this.form.initFormFields(res.data.conditions);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })
  }


  getCompanyForm() {
    let params = {
      addType: 'normal',
      viewattr: 2
    };
    this.setDialogLoadingStatus(true);
    Api.getCompanyForm(params).then(res => {
      if (res.code === 200) {
        this.setDialogLoadingStatus(false);
        res.data.condition && this.setCondition(res.data.condition);
        res.data.condition && this.form1.initFormFields(res.data.condition);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })

  }

  getMoveForm() {
    Api.getMoveForm().then(res => {
      if (res.code === 200) {
        res.data && this.setTransferCondition(res.data);
        res.data && this.form2.initFormFields(res.data);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })
  }

  @action transfer(id) {
    let params = {
      id: id,
      ...this.form2.getFormParams(),
    };
    Api.move(params).then(response => {
      return response.json()
    }).then(data => {
      if (data.code === 200) {
        message.success(data.msg);
        this.getTableInfo();
      } else {
        message.warning(data.msg);
      }
    }).catch(error => {
      message.warning(error.msg);
    })
  }

  save() {
    let params = {
      ...this.form1.getFormParams()
    };
    this.form1.validateForm().then(f => {
      if (f.isValid) {
        Api.add(params).then(data => {
          if (data.code === 200) {
            message.success(data.msg);
            this.getTableInfo();
            this.setNewVisible(false);
            this.saveAndSetting && window.open(`/spa/organization/static/index.html#/main/organization/companyExtend/${data.data}`, "_blank")
          } else {
            message.warning(data.msg);
          }
        }).catch(error => {
          message.warning(error.msg);
        })
      } else {
        f.showErrors();
        this.setDate(new Date());
      }
    });
  }

  @action getHasRight() {
    Api.getHasRight().then(res => {
      if (res.code === 200) {
        res.data.rightMenu && this.setRightMenu(res.data.rightMenu);
        res.data.topMenu && this.setTopMenu(res.data.topMenu);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })
  }

  @action("另存为版本") version(id) {
    Api.version({id:id}).then(res => {
      if (res.code === 200) {
        message.success(res.msg);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })
  }

  updateFields(val) {
    this.form.updateFields({
      subCompanyDesc: {
        value: val
      }
    });
  }

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

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

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

  setCompanyName(val) {
    this.companyName = val;
  }

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


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

  setId(id) {
    this.id = id;
  }

  scLoadingReset() {
    this.searchConditionLoading = true;
  }


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


  setVisible(bool) {
    this.visible = bool;
  }

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


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

  setCompanyId(companyId) {
    this.companyId = companyId;
  }

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

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

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

  setDataSource(dataSource) {
    this.dataSource = dataSource;
  }

  setDeptDataSource(deptDataSource) {
    this.deptDataSource = deptDataSource;
  }

  setColumns(columns) {
    this.columns = columns
  }

  setDeptColumns(deptColumns) {
    this.deptColumns = deptColumns
  }

  setSelectedRowKeys(selectedRowKeys) {
    this.selectedRowKeys = selectedRowKeys;
  }

  setLoading(bool) {
    this.loading = bool;
  }

  setNeDialogTitle(title) {
    this.nEdialogTitle = title
  }

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

  setNewVisible(bool) {
    this.formReset();
    this.newVisible = bool;
  }

  setTotal(total) {
    this.total = total;
  }

  setCurrent(current) {
    this.current = current;
  }

  setPageSize(pageSize) {
    this.pageSize = pageSize;
  }

  setInit(bool) {
    this.init = bool;
  }

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

  setSortParams(sorter) {
    this.sortParams = [];
    sorter.order && this.sortParams.push({
      orderkey: sorter.field,
      sortOrder: sorter.order
    })
  }

  setSaveAndSetting(bool) {
    this.saveAndSetting = bool;
  }

  setTransferCondition(transferCondition) {
    this.transferCondition = transferCondition;
  }



}