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

// packages/WaterPurifier/pages/upKeep/upKeep.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, // 胶囊高度
        admin: "",
        value1: 0,
        value2: 'a',
        show: false,
        MasterList: [

        ],

        searchText: "",//搜索文字
        page_num: 1,
        page_size: 10,
        total: '',
        deviceList: [],
        sendOrders: {}, // 派单订单
        flag: false,
        admin: "",
        show: false,
        show2: false,
        masterList: [],
        currentMaster: "",
        radio: "",
        currentOrder: "",
        radio: "",
    },

    back(){
        wx.reLaunch({
          url: '/packages/partner/pages/partner',
        })
    },
    onConfirms() {
        let that = this
        if (!that.data.currentMaster && !that.data.currentMaster.info_id) {
            return wx.showToast({
                title: '请选择师傅',
                icon: "none"
            })
        }

        util.postUrl4(apiArr.assignPartReplaceMaster, {
            replace_id: that.data.currentOrder.replace_id,
            master_id: that.data.currentMaster.info_id,
            is_up: that.data.radio,
            address: that.data.address,
        }, res => {
            wx.showToast({
                title: '分配师傅成功',
                icon: "none"
            })
            that.setData({
                show: false
            })
            setTimeout(() => {
                that.getNewOrderList()
            }, 1500)
        })

    },
    onChange2(e) {
        let that = this
        that.setData({
            radio: e.detail
        })
    },
    onConfirm(e) {
        console.log(e);
        let that = this
        that.setData({
            currentMaster: e.detail.value,
            show2: false
        })
    },
    onClose2() {
        let that = this
        that.setData({
            show2: false
        })
    },

    getMasterList() {
        let that = this
        util.postUrl4(apiArr.masterSelect, {
            dealer_id: that.data.admin.shop_id
        }, res => {
            console.log(res.rows);
            that.setData({
                masterList: res.rows
            })
        })
    },

    chooseMaster() {
        let that = this
        that.setData({
            show2: true
        })
    },
    dispatch(e) {
        let that = this
        console.log(e);
        that.setData({
            show: true,
            currentOrder: e.currentTarget.dataset.item,
            address: e.currentTarget.dataset.item.address,
        })
    },
    onClose() {
        let that = this
        that.setData({
            show: false
        })
    },
    desc(e) {
        let that = this
        wx.navigateTo({
            url: `/packages/partner/pages/OrderMsgInfo/OrderMsgInfo?id=${e.currentTarget.dataset.id}&type=2`,
        })
        // wx.navigateTo({
        //     url: `../partsOrderInfo/partsOrderInfo?id=${e.currentTarget.dataset.id}`,
        // })
    },

    ipt1(e) {
        let that = this
        that.setData({
            searchText: e.detail.value,
        })
    },

    search() {
        let that = this
        that.setData({
            page_num: 1,
            flag: false,
            deviceList: []
        })
        that.getNewOrderList()
    },


    //工单列表
    getNewOrderList() {
        let that = this
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })


        //设置加载状态
        util.postUrl4(apiArr.getPartList, {
            page_size: that.data.page_size,
            page_num: that.data.page_num,
            dealer_id: that.data.admin.shop_id,
            search:that.data.searchText
        }, res => {
            wx.hideLoading()
            that.getMasterList()
            if (res.rows) {
                let flag = false
                if (res.rows.length == that.data.page_size) {
                    flag = true
                } else {
                    flag = false
                }
                that.setData({
                    flag,
                    deviceList: that.data.deviceList.concat(res.rows || []),
                    page_num: that.data.page_num + 1
                })
            }
        })
    },





    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.setData({
            admin: wx.getStorageSync('admin')
        })
        that.getNewOrderList()
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

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

    },

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

    },

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

    },

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

    },

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

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

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