// http://gavin0212.imwork.net/3rd/bigScreenData/bigScreenMessage?gId=133783305191425&cId=133783305191425&path=fireData1
//     http://gavin0212.imwork.net/3rd/bigScreenData/bigScreenMessage?gId=133783305191425&cId=133783305191425&path=fireData2
import {dateUtils} from 'happy-utils'

/**
 * 1：用户信息传输装置     这个就是网站上的:火灾自动报警系统
 2：电气火灾监控器                        电气火灾智能监控系统
 3：液位
 4：水压                 3和4就是网站上的: 消防给水监测系统
 5：燃气                 本次用不到
 6：无线烟感                                 无线火灾报警监控系统
 * @param params
 * @returns {AxiosPromise}
 */

import {getEquipmentMonitoringData} from '@/api/smart_fire'

const getDefaultState = () => {
    return {
        freAlarmData: null, // 火灾自动报警系统
        fireWaterData: null,    // 消防给水监测系统
        eleFireData: null,   // 电气火灾智能监控系统
        gasMoData: null,    // 可燃气体检测预警系统
        alarmMoData: null,    // 无线火灾报警监控系统
    }
}

const state = getDefaultState()

const mutations = {
    RESET_freAlarmData: (state, data) => {
        state.freAlarmData = data
    },
    RESET_fireWaterData: (state, data) => {
        state.fireWaterData = data
    },
    RESET_fireWaterData_fireData3: (state, data) => {
        let temp = state.fireWaterData;
        if (temp) {
            state.fireWaterData = {
                fireData3: data,
                fireData4: temp.fireData4
            }
        } else {
            state.fireWaterData = {
                fireData3: data,
                fireData4: null
            }
        }

    },
    RESET_fireWaterData_fireData4: (state, data) => {
        let temp = state.fireWaterData;
        if (temp) {
            state.fireWaterData = {
                fireData3: temp.fireData3,
                fireData4: data
            }
        } else {
            state.fireWaterData = {
                fireData3: null,
                fireData4: data
            }
        }

    },
    RESET_eleFireData: (state, data) => {
        state.eleFireData = data
    },
    RESET_gasMoData: (state, data) => {
        state.gasModata = data
    },
    RESET_alarmMoData: (state, data) => {
        state.alarmMoData = data
    },
}

const actions = {
    /**
     * 火灾自动报警系统
     * @param params
     * @returns {AxiosPromise}
     */
    getFireAlarmData({commit, state}, params) {
        return new Promise((resolve, reject) => {
            getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData1'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                    }))
                }
                commit('RESET_freAlarmData', parseData)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    /**
     * 消防给水监测系统
     * @param params
     * @returns {AxiosPromise}
     */
    getFireWaterData({commit, state}, params) {
        return new Promise(async (resolve, reject) => {
            let setData = {};
            await getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData3'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                        if (item.last7DayDatas) {
                            let names = [], values = [];
                            item.last7DayDatas.forEach((cItem) => {
                                names.push(dateUtils.dateFormat(new Date(cItem.occurTime), 'yyyy-MM-dd'))
                                // values.push(cItem.actVal.replace(item.viewInfo.unit,''))
                                values.push(cItem.actVal.replace(item.viewInfo.unit, ''))
                            })
                            item.names = names;
                            item.values = values;
                        }
                    }))
                }
                setData.fireData3 = parseData
            }).catch(error => {
                reject(error)
            })

            await getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData4'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                        if (item.last7DayDatas) {
                            let names = [], values = [];
                            item.last7DayDatas.forEach((cItem) => {
                                names.push(dateUtils.dateFormat(new Date(cItem.occurTime), 'yyyy-MM-dd'))
                                // values.push(cItem.actVal.replace(item.viewInfo.unit,''))
                                values.push(cItem.actVal.replace(item.viewInfo.unit, ''))
                            })
                            item.names = names;
                            item.values = values;
                        }
                    }))

                }
                setData.fireData4 = parseData
            }).catch(error => {
                reject(error)
            })

            commit('RESET_fireWaterData', setData)
            resolve()
        })
    },

    /**
     * 电气火灾智能监控系统
     * @param params
     * @returns {AxiosPromise}
     */
    getEleFireData({commit, state}, params) {
        return new Promise((resolve, reject) => {
            getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData2'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                    }))
                }
                commit('RESET_eleFireData', parseData)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    /**
     * 可燃气体检测预警系统
     * @param params
     * @returns {AxiosPromise}
     */
    getGasMoData({commit, state}, params) {
        return new Promise((resolve, reject) => {
            getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData5'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                    }))
                }
                commit('RESET_gasMoData', parseData)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    /**
     * 无线火灾报警监控系统
     * @param params
     * @returns {AxiosPromise}
     */
    getAlarmMoData({commit, state}, params) {
        return new Promise((resolve, reject) => {
            getEquipmentMonitoringData(params.baseUrl, {
                gId: params.gId,
                cId: params.cId,
                path: 'fireData6'
            }).then(response => {
                const {data} = response
                let parseData = JSON.parse(data);
                if (parseData.fireData) {
                    parseData.fireData[0].forEach((item => {
                        item.viewInfo = JSON.parse(item.viewInfo);
                    }))
                }
                commit('RESET_alarmMoData', parseData)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
}

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



