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

import * as API from '../apis/taxrate'; // 引入API接口文件

const { TableStore } = WeaTableNew;

let emptyItem = {
  indexNum: 1,
  incomeLowerLimit: "0.00",
  incomeUpperLimit: "0.00",
  dutyFreeValue: "0.00",
  dutyFreeRate: "0.00",
  taxableIncomeLl: "0.00",
  taxableIncomeUl: "0.00",
  taxRate: "0.00",
  taxDeduction: "0.00"
}


export class taxRateStore {
  @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 dataSource = [];
  @observable nameValue = "";
  @observable remarkValue = "";
  @observable slideVisiable = false; // 侧边划入是否展示

  currentId = ""; // 当前编辑的数据id

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

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

  @action
  setNameValue = nameValue => this.nameValue = nameValue;

  @action
  setRemarkValue = remarkValue => this.remarkValue = remarkValue;

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

  @action
  initDataSource = () => {
    this.dataSource = []
    this.dataSource.push(emptyItem)
  }

  // 渲染table数据
  @action
  getTableDatas = (params) => {
    params = params || {};
    API.getTaxRateList(params).then(action(res => {
      if (res.status) { // 接口请求成功/失败处理
        this.tableStore.getDatas(res.data.datas); // table 请求数据
      } else {
        message.error(res.errormsg  || '接口调用失败！')
      }
      this.loading = false;
    }));
  }

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

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


  @action doSave = () => {
    let params = {
      taxRateBatch: {
        name: this.nameValue,
        description: this.remarkValue
      },
      taxRateRecords: this.dataSource
    }
    if(!params.name || params.name == "" ) {
      message.warning("名称不能为空")
      return
    }
    API.saveTaxRate(params).then(res => {
      if(res.status) {
        message.success("保存成功");
        this.getTableDatas();
        this.showSearchAd = false;
        this.setSlideVisiable(false);
      } else {
        message.error(res.errormsg  || "保存失败");
      }
    })
  }

  @action emptyForm = () => {
    this.setDataSource([]);
    this.setNameValue("");
    this.setRemarkValue("");
  }

  // 获取表单数据
  @action getItemInform = (id) => {
    this.currentId= id;
    API.getTaxRateForm({id}).then(res => {
      if(res.status) {
        let { taxRateBatch, taxRateRecords } = res.data.form
        this.setNameValue(taxRateBatch.name);
        this.setRemarkValue(taxRateBatch.description);
        this.setDataSource(taxRateRecords)
      } else {
        message.error(res.errormsg  || "获取数据失败");
      }
    })
  }

  @action doUpdate = () => {
    let params = {
      taxRateBatch: {
        name: this.nameValue,
        description: this.remarkValue,
        id: this.currentId
      },
      taxRateRecords: this.dataSource
    }

    if(!params.name || params.name == "") {
      message.warning("名称不能为空")
      return
    }
    API.updateTaxRate(params).then(res => {
      if(res.status) {
        message.success("保存成功");
        this.getTableDatas();
        this.showSearchAd = false;
        this.setSlideVisiable(false);
      } else {
        message.error(res.errormsg  || "保存失败");
      }
    })
  }

  @action doDelete = (ids) => {
    API.deleteTaxRate(ids).then(res => {
      if(res.status) {
        message.success("删除成功");
        this.getTableDatas();
        this.showSearchAd = false;
      } else {
        message.error(res.errormsg  || "删除失败");
      }
    })
  }

  // 批量删除
  @action
  doBatchDelete = () => {
    let ids = toJS(this.tableStore.selectedRowKeys)
    if(ids.length == 0) {
      message.warning("未选择任何条目");
      return
    }
    this.doDelete(toJS(this.tableStore.selectedRowKeys))
  }


}
