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

const __staticUrl = require('../../../config/env.js').static_url
Page({
    data: {
        newOrderTotal: 200,
        s: app.system.statusBarHeight, // 状态栏高度
        n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, // 导航栏高度
        t: app.menu.top, // 胶囊局顶部距离
        h: app.menu.height, // 胶囊高度
        currentType: "",
        option1: [
            { text: '新装工单', value: 0 },
            { text: '维修工单', value: 1 },
            { text: '拆机工单', value: 3 },
            { text: '移机工单', value: 4 },
            { text: '滤芯更换工单', value: 2 },
        ],
        active: 0,
        searchText: "",

        value1: 0,
        page_num: 1,
        page_size: 10,
        flag: false,
        searchCon: "",
        NewOrderList: [], //新装工单
        RepairOrderList: [],//维修工单
        PartOrderList: [],//滤芯跟换工单
        UninstallOrderList: [],//拆卸工单
        RemoveList: [],//移机工单



        info_id: "",//师傅id
    },
    back(){
        wx.reLaunch({
          url: '/packages/master/masterIndex/index',
        })
    },
    search() {
        let that = this
        that.setData({
            NewOrderList: [],
            RepairOrderList: [],
            PartOrderList: [],
            UninstallOrderList: [],
            RemoveList: [],
            page_num: 1,
            flag: false
        })
        if (that.data.active == 0) {
            that.getNewOrderList()
        } else if (that.data.active == 1) {
            that.gerRepairOrderList()
        } else if (that.data.active == 3) {
            that.getPartOrderList()
        } else if (that.data.active == 2) {
            this.getUninstallOrder()
        }
    },
    ipt1(e) {
        let that = this
        that.setData({
            searchText: e.detail.value
        })
    },
    changeActive(e) {
        let that = this
        that.setData({
            active: e.currentTarget.dataset.item.value,
            page_num: 1,
            NewOrderList: [],
            RepairOrderList: [],
            PartOrderList: [],
            UninstallOrderList: [],
        })

        if (that.data.active == 1) {
            that.getNewOrderList()
        } else if (that.data.active == 2) {
            that.gerRepairOrderList()
        } else if (that.data.active == 4) {
            that.getPartOrderList()
        } else if (that.data.active == 3) {
            this.getUninstallOrder()
        }

    },

    // 查看工单详情
    viewOrderDetail(e) {
        let that = this
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/packages/master/orderDesc/index?id=${id}&type=${that.data.active}`
        });
    },

    // 完成订单
    completeOrder(e) {
        const orderId = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确认完成该订单吗？',
            success: (res) => {
                if (res.confirm) {
                    this.removeOrder(orderId);
                }
            }
        });
    },

    // 移除已完成的订单
    removeOrder(orderId) {
        const newOrderList = this.data.orderList.filter(item => item.id !== orderId);
        this.setData({
            orderList: newOrderList
        });
        wx.showToast({
            title: '订单已完成',
            icon: 'success'
        });
    },

    onLoad(options) {
        let currentType = ''
        if (options.type == 0) {
            currentType = '新装工单'
            wx.setNavigationBarTitle({
                title: '新装工单',
            })
        } else if (options.type == 1) {
            currentType = '维修工单'
            wx.setNavigationBarTitle({
                title: '维修工单',
            })
        } else if (options.type == 3) {
            currentType = '拆除工单'
            wx.setNavigationBarTitle({
                title: '拆除工单',
            })
        } else if (options.type == 2) {
            currentType = '滤芯更换工单'
            wx.setNavigationBarTitle({
                title: '滤芯更换工单',
            })
        }
        // 移除原有的wx.setNavigationBarRightButton相关代码
        let that = this
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
        that.setData({
            currentType,
            active: options.type
        })
    },
    onShow() {
        let that = this
        that.setData({
            flag: false,
            NewOrderList: [],
            RepairOrderList: [],
            PartOrderList: [],
            UninstallOrderList: [],
            page_num: 1,
            searchText: ""
        })

        that.getMasterInfo()
    },


    // 跳转到个人中心
    navigateToProfile() {
        wx.navigateTo({
            url: '/packages/master/PersonCen/index'
        });
    },

    //新装工单
    getNewOrderList() {
        let that = this
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        util.postUrl(apiArr.getNewOrder, {
            info_id: that.data.info_id,
            search: that.data.searchCon,
            page_num: that.data.page_num,
            page_size: that.data.page_size,
            search: that.data.searchText,
        }, res => {
            wx.hideLoading()
            if (res.msg == '操作成功') {
                let flag = false
                if (res.rows && res.rows.length == that.data.page_size) {
                    flag = true
                } else {
                    flag = false
                }
                that.setData({
                    NewOrderList: that.data.NewOrderList.concat(res.rows || []),
                    page_num: that.data.page_num + 1,
                    newOrderTotal: res.total,
                    flag
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        })
    },
    //维修工单
    gerRepairOrderList() {
        let that = this
        wx.showLoading({
            title: '加载中...',
            mask: true

        })
        util.postUrl(apiArr.gerRepairOrder, {
            info_id: that.data.info_id,
            page_num: that.data.page_num,
            page_size: that.data.page_size,
            search: that.data.searchText
        }, res => {
            wx.hideLoading()
            if (res.msg == '操作成功') {
                let flag = false
                if (res.rows && res.rows.length == that.data.page_size) {
                    flag = true
                } else {
                    flag = false
                }
                that.setData({
                    RepairOrderList: that.data.RepairOrderList.concat(res.rows || []),
                    page_num: that.data.page_num + 1,
                    flag
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        })
    },
    //滤材更换工单
    getPartOrderList() {
        let that = this
        wx.showLoading({
            title: '加载中...',
            mask: true

        })
        util.postUrl(apiArr.getPartOrder, {
            info_id: that.data.info_id,
            page_num: that.data.page_num,
            page_size: that.data.page_size,
            search: that.data.searchText
        }, res => {
            wx.hideLoading()
            if (res.msg == '操作成功') {
                let flag = false
                if (res.rows && res.rows.length == that.data.page_size) {
                    flag = true
                } else {
                    flag = false
                }
                that.setData({
                    PartOrderList: that.data.PartOrderList.concat(res.rows || []),
                    page_num: that.data.page_num + 1,
                    flag
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        })
    },
    //拆除工单
    getUninstallOrder() {
        let that = this
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        util.postUrl(apiArr.getUninstallOrder, {
            info_id: that.data.info_id,
            status: 0,
            page_num: that.data.page_num,
            page_size: that.data.page_size,
            search: that.data.searchText
        }, res => {
            wx.hideLoading()
            if (res.msg == '操作成功') {
                let flag = false
                if (res.rows && res.rows.length == that.data.page_size) {
                    flag = true
                } else {
                    flag = false
                }
                that.setData({
                    UninstallOrderList: that.data.UninstallOrderList.concat(res.rows || []),
                    page_num: that.data.page_num + 1,
                    flag
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        })
    },

    //师傅信息
    getMasterInfo() {
        let that = this
        util.postUrl(apiArr.masterInfo, {
            user_id: wx.getStorageSync('userId')
        }, res => {
            that.setData({
                info_id: res.info_id
            })
            wx.setStorageSync('info_id', res.info_id)
            that.searchIcon()
        })
    },

    searchIcon() {

        let that = this
        that.setData({
            flag: false,
            NewOrderList: [],
            RepairOrderList: [],
            PartOrderList: [],
            UninstallOrderList: [],
            page_num: 1
        })
        if (that.data.active == 0) {
            that.getNewOrderList()
        } else if (that.data.active == 1) {
            that.gerRepairOrderList()
        } else if (that.data.active == 2) {
            that.getPartOrderList()
        } else if (that.data.active == 3) {
            this.getUninstallOrder()
        }
    },

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

    onReachBottom() {
        let that = this
        if (that.data.flag) {
            that.getMasterInfo()
        }
    },
    onPullDownRefresh() {
        let that = this
        that.setData({
            flag: false,
            NewOrderList: [],
            RepairOrderList: [],
            PartOrderList: [],
            UninstallOrderList: [],
            page_num: 1,
            searchText: ""
        })
        that.getMasterInfo()
        wx.stopPullDownRefresh();
    },

    //新增维修工单
    addWorkOrder(){
        wx.navigateTo({
          url: '/packages/master/addNewOrder/index',
        })
    },

});