import db from "../../plugins/db";

const state = {
  clientConfig: {},
  curLevel: "",
  camera: "", // 相机编号
  curSubject: {},
  scanArea: {},
  scanNo: 0, // 已采集数量（从当前设置的计数时间算起）
  unuploadNo: 0, // 未上传数量（从当前设置的计数时间算起）,
  startCountTime: null, // 计数时间，默认：当前登录时间
  showToLoginModel: false
};

const mutations = {
  setClientConfig(state, clientConfig) {
    state.clientConfig = Object.assign({}, state.clientConfig, clientConfig);
  },
  setCurLevel(state, curLevel) {
    state.curLevel = curLevel;
  },
  setCamera(state, camera) {
    state.camera = camera;
  },
  setStartCountTime(state, startCountTime) {
    state.startCountTime = startCountTime;
  },
  setShowToLoginModel(state, showToLoginModel) {
    state.showToLoginModel = showToLoginModel;
  },
  setCurSubject(state, curSubject) {
    state.curSubject = curSubject;
  },
  setScanArea(state, scanArea) {
    state.scanArea = scanArea;
  },
  setScanNo(state, scanNo) {
    state.scanNo = scanNo;
  },
  setUnuploadNo(state, unuploadNo) {
    state.unuploadNo = unuploadNo;
  }
};

const actions = {
  async updateCurSubject({ state, commit }, curSubject) {
    const scanArea = Object.assign({}, state.scanArea, {
      [curSubject.id]: curSubject
    });
    commit("setCurSubject", curSubject);
    commit("setScanArea", scanArea);
    await db.setDict("scanArea", JSON.stringify(scanArea));
  },
  async updateStartCountTime({ commit }, data) {
    const startCountTime = Object.assign({}, state.startCountTime, data);
    commit("setStartCountTime", startCountTime);
    await db.setDict("startCountTime", JSON.stringify(startCountTime));
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
