import { action, observable } from "mobx";
import { message } from "antd";
import { WeaForm, WeaTableNew } from "comsMobx";

import * as API from "../apis/taxAgent"; // 引入API接口文件
import { decentralizationConditions, editConditions } from "../pages/taxAgent/editConditions";

const { TableStore } = WeaTableNew;

export class TaxAgentStore {
  @observable advanceForm = new WeaForm(); //权限-角色高级搜索form表单
  @observable roleForm = new WeaForm(); //权限-角色form表单
  @action initRoleForm = () => this.roleForm = new WeaForm();
  @observable roleOperatorForm = new WeaForm(); //权限-角色操作者form表单
  @action initRoleOperatorForm = () => this.roleOperatorForm = new WeaForm();
  @observable PageAndOptAuth = { able: false, opts: [] }; // 业务线页面权限
  @action initPageAndOptAuth = () => this.PageAndOptAuth = { able: true, opts: ["query", "admin"] };// 设置业务线页面权限


  @observable tableStore = new TableStore(); // new table
  @observable form = new WeaForm(); //表单实体
  @observable formDecentralization = new WeaForm(); //关闭分权表单
  @observable condition = []; // 存储后台得到的form数据
  @observable hasRight = true; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
  @observable showSearchAd = false; // 高级搜索面板显示
  @observable showOperateBtn = false; // 页面操作按钮显示权限
  @observable showSalaryItemBtn = false; // 薪资项目管理页面操作按钮显示权限
  @observable statisticsReportBtn = false; // 薪酬统计报表页面操作按钮显示权限
  @observable payrollPermission = false; // 薪资核算显示权限
  @observable loading = false; // 数据加载状态
  @observable modalVisiable = false; // EditModal 模态框
  @observable columns = [];
  @observable pageObj = {}; //列表数据
  @observable dataSource = []; //列表数据
  @observable taxAgentOption = []; // 个税扣缴义务人
  @observable taxAgentAdminOption = []; // 当前登录人个税扣缴义务人下拉列表项
  @observable hideIconInTax = true; // 当前登录人是否有添加人员范围的权限


  @action setModalVisiable = visiable => (this.modalVisiable = visiable);

  @action setColumns = columns => (this.columns = columns);

  @action setPageObj = pageObj => (this.pageObj = pageObj);

  @action setDataSource = dataSource => (this.dataSource = dataSource);

  @action setShowOperateBtn = bool => (this.showOperateBtn = bool);

  @action setSalaryItemBtn = bool => (this.showSalaryItemBtn = bool);//薪资项目权限
  @action setStatisticsReportBtn = bool => (this.statisticsReportBtn = bool);//薪酬统计报表权限
  @action setPayrollPermission = bool => (this.payrollPermission = bool);// 薪资核算页面权限

  // 初始化操作
  @action
  doInit = params => {
    this.getTaxAgentList(params);
    this.getCondition(editConditions);
    this.getFormDecentralizationCondition(decentralizationConditions);
    this.hasIconInTax();
  };

  @action
  getCondition = condition => {
    this.form.initFormFields(condition);
  };

  @action
  getFormDecentralizationCondition = condition => {
    this.formDecentralization.initFormFields(condition);
  };

  // 渲染table数据
  @action
  getTaxAgentList = params => {
    this.loading = true;
    params = params || {};
    API.getTaxAgentList(params).then(
      action(({ data, status }) => {
        this.loading = false;
        if (status) {
          // 接口请求成功/失败处理
          const { columns, list, total, pageNum: current, pageSize } = data;
          this.setColumns(columns);
          this.setDataSource(list);
          this.setPageObj({
            total,
            current,
            pageSize
          });
        } else {
          message.error(res.msg || "接口调用失败！");
        }
      })
    );
  };

  @action setShowSearchAd = bool => (this.showSearchAd = bool);

  @action("同步人员范围")
  taxAgentRangeSync = params => {
    return API.taxAgentRangeSync(params);
  };

  // 高级搜索 - 搜索
  @action
  doSearch = name => {
    this.getTaxAgentList({ name });
    this.showSearchAd = false;
  };

  // 新增
  @action
  saveTaxAgent = params => {
    return API.saveTaxAgent(params);
  };

  // 更新
  @action
  updateTaxAgent = params => {
    return API.updateTaxAgent(params);
  };

  // 获取个税扣缴义务人基础信息表单
  @action
  getTaxAgentBaseForm = params => {
    return API.getTaxAgentBaseForm(params);
  };

  // 获取个税扣缴义务人表单
  @action
  getTaxAgentForm = params => {
    return API.getTaxAgentForm(params);
  };

  // 保存个税扣缴义务人基础信息
  @action
  taxAgentBaseSave = params => {
    return API.taxAgentBaseSave(params);
  };

  @action
  deleteTaxAgent = params => {
    return API.deleteTaxAgent(params);
  };

  @action
  getPermission = params => {
    return new Promise((resolve, reject) => {
      API.getPermission(params).then(({ status, data }) => {
        if (status) {
          this.PageAndOptAuth = data;
          const { isAdminEnable, isChief, isOpenDevolution } = data;
          this.setShowOperateBtn(
            !isOpenDevolution ? true : isAdminEnable ? true : false
          );
          this.setSalaryItemBtn((isOpenDevolution && (isChief || isAdminEnable)));
          //薪酬统计报表权限
          this.setStatisticsReportBtn(!isOpenDevolution ? true : !!(isAdminEnable || isChief));
          //薪资核算详情页面查看权限
          this.setPayrollPermission((isOpenDevolution && isAdminEnable) || !isOpenDevolution);
          resolve({ status, data });
        } else {
          reject();
        }
      }).catch(() => reject());
    });
  };
  // 人员范围列表
  @action
  getTaxAgentRangeListInclude = params => {
    return API.getTaxAgentRangeListInclude(params);
  };
  // 人员范围排除列表
  @action
  getTaxAgentRangeListExclude = params => {
    return API.getTaxAgentRangeListExclude(params);
  };
  // 获取人员范围表单
  @action
  getTaxAgentRangeForm = params => {
    return API.getTaxAgentRangeForm(params);
  };
  // 非系统人员范围保存
  @action
  taxAgentRangeExtSave = params => {
    return API.taxAgentRangeExtSave(params);
  };
  // 非系统人员范围查询
  @action
  taxAgentRangelistExt = params => {
    return API.taxAgentRangelistExt(params);
  };
  // 非系统人员范围删除
  @action
  taxAgentRangeExtDelete = params => {
    return API.taxAgentRangeExtDelete(params);
  };
  // 人员范围保存
  @action
  taxAgentRangeSave = params => {
    return API.taxAgentRangeSave(params);
  };
  // 人员范围删除
  @action
  taxAgentRangeDelete = params => {
    return API.taxAgentRangeDelete(params);
  };

  @action
  fetchTaxAgentOption = () => {
    return new Promise((resolve, reject) => {
      API.getTaxAgentSelectList().then(
        action(res => {
          if (res.status) {
            this.taxAgentOption = res.data.map(item => {
              return { key: item.id, showname: item.content };
            });
            resolve(res);
          } else {
            message.error(res.errormsg || "获取失败");
            reject();
          }
        })
      );
    });
  };

  @action("当前登录人个税扣缴义务人下拉列表")
  getTaxAgentSelectListAsAdmin = () => {
    return new Promise((resolve, reject) => {
      API.getTaxAgentSelectListAsAdmin().then(
        action(res => {
          if (res.status) {
            this.taxAgentAdminOption = res.data.map(item => {
              return { key: item.id, showname: item.content };
            });
            resolve(res);
          } else {
            message.error(res.errormsg || "获取失败");
            reject();
          }
        })
      );
    });
  };

  @action("当前登录人是否有添加人员范围的权限")
  hasIconInTax = () => {
    return new Promise((resolve, reject) => {
      API.hasIconInTax().then(
        action(res => {
          if (res.status) {
            this.hideIconInTax = res.data !== "true";
            resolve(res);
          } else {
            message.error(res.errormsg || "获取失败");
            reject();
          }
        })
      );
    });
  };
}
