import { action, observable } from "mobx";
import { message } from "antd";
import { WeaForm, WeaTableNew } from "comsMobx";
import { removePropertyCondition } from "../util/response";
import * as API from "../apis/standingBook";

const { TableStore } = WeaTableNew;

export class StandingBookStore {
  @observable accountForm = new WeaForm(); // 社保台账核算Form
  @action initAccountForm = () => this.accountForm = new WeaForm();
  @observable addCPForm = new WeaForm(); // 社保台账详情页面，添加补差人员form
  @action initAddCPForm = () => this.addCPForm = new WeaForm();
  @observable welfareRQForm = new WeaForm(); // 社保台账页面重构，查询form


  @observable tableStore = new TableStore(); // new table
  @observable form = new WeaForm(); // nrew 一个form
  @observable ocForm = new WeaForm(); // 社保福利线下对比form
  @observable condition = []; // 存储后台得到的form数据
  @observable hasRight = true; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
  @observable showSearchAd = false; // 高级搜索面板显示
  @observable loading = false; // 数据加载状态
  @observable inspectLoading = false; // 核算异常列表加载loading
  @observable saveLoading = false; // 添加正常缴纳人员loading
  @observable deleteLoading = false; // 删除月份表单loading

  @observable previewStandingBookAcctResultColumns = []; // 预览列表
  @observable previewStandingBookAcctResultDataSource = []; // 预览DataSource
  @observable importStandingBookAcctResult = {}; // 导入结果
  @observable importOCStandingBookAcctResult = {}; // 导入结果

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

  // 社会福利台账-获取正常缴纳列表
  @action
  getCommonList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getCommonList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            const {
              pageInfo: { list, columns, total, pageNum }
            } = data;
            resolve({ list, columns, total, pageNum });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-获取正常缴纳列表
  @action
  getNormalList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getNormalList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            const {
              pageInfo: { list, columns, total }
            } = data;
            resolve({ list, columns, total });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-获取补缴列表
  @action
  getSupplementaryList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getSupplementaryList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            const {
              pageInfo: { list, columns, total }
            } = data;
            resolve({ list, columns, total });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-获取总览列表
  @action
  getOverViewList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getOverViewList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            const { list, columns, total } = data;
            resolve({ list, columns, total });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-获取异动列表
  @action
  getChangeList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getChangeList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            const {
              dataKey: { datas },
              pageInfo: { list, total }
            } = data;
            this.tableStore.getDatas(datas);
            resolve({ list, total });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-获取正tab列表
  @action
  getTabList = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.getTabList(params).then(
        action(({ data, status }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            resolve({ data });
          } else {
            message.error("接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-保存并进入核算
  @action
  save = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.save(params).then(
        action(({ data, status, errormsg }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-归档
  @action
  siaccountFile = (params) => {
    this.loading = true;
    return new Promise((resolve, reject) => {
      API.siaccountFile(params).then(
        action(({ data, status, errormsg }) => {
          this.loading = false;
          if (status) {
            // 接口请求成功/失败处理
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-删除正常缴纳人员
  @action
  siaccountCommonDelete = (params) => {
    return new Promise((resolve, reject) => {
      API.siaccountCommonDelete(params).then(
        action(({ data, status, errormsg }) => {
          if (status) {
            // 接口请求成功/失败处理
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-核算异常列表
  @action
  inspectList = (params) => {
    this.inspectLoading = true;
    return new Promise((resolve, reject) => {
      API.inspectList(params).then(
        action(({ data, status, errormsg }) => {
          this.inspectLoading = false;
          if (status) {
            // 接口请求成功/失败处理
            const {
              dataKey: { datas },
              pageInfo: { list, total }
            } = data;
            this.tableStore.getDatas(datas);
            resolve({ list, total });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-添加正常缴纳人员
  @action
  siaccountCommonSave = (params) => {
    this.saveLoading = true;
    return new Promise((resolve, reject) => {
      API.siaccountCommonSave(params).then(
        action(({ data, status, errormsg }) => {
          this.saveLoading = false;
          if (status) {
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-添加补缴人员
  @action
  siaccountSupplementarySave = (params) => {
    this.saveLoading = true;
    return new Promise((resolve, reject) => {
      API.siaccountSupplementarySave(params).then(
        action(({ data, status, errormsg }) => {
          this.saveLoading = false;
          if (status) {
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 社会福利台账-删除月份表单
  @action
  siaccountDelete = (params) => {
    this.deleteLoading = true;
    return new Promise((resolve, reject) => {
      API.siaccountDelete(params).then(
        action(({ data, status, errormsg }) => {
          this.deleteLoading = false;
          if (status) {
            resolve({ data });
          } else {
            message.error(errormsg || "接口调用失败！");
            reject();
          }
        })
      );
    });
  };
  // 获得高级搜索表单数据
  @action
  siaccountCommonForm = () => {
    API.siaccountCommonForm().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 || "接口调用失败！");
        }
      })
    );
  };
  // 补缴添加缴纳人员表单
  @action
  querySupplementaryForm = () => {
    API.querySupplementaryForm().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) => {
    this.loading = true;
    const formParams = this.form.getFormParams() || {};
    params = params || formParams;
    API.getTableDatas(params).then(
      action((res) => {
        if (res.api_status) {
          // 接口请求成功/失败处理
          this.tableStore.getDatas(res.datas); // table 请求数据
          this.hasRight = res.hasRight;
        } else {
          message.error(res.errormsg || "接口调用失败！");
        }
        this.loading = false;
      })
    );
  };

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

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

  @action
  commonAccount = (params) => {
    return new Promise((resolve, reject) => {
      API.commonAccount(params).then(res => {
        if (res.status) {
          resolve();
        } else {
          message.error(res.errormsg || "接口调用失败！");
          reject();
        }
      });
    });
  };

  @action("社保福利台账重新核算")
  socialSecurityBenefitsRecalculate = (params) => {
    return new Promise((resolve, reject) => {
      API.socialSecurityBenefitsRecalculate(params).then(res => {
        if (res.status) {
          resolve();
        } else {
          reject(res.errormsg || "接口调用失败！");
        }
      });
    });
  };

  // 获取当前管理员下的所有个税扣缴义务人
  @action
  getAdminTaxAgentList = () => {
    return new Promise((resolve, reject) => {
      API.getAdminTaxAgentList().then(res => {
        if (res.status) {
          resolve(res.data);
        } else {
          message.error(res.errormsg || "接口调用失败！");
          reject();
        }
      });
    });
  };

  @action("社保福利台账核算导入信息表头信息列表")
  getWelfareList = () => {
    return new Promise((resolve, reject) => {
      API.getWelfareList().then(res => {
        if (res.status) {
          resolve(res.data);
        } else {
          message.error(res.errormsg || "接口调用失败！");
          reject();
        }
      });
    });
  };
  @action("社保福利台账补差导入信息表头信息列表")
  getBalanceWelfareList = () => {
    return new Promise((resolve, reject) => {
      API.getBalanceWelfareList().then(res => {
        if (res.status) {
          resolve(res.data);
        } else {
          message.error(res.errormsg || "接口调用失败！");
          reject();
        }
      });
    });
  };

  @action
  setPreviewStandingBookAcctResultDataSource = previewAcctResultDataSource => {
    this.previewStandingBookAcctResultDataSource = previewAcctResultDataSource;
  };
  @action
  setPreviewStandingBookAcctResultColumns = (previewAcctResultColumns) => {
    this.previewStandingBookAcctResultColumns = previewAcctResultColumns;
  };
  @action
  setImportStandingBookAcctResult = importAcctResult => {
    this.importStandingBookAcctResult = importAcctResult;
  };


  @action("社保福利台账-导入预览")
  welfarePreview = (params) => {
    API.welfarePreview(params).then((res) => {
      if (res.status) {
        this.previewStandingBookAcctResultColumns = res.data.headers.map((item, index) => {
          let column = {};
          column.title = item;
          column.dataIndex = "" + index;
          column.key = index + "";
          return column;
        });

        this.previewStandingBookAcctResultDataSource = res.data.list.map((item) => {
          let data = {};
          item.map((i, index) => {
            data[index + ""] = i;
          });
          return data;
        });
      } else {
        message.error(res.errormsg || "获取失败");
      }
    });
  };

  @action("社保福利台账-补差数据导入")
  importBalanceInsuranceDetail = (params) => {
    API.importBalanceInsuranceDetail(params).then(res => {
      if (res.status) {
        this.importStandingBookAcctResult = res.data;
      } else {
        message.error(res.errormsg || "导入失败");
      }
    });
  };
  @action("社保福利台账-核算数据导入")
  importInsuranceAcctDetail = (params) => {
    API.importInsuranceAcctDetail(params).then(res => {
      if (res.status) {
        this.importStandingBookAcctResult = res.data;
      } else {
        message.error(res.errormsg || "导入失败");
      }
    });
  };
  @action("社保福利台账-线下对比数据导入")
  importComparisonExcelStandingBookResult = (params) => {
    API.importExcelInsuranceDetail(params).then(res => {
      if (res.status) {
        this.importOCStandingBookAcctResult = res.data;
      } else {
        message.error(res.errormsg || "导入失败");
      }
    });
  };
}
