import Vue from 'vue'
import App from './App'
import './uni.promisify.adaptor'
import MqttService from "./utils/globalMqtt";

import {
    http
} from "./components/js_sdk/index";

import alarModel from "./components/component/alarModel.vue";
import tenMinutes from './components/component/tenMinutes.vue';

Vue.component('alarModel', alarModel);
Vue.component('tenMinutes', tenMinutes);

Vue.prototype.$http = http;

// 全局方法：初始化 MQTT（登录成功时调用）
Vue.prototype.$initMqtt = function () {
    const userId = uni.getStorageSync("userId");
    if (!userId) {
        console.log("MQTT 初始化失败：缺少 userId");
        return;
    }
    // 连接 DATA
    // 如果已经连接过 DATA，直接返回
    if (MqttService.dataConnected) {
        uni.$emit('mqttData-ready', MqttService.dataClient);
        uni.$emit('mqttDataReady', MqttService.dataClient);
        return Promise.resolve(MqttService.dataClient);
    }
    return MqttService.connectData(userId)
        .then(client => {
            uni.$emit('mqttDataReadyOnce', this.dataClient);
            uni.$emit('mqttDataReady', this.dataClient);
            console.log("DATA MQTT 初始化完成", client);
        })
        .catch(err => {
            console.error("DATA MQTT 初始化失败", err);
        });
}


// 挂载处理时间的方法
function timestampToTime(value, type = 0) {
    var time = new Date(value);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hour = time.getHours();
    var minute = time.getMinutes();
    var second = time.getSeconds();

    // 补零
    month = month < 10 ? "0" + month : month;
    date = date < 10 ? "0" + date : date;
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;

    var arr = [
        year + "-" + month + "-" + date,
        year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
        year + "年" + month + "月" + date + "日",
        year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
        hour + ":" + minute + ":" + second,
        year + "-" + month + "-" + date + " " + hour + ":" + minute,
        month + "月" + date + "日" + hour + ":" + minute,
    ]
    return arr[type];
}

Vue.prototype.$time = timestampToTime;


Vue.config.productionTip = false;
if (__wxConfig.envVersion == 'develop') {
    Vue.prototype.uploadimg = 'https://api.radar-power.asia:4443';

}
if (__wxConfig.envVersion == 'trial') {
    Vue.prototype.upload = 'https://api.radar-power.asia:4443';
}
if (__wxConfig.envVersion == 'release') {
    Vue.prototype.uploadimg = 'https://api.radar-power.asia:4443';
}
App.mpType = "app";

const app = new Vue({
    ...App,
});
app.$mount();
