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

import * as API from "../apis/cumDeduct"; // 引入API接口文件
import { removePropertyCondition } from "../util/response";

const { TableStore } = WeaTableNew;

export class CumDeductStore {
  @observable tableStore = new TableStore(); // new table
  @observable slideTableStore = new TableStore();
  @observable form = new WeaForm(); // new 一个form
  @observable addForm = new WeaForm(); // 新增form
  @action initAddForm = () => this.addForm = new WeaForm(); // 初始化新增form
  @observable condition = []; // 存储后台得到的form数据
  @observable hasRight = true; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
  @observable showSearchAd = false; // 高级搜索面板显示
  @observable loading = true; // 数据加载状态
  @observable slideLoading = true; // 详情数据加载状态
  @observable step = 0; //  当前所在第几步
  @observable slideVisiable = false; // slide 是否隐藏
  @observable slideDataSource = [];
  @observable importResult = {};
  @observable modalVisiable = false; // 模态框显示
  @observable currentRecord = {}; // 当前record
  @observable columns = [];
  @observable pageObj = {}; //列表数据
  @observable dataSource = []; //列表数据
  @observable slideColumns = []; //详情列数据
  @observable slidePageObj = {}; //详情分页列表数据
  @observable slideTableDataSource = []; //详情列表数据

  // ** 设置导入参数 start **
  @action
  setSlideDataSource = (slideDataSource) => {
    this.slideDataSource = slideDataSource;
  };

  @action
  setImportResult = (importResult) => {
    this.importResult = importResult;
  };
  // ** 设置导入参数 end **

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

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

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

  // 详情列表数据
  @action
  setSlideColumns = (columns) => (this.slideColumns = columns);

  @action
  setSlidePageObj = (pageObj) => (this.slidePageObj = pageObj);

  @action
  setSlideTableDataSource = (dataSource) =>
    (this.slideTableDataSource = dataSource);

  @action
  setCurrentRecord = (currentRecord) => (this.currentRecord = currentRecord);

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

  @action
  setStep = (step) => (this.step = step);

  @action
  setSlideVisiable = (slideVisiable) => (this.slideVisiable = slideVisiable);

  // 初始化操作
  @action
  doInit = (params = {}) => {
    this.getCondition();
    this.getTableDatas(params);
  };

  // 获得高级搜索表单数据
  @action
  getCondition = () => {
    API.getCumDeductSaCondition().then(
      action((res) => {
        if (res.status) {
          // 接口请求成功/失败处理
          let condition = removePropertyCondition(res.data.condition);
          this.condition = condition;
          this.form.initFormFields(condition); // 渲染高级搜索form表单
        } else {
          message.error(res.errormsg || "接口调用失败！");
        }
      })
    );
  };

  // 渲染table数据
  @action
  getTableDatas = (params) => {
    params = params || {};
    this.loading = true;
    let requestParams = this.form.getFormParams() || {};
    requestParams = {
      ...requestParams,
      current: this.pageObj.current,
      pageSize: this.pageObj.pageSize,
      ...params
    };
    API.getCumDeductList(requestParams).then(
      action(({ status, data, errormsg }) => {
        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 {
          this.setDataSource([]);
          this.setPageObj({
            ...this.pageObj,
            total: 0
          });
          message.error(errormsg || "接口调用失败！");
        }
      })
    );
  };

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

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

  // 导入
  @action importFile = (params) => {
    API.importCumDeductParam(params).then(
      action((res) => {
        if (res.status) {
          this.importResult = res.data;
        }
      })
    );
  };

  // 导入预览
  @action previewImport = (params) => {
    API.importCumDeductPreview(params).then(
      action((res) => {
        if (res.status) {
          this.slideDataSource = res.data.preview;
        } else {
          message.error(res.errormsg || "预览失败");
        }
      })
    );
  };

  // 导出
  @action exportCumDeductList = (ids = "") => {
    API.exportCumDeductList(ids);
  };

  // 查询明细
  @action getCumDeductDetailList = (id, param = {}) => {
    let requestParams = { accumulatedSpecialAdditionalDeductionId: id };
    requestParams = { ...requestParams, ...param };
    this.slideLoading = true;
    API.getCumDeductDetailList(requestParams).then(
      ({ data, status, errormsg }) => {
        this.slideLoading = false;
        if (status) {
          const { columns, list, total, pageNum: current, pageSize } = data;
          this.setSlideColumns(columns);
          this.setSlideTableDataSource(list);
          this.setSlidePageObj({
            total,
            current,
            pageSize
          });
        } else {
          this.setSlideTableDataSource([]);
          this.setSlidePageObj({
            ...this.slidePageObj,
            total: 0
          });
          message.error(errormsg || "接口调用失败！");
        }
      }
    );
  };

  // 导出明细
  @action exportCumDeductDetailList = (id, ids = "", taxAgentId = "") => {
    API.exportCumDeductDetailList(id, ids, taxAgentId);
  };
}
