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

import * as API from "../apis"; // 引入API接口文件
import { getVariableSalaryList } from "../apis/variableSalary"; //浮动薪酬-薪资档案列表查询

const { TableStore } = WeaTableNew;

export class BaseTableStore {
  @observable tableStore = new TableStore(); // new table
  @observable form = new WeaForm(); // nrew 一个form
  @observable condition = []; // 存储后台得到的form数据
  @observable hasRight = true; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
  @observable showSearchAd = false; // 高级搜索面板显示
  @observable loading = true; // 数据加载状态

  // 浮动薪酬相关
  @observable VExtraSalryForm = new WeaForm(); // 浮动薪酬extra查询form
  @observable VSalryForm = new WeaForm(); // 浮动薪酬查询form
  @observable VSSalaryItemForm = new WeaForm(); // 新增编辑浮动薪酬项目form
  @action initVSSalaryItemForm = () => this.VSSalaryItemForm = new WeaForm();
  @observable VSSalaryFileForm = new WeaForm(); // 新增编辑薪资档案form
  @action initVSSalaryFileForm = () => this.VSSalaryFileForm = new WeaForm();
  @observable SFTableStore = new TableStore(); // 浮动薪酬-薪资档案table

  @action("浮动薪酬-薪资档案列表查询")
  getVariableSalaryList = (payload) => {
    return new Promise((resolve, reject) => {
      getVariableSalaryList(payload).then(res => {
        const { data, status } = res;
        if (status) {
          const { dataKey } = data;
          const { datas } = dataKey;
          this.SFTableStore.getDatas(datas);
        }
        resolve(res);
      }).catch(() => {
        reject();
      });
    });
  };


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

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

  // 渲染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.msg || "接口调用失败！");
      }
      this.loading = false;
    }));
  };

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

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

}
