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


export class ResourceCardStore {
    @observable userid = '';
    @observable height = 2000;
    @observable scrollLoading = false;
    @observable offset = 20;
    @observable overtime = 1000;
    @observable intervalTime = 500;
    @observable data = {};
    @observable detectElements = [] // 受监控元素,可能存在空项
    @observable flag = true; // 点击Anchor菜单项,不执行监听滚动事件

    @observable open = false;
    @observable version = '1.0';
    @observable versionList = {};

    @action("初始化") init = () => {
        //const { userid } = JSON.parse(localStorage.getItem("theme-account"));
        this.getCardData();

    }

    @action("获取卡片信息")  getCardData = () => {
        Api.getCardData(this.userid).then((res) => {
            if (res.code === 200) {
              this.setData(res.data)
              this.buildDetectElements(res.data); //生成受监控元素
            } else {
              message.warning(res.msg);
            }
          }, error => {
            message.warning(error.msg);
          })
    }

    @action("历史版本") selectVersions = () => {
        let params = {
          id:this.userid,
          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);
        })
      }

    buildDetectElements = (data) => {
        !this.isEmptyObject(data) && data.anchorList.map((item, index) => {
            item.activity = index;
            item.offset = document.getElementById(item.id).offsetTop + document.getElementById(item.id).clientHeight;
        })
        this.setDetectElements(data.anchorList);
    }

    isEmptyObject(obj) {
        for (let key in obj) {
            return false;
        }
        return true;
    }

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


    setData(value){
        this.data = value;
    }


    setDetectElements(value) {
        this.detectElements = value;
    }

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

}
