let util = require('../../../../utils/util')
const apiArr = require('../../../../api/water_filter')
const apiArr2 = require('../../../../api/partner')
const app = getApp({ allowDefault: true })

// packages/WaterPurifier/pages/device/deviceList/deviceList.js
const __staticUrl = require('../../../../config/env.js').static_url
Page({

    /**
     * 页面的初始数据
     */
    data: {
        s: app.system.statusBarHeight, // 状态栏高度
        n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, // 导航栏高度
        t: app.menu.top, // 胶囊局顶部距离
        h: app.menu.height, // 胶囊高度

        option1: [
            { text: '全部', value: 0 },
            { text: '已安装', value: 1 },
            { text: '空闲', value: 2 },
            { text: '已拆机', value: 3 },
        ],
        option2: [
            { text: '全部', value: 0 },
            { text: '在用', value: 1 },
            { text: '待启用', value: 2 },
            { text: '关机', value: 3 },
            { text: '已拆机', value: 4 },
            { text: '漏水', value: 5 },
            { text: '制水故障', value: 6 },
            { text: '报废', value: 7 },
            { text: '已欠费', value: 8 },
            { text: '在库', value: 9 },
            { text: '激活失败', value: 10 },
        ],
        stateType: {
            1: '在用',
            2: '待启用',
            3: '关机',
            4: '已拆机',
            5: '漏水',
            6: '制水故障',
            7: '报废',
            8: '已欠费',
            9: '在库',
            10: '激活失败'
        },
        value1: 0,
        value2: 0,
        nav_list: {},
        foot_width: "",

        searchText: "",//搜索文字
        page_num: 1,
        page_size: 10,
        flag: false,
        total: '',
        deviceList: [],
        dropdownOpen: false,
        text1:'安装状态：全部',
        text2:"设备状态：全部"

    },
    back(){
        wx.reLaunch({
          url: '/packages/partner/pages/mine/mine',
        })
    },
    onDropdownOpen() {
        console.log(123);
        this.setData({ dropdownOpen: true });
    },
    onDropdownClose() {
        this.setData({ dropdownOpen: false });
    },
    ipt1(e) {
        let that = this
        that.setData({
            searchText: e.detail.value,
        })
    },

    deviceDesc(e) {
        wx.navigateTo({
            url: `../deviceDesc/deviceDesc?device_id=${e.currentTarget.dataset.device_id}`,
        })
    },
    addWorkOrders() {
        console.log(123123);
        wx.navigateTo({
            url: '/packages/partner/pages/newWorkOrder/newWorkOrder',
        })
    },
    //安装状态
    headerNetWorkClick(e) {
        let that = this
        let text1 = ''
        if(e.detail == 0){
            text1 = '安装状态：全部'
        }else if(e.detail == 1){
            text1 = '安装状态：已安装'
        }else if(e.detail == 2){
            text1 = '安装状态：空闲'
        }else if(e.detail == 3){
            text1 = '安装状态：已拆机'
        }

        this.setData({
            value1: e.detail,
            page_num: 1,
            deviceList: [],
            text1
        });
        if (e.detail == 0) {
            this.getDeviceList(0,that.data.value2);
            return;
        }
        this.getDeviceList(e.detail, that.data.value2);
    },

    // 设备状态
    headerEquipmentClick(e) {
        let that = this
        let text2 = ''
        if(e.detail == 0){
            text2 = '设备状态：全部'
        }else if(e.detail == 1){
            text2 = '设备状态：在用'
        }else if(e.detail == 2){
            text2 = '设备状态：待启用'
        }else if(e.detail == 3){
            text2 = '设备状态：关机'
        }else if(e.detail == 4){
            text2 = '设备状态：已拆机'
        }else if(e.detail == 5){
            text2 = '设备状态：漏水'
        }else if(e.detail == 6){
            text2 = '设备状态：制水故障'
        }else if(e.detail == 7){
            text2 = '设备状态：报废'
        }else if(e.detail == 8){
            text2 = '设备状态：已欠费'
        }else if(e.detail == 9){
            text2 = '设备状态：在库'
        }else if(e.detail == 10){
            text2 = '设备状态：激活失败'
        }
        this.setData({
            value2: e.detail,
            page_num: 1,
            deviceList: [],
            text2
        });
        if (e.detail == 0) {
            this.getDeviceList(that.data.value1,0);
            return;
        }
        this.getDeviceList(that.data.value1, e.detail);
    },

    search2() {
        let that = this
        that.setData({
            deviceList: [],
            flag: false,
            page_num: 1
        })
        that.getDeviceList('', '')
    },
    getDeviceList(networkStatus, status) {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        let that = this
        let admin = wx.getStorageSync('admin')
        let param = {
            page_size: that.data.page_size,
            page_num: that.data.page_num,
            search: that.data.searchText,
            dealer_id: admin.shop_id
        };
        // if (networkStatus) {
        //     param = {
        //         ...param,
        //         install_status: networkStatus
        //     }
        // }
        // if (status) {
        // }
        param = {
            ...param,
            status: Number(status),
            install_status: networkStatus
        }

        util.postUrl4(apiArr2.deviceList, param, res => {
            wx.hideLoading()
            let flag = false
            if (res.list && res.list.length == that.data.page_size) {
                console.log(123);
                flag = true
            } else {
                console.log(456);
                flag = false
            }
            that.setData({
                deviceList: that.data.deviceList.concat(res.list || []),
                total: res.total,
                flag,
                page_num: that.data.page_num + 1
            })
            console.log(that.data.flag);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDeviceList();
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this
        that.setData({
            flag: false,
            page_num: 1,
            total: 0,
            searchText: "",
            value1: "",
            value2: "",
            deviceList: []
        })
        that.getDeviceList();
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this
        if (that.data.flag) {
            that.getDeviceList('', that.data.value2)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: '人人爱净水',  // 分享卡片标题（必填）
            path: '/pages/water_filter/water_filter',  // 用户点击后跳转的路径（可选，默认当前页）
            imageUrl: (__staticUrl + '/share.png')  // 自定义图片（可选，比例建议 5:4）
        }
    },
    onShareTimeline() {
        return {
            title: '人人爱净水', // 自定义标题
            query: '', // 自定义页面路径中的参数
            imageUrl: (__staticUrl + '/share.png') // 自定义分享图片路径
        }
    },
})