import { index } from "@/api/system/baseForm";
import * as $moment from "moment";
const state = {
  assets: [[],[]],
  leases: [],
  adventLeases: [],
  safety: [],
  safetyPlan: [],
  adventSafetyPlan: [],
  type: 1,
}
let config = {}

const mutations = {
  SET_ASSETS: (state, assets) => {
    state.assets = assets
  },
  SET_LEASES: (state, leases) => {
    state.leases = leases
  },
  SET_ADVENT_LEASES: (state, adventLeases) => {
    state.adventLeases = adventLeases
  },
  SET_SAFETY_PLAN: (state, safetyPlan) => {
    state.safetyPlan = safetyPlan
  },
  SET_ADVENT_SAFETY_PLAN: (state, adventSafetyPlan) => {
    state.adventSafetyPlan = adventSafetyPlan
  },
  SET_TYPE: (state, type) => {
    state.type = type
  },
  SET_SAFETY: (state, safety) => {
    state.safety = safety
  }
}

const actions = {
  getAssets({ commit, state }) {
    return new Promise((resolve, reject) => {
      Promise.all([
        index({
          table_name: "houses",
          page: 1,
          page_size: 999
        },false),
        index({
          table_name: "lands",
          page: 1,
          page_size: 999
        })
      ]).then(res => {
        const [houses,lands] = res;
        commit("SET_ASSETS",[houses.data,lands.data])
        resolve([houses.data,lands.data])
      }).catch(err => {
        reject(err)
      })
    })
  },
  getLeases({ commit, state }) {
    return new Promise((resolve, reject) => {
      index({
        table_name: "leases",
        page: 1,
        page_size: 999,
        filter: [
          {
            key: 'zulinjieshuqixian',
            op: 'range',
            value: `${$moment().format("YYYY-MM-DD")},2999-12-31`
          },
          {
            key: 'zulinkaishiqixian',
            op: 'range',
            value: `${$moment(0).format("YYYY-MM-DD")},${$moment().format("YYYY-MM-DD")}`
          }
        ]
      }).then(res => {
        commit("SET_LEASES", res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  async getAdventLeases({ commit, state }) {
    if (!config.time || !config.unit) {
      const res = await index({
        table_name: 'warnings',
        filter: [
          {
            key: 'flag',
            op: 'eq',
            value: 'contract'
          }
        ]
      },false)
      config.time = Number(res.data[0]?.time);
      config.unit = res.data[0]?.unit;
    }
    return new Promise((resolve, reject) => {
      let preDate = $moment().add(config.time, config.unit).format("YYYY-MM-DD")
      index({
        table_name: "leases",
        page: 1,
        page_size: 999,
        filter: [
          {
            key: 'zulinjieshuqixian',
            op: 'range',
            value: `${$moment().format("YYYY-MM-DD")},${preDate}`
          },
          {
            key: 'zulinkaishiqixian',
            op: 'range',
            value: ``
          }
        ]
      }).then(res => {
        commit("SET_ADVENT_LEASES", res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  getSafety({ commit, state }) {
    return new Promise((resolve, reject) => {
      index({
        table_name: "asset_safety_inspections",
        page: 1,
        page_size: 999
      }).then(res => {
        commit("SET_SAFETY", res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  getSafetyPlan({ commit, state }) {
    return new Promise((resolve, reject) => {
      index({
        table_name: "asset_safety_plans",
        page: 1,
        page_size: 999,
        filter: [
          {
            key: "jihuajieshushijian",
            op: "range",
            value: `${$moment().format("YYYY-MM-DD")},2999-12-31`
          },
          {
            key: "jihuakaishishijian",
            op: "range",
            value: `${$moment(0).format("YYYY-MM-DD")},${$moment().format("YYYY-MM-DD")}`
          }
        ]
      }).then(res => {
        commit("SET_SAFETY_PLAN", res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  async getAdventSafetyPlan({ commit, state }) {
    if (!config.time || !config.unit) {
      const res = await index({
        table_name: 'warnings',
        filter: [
          {
            key: 'flag',
            op: 'eq',
            value: 'contract'
          }
        ]
      },false)
      config.time = Number(res.data[0]?.time);
      config.unit = res.data[0]?.unit;
    }
    return new Promise((resolve, reject) => {
      let preDate = $moment().add(config.time, config.unit).format("YYYY-MM-DD")
      index({
        table_name: "asset_safety_plans",
        page: 1,
        page_size: 999,
        filter: [
          {
            key: "jihuajieshushijian",
            op: "range",
            value: `${$moment().format("YYYY-MM-DD")},${preDate}`
          },
          {
            key: "jihuakaishishijian",
            op: "range",
            value: ``
          }
        ]
      }).then(res => {
        commit("SET_ADVENT_SAFETY_PLAN", res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

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

