import { observable, action, toJS } from "mobx";
import { WeaForm } from "comsMobx";
import isEmpty from 'lodash/isEmpty'
import { WeaTableNew } from "comsMobx";
import { Modal, message } from "antd";
import { i18n } from "../public/i18n";
import forEach from 'lodash/forEach'
import * as Api from '../apis/company'; // 引入API接口文件


export class CompanyExtendStore {
  @observable form = new WeaForm();
  @observable tableInfo = []
  @observable conditions = [];
  @observable isEditor = false;
  @observable isNew = true;
  @observable loading = true;
  @observable tabInfo = [];
  @observable selectedKey = '0';
  @observable detailSelectedKey = '0';
  @observable topTab = [];
  @observable buttons = {};
  @observable id = ''; //分部id
  @observable date = '';
  @observable personalEditTables;
  @observable tabkey = '0';

  @observable open = false;
  @observable version = '1.0';

  @observable selectedRowKeys = [];
  @observable selectedRows = [];
  @observable versionList = {};

  @action onRowSelect = (keys) => {
    this.setSelectedRowKeys(keys);
  }

  @action setSelectedRowKeys = (keys) => {
    this.selectedRowKeys = keys;
  }


  @action edit = () => {
    this.isEditor = true;
    this.getData();
    this.getTabInfo();
    this.detailSelectedKey = '0'
  }

  @action selectVersions = () => {
    let params = {
      id:this.id,
      version:this.version
    }
    Api.selectVersions(params).then((res) => {
      this.open = true;
      if (res.code === 200) {
        this.setVersionList(res.data);
      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })
  }

  init = () => {
    this.detailSelectedKey = '0'
    this.isEditor = false;
  }

  save = () => {
    if (this.loading)
      return;
    this.form.validateForm().then(f => {
      if (f.isValid) {
        this.loading = true;
        if (this.personalEditTables) {
          const targetDatas = this.tableInfo[this.detailSelectedKey].tabinfo.datas,
            isPass = (targetDatas.length > 0) ? this.personalEditTables.refs.edit.doRequiredCheck().pass : true
          if (isPass) {
            this.editResource()
          } else {
            this.loading = false;
          }
        } else {
          this.editResource();
        }
      } else {
        f.showErrors();
        this.setDate(new Date());
        this.loading = false;
      }
    })
  }

  editResource = () => {
    let pDatas = this.form.getFormParams();
    Api.editResource({
      ...{
        id: this.id,
        viewCondition: this.selectedKey
      },
      ...pDatas,
      ...this.getTableEditParams()
    }).then(data => {
      if (data.code == 200) {
        message.success(i18n.message.saveSuccess());
        this.init();
        this.getData();
        this.selectedRowKeys = [];
      } else {
        message.warning(data.msg);
      }
      this.loading = false;
    }, error => {
      message.warning(error.msg);
      this.loading = false;
    })
  }

  getTableEditParams = () => {
    const params = {};
    this.tableInfo && this.tableInfo.forEach(t => {
      t.tabinfo.datas = t.tabinfo.datas || [];
      params[t.tabinfo.rownum] = t.tabinfo.datas.length;
      t.tabinfo && t.tabinfo.datas && t.tabinfo.datas.forEach((item, index) => {
        !isEmpty(item) && forEach(item, (value, key) => {
          Object.assign(params, {
            [`${key}_${index}`]: value
          });
        })
      })
    })
    return params
  }



  getData = () => {
    this.setLoading(true);
    this.form = new WeaForm();
    let params = {
      viewAttr: this.isEditor ? 2 : 1,
      id: this.id,
      viewCondition: this.selectedKey
    }
    Api.getCompanyExtendForm(params).then((res) => {
      if (res.code === 200) {
        res.data.result.conditions && this.form.initFormFields(res.data.result.conditions);
        res.data.result.conditions && this.setConditions(res.data.result.conditions);
        //this.tableInfo = this.handleTable(res.data.result.tables);
        this.getTabInfo();
        res.data.result.buttons && this.setButtons(res.data.result.buttons);
        res.data.result.tabInfo && this.setTopTab(res.data.result.tabInfo);
        this.isEditor && this.getSelectedRows();
        this.setLoading(false);

      } else {
        message.warning(res.msg);
      }
    }, error => {
      message.warning(error.msg);
    })




  }

  handleTable = (datas) => {
    return datas && datas.map(data => {
      const { tabinfo: { columns } } = data;
      const length = columns.length;
      columns.map(c => {
        c.width = `${95 / length}%`
      })
      return data
    })
  }

  getTabInfo = () => {
    this.tabInfo = [];
    this.tableInfo && this.tableInfo.forEach((c, idx) => {
      if (!c.hide) {
        this.tabInfo.push({
          key: `${idx}`,
          title: c.tabname,
        })
      }
    })
    //if (!isEmpty(this.tabInfo)) this.tabkey = this.tabInfo[0].key;
    if (!isEmpty(this.tabInfo)) this.detailSelectedKey = this.tabInfo[0].key;
  }

  changeVersion = data => {
    this.version = data;
    this.selectVersions();
  }

  setLoading(val) {
    this.loading = val;
  }


  updateTabKey = (key) => {
    this.tabKey = key;
  }

  updateDetailSelectedKey = (key) => {
    this.detailSelectedKey = key;
  }

  updateTableInfo = (data) => {
    this.tableInfo = data
  }

  setSelectedKey = (key) => {
    this.selectedKey = key;
  }

  getSelectedRows = () => {
    const selectedRows = [];
    this.tableInfo.forEach(t => {
      const singleTableRows = [];
      t.tabinfo.datas.forEach((data, i) => {
        if (data.viewAttr === 1) {
          singleTableRows.push(i);
        }
      });
      selectedRows.push(singleTableRows);
    })
    this.selectedRows = selectedRows;
  }


  setTopTab(topTab) {
    this.topTab = topTab;
  }

  changeData(key) {
    this.setSelectedKey(key);
    this.getData();
  }

  setId(id) {
    this.id = id;
  }

  setPersonalEditTables = (ref) => {
    this.personalEditTables = ref;
  }

  setConditions(conditions) {
    this.conditions = conditions;
  }

  setButtons(buttons) {
    this.buttons = buttons;
  }

  setDate(date) {
    this.date = date;
  }

  setVersionList(obj) {
    this.versionList = obj;
  }

  //历史版本
  // @observable versionList = {
  //   operateName: '史永成',
  //   operateTime: '2022-12-31',
  //   versions: [
  //     { value: "1.0" },
  //     { value: "1.1" },
  //     { value: "1.2" },
  //     { value: "1.3" }
  //   ],
  //   datas: [
  //     {
  //       fieldId: '简称',
  //       fieldValue: '泛微'
  //     }, {
  //       fieldId: '全称',
  //       fieldValue: '泛微'
  //     }, {
  //       fieldId: '上级分部',
  //       fieldValue: '泛微北京'
  //     }, {
  //       fieldId: '网站',
  //       fieldValue: '1546875925@qq.com'
  //     }, {
  //       fieldId: '分部ID',
  //       fieldValue: '8'
  //     }, {
  //       fieldId: '显示顺序',
  //       fieldValue: '9'
  //     }, {
  //       fieldId: '分部编号',
  //       fieldValue: '分部1-1'
  //     }, {
  //       fieldId: '限制用户数',
  //       fieldValue: '产品运营'
  //     }, {
  //       fieldId: '在职离职人数',
  //       fieldValue: '产品运营'
  //     }
  //   ]



  // }

}
