import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaLogView, WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import * as Api from './../../apis/privateGroupSetingApi';
import { message, Modal, Button } from 'antd';

const { TableStore } = WeaTableNew;
const { LogStore } = WeaLogView;
const { getLabel } = WeaLocaleProvider;
const confirm = Modal.confirm;

class PrivateGroupStore {
  @observable dialogVisible = false;
  @observable tableStore = new TableStore();
  @observable selectedRowKeys = [];
  @observable logVisible = false;
  @observable logStore = new LogStore();
  @observable logType = '';
  @observable logSmallType = '';
  @observable isOdocPanelShow = false;

  @observable loading = false;

  @observable isCreat = true;
  @observable conditions = [];
  @observable receiveId = '';
  @observable clickRowKey = '';


  /* --编辑弹窗--*/
  @observable odocForm = new WeaForm();
  @observable odoConditionForm = new WeaForm();
  @observable creatDialogVisible = false;

  // 获取列表数据
  @action.bound getTableData () {
    const param = {
      ConditionType: '1',
      ...this.odoConditionForm.getFormParams(),
    };
    this.selectedRowKeys = [];
    Api.getTableData(param).then((data) => {
      if (data.sessionkey) {
        this.tableStore.getDatas(data.sessionkey);
      }
    });
  }

  // 行选中
  @action.bound rowSelection (selectRow) {
    this.selectedRowKeys = selectRow;
  }

  // 弹窗隐藏
  @action.bound dialogHide () {
    this.dialogVisible = false;
  }

  // 弹窗显示
  @action.bound dialogShow () {
    this.dialogVisible = true;
  }

  // 编辑/新建 弹窗隐藏
  @action.bound creatDialogHide (save='') {
    this.creatDialogVisible = false;
    if (save=='save') {
      this.selectedRowKeys = [];
    }
    this.receiveId = '';
  }

  // 编辑/新建 弹窗显示
  @action.bound creatDialogShow (isCreat = true) {
    this.isCreat = isCreat;
    this.getCommReceiveItems(isCreat);
    this.creatDialogVisible = true;
  }


  @action
  updateOdocForm (val) {
    this.odoConditionForm.updateFields({
      receiveName: val,
    });
  }

  // 获取odocForm表单
  @action.bound getCommReceiveItems (isCreat) {
    if (!isCreat) {
      this.receiveId = this.clickRowKey;
    }
    const params = {
      ConditionType: '1',
      receiveId: this.receiveId,
    };
    Api.getCommReceiveItems(params).then((data) => {
      if (data.api_status) {
        this.odocForm = new WeaForm();
        this.odocForm.setCondition(data.groupsCondition);
        if (isCreat) {
          this.odocForm.updateFields({ sorting: { value: '0' } });
        }
      }
    });
  }

  // 右侧操作菜单
  @action.bound onOperatesClick (record, index, operate, flag) {
    this.clickRowKey = record.id;
    if (flag == '0') {
      this.creatDialogShow(false);
    } else if (flag == '2') {
      this.showConfirm('one');
    } else if (flag == '3') {
      this.logShow();
    }
  }

  // 校验
  @action.bound onCheck () {
    this.odocForm.validateForm().then((data) => {
      if (data.isValid) {
        this.saveReceiveGroup();
      } else {
        data.showErrors();
      }
    });
  }

  // 编辑 新建保存
  @action.bound saveReceiveGroup () {
    this.setLoading(true);
    const formData = this.odocForm.getFormParams();
    const id = this.clickRowKey;
    const param = {
      ...formData,
      receiveunitType: '1',
      id: this.isCreat ? '' : id,
    };
    Api.saveReceiveGroup(param).then((data) => {
      this.setLoading(false);
      if (data.api_status) {
        this.getTableData();
        this.creatDialogHide('save');
        message.success(getLabel(22619, '保存成功！'));
      } else {
        message.error(data.api_errormsg);
      }
    });
  }

  // 获取高级搜索列表
  @action.bound getPanelForm () {
    const params = {
      cmd: 'base',
    };
    Api.getSearchCondition(params).then((data) => {
      this.conditions = data.conditions;
      if (window.console) console.log(data);
      this.odoConditionForm.initFormFields(data.conditions);
    });
  }


  @action.bound setOdocPanelStatus (bool) {
    this.isOdocPanelShow = bool;
  }

  @action.bound showConfirm (type = '') {
    const _this = this;
    confirm({
      title: getLabel('384484', '您是否确认要删除这项内容'),
      onOk () {
        _this.deletePrivate(type);
      },
    });
  }


  // 删除
  @action.bound deletePrivate (type) {
    this.setLoading(true);
    const params = {
      ids: type == 'one' ? this.clickRowKey : this.selectedRowKeys.join(','),
    };
    Api.deleteReceiveGroup(params).then((data) => {
      this.setLoading(false);
      if (data.api_status) {
        message.success(getLabel(20461, '删除成功'));
        this.getTableData();
        this.selectedRowKeys = [];
        this.clickRowKey = '';
      } else {
        message.error(data.api_errormsg);
      }
    });
  }


  // 获取日志参数
  @action.bound getPrivateReceiveLogType () {
    Api.getPrivateReceiveLogType().then((data) => {
      this.logType = data.logType;
      this.logSmallType = data.logSmallType;
    });
  }

  // 日志显示
  @action.bound logShow () {
    this.logVisible = true;
  }

  // 日志隐藏
  @action.bound logHide () {
    this.logVisible = false;
    // this.selectedRowKeys = [];
    this.clickRowKey = '';
  }

  @action.bound setLoading (bool) {
    this.loading = bool;
  }

  @action
  getRightMenu = () => {
    const disabled = !(toJS(this.selectedRowKeys).length > 0);
    let btns = [];
    btns.push({
      key: '1',
      icon: <i className="icon-coms-Add-to" />,
      content: getLabel(365, '新建'),
      onClick: () => {
        this.creatDialogShow();
      },
    }, {
      key: '2',
      disabled: !this.loading && disabled,
      icon: <i className="icon-coms-form-delete" />,
      content: getLabel(32136, '批量删除'),
      onClick: () => {
        this.showConfirm();
      },
    });
    return btns;
  }
  @action
  getButtons = () => {
    const disabled = !(toJS(this.selectedRowKeys).length > 0);
    const btns = [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@73s0y6@create`} type="primary" onClick={() => this.creatDialogShow()} >{getLabel(365, '新建')}</Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7ajdiv@batchDelete`} type="primary" disabled={!this.loading && disabled} onClick={() => this.showConfirm()} >{getLabel(32136, '批量删除')}</Button>,
    ];

    return btns;
  }
}
export default PrivateGroupStore;
