const _Http = getApp().globalData.http;
import currency from "../../utils/currency";

Page({
    data: {
        loading: true,
        active: "新建",
        "content": {
            nocache: true,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            "isManage": 0, //默认是1，表示管理端，0表示非管理端，根据用户类型自动区分业务员还是经销商
            "isExport": 0,
            total: 0,
            "where": {
                "condition": "",
            },
            sort: []
        },
        createShow: false,
        types: [{
            name: "按借用合同借用"
        }, {
            name: "单个借用"
        }]
    },
    onLoad(options) {
        this.getList(true)
        this.setData({
            userrole: wx.getStorageSync('userrole')
        })
    },
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        content.where.status = this.data.active;
        _Http.basic({
            "id": 20230114105002,
            "version": 1,
            content
        }).then(res => {
            console.log("工具借用列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            res.data = res.data.map(v => {
                v.amount = currency(v.amount, {
                    symbol: "￥",
                    precision: 2
                }).format()
                return v
            })
            this.setData({
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                "content.pageNumber": res.pageNumber + 1,
                "content.pageTotal": res.pageTotal,
                "content.sort": res.sort,
                "content.total": res.total,
                loading: false
            })
        })
    },
    /* 去新增 */
    openAction() {
        this.setData({
            createShow: true
        })
    },
    createBor({
        detail
    }) {
        this.onCancel();
        if (detail.name == '按借用合同借用') {
            wx.navigateTo({
                url: `/select/contract/index?params=${JSON.stringify({
                    "id": 20230114092502,
                    "content": {
                        nocache: true,
                        "pageNumber": 1,
                        "pageTotal": 1,
                        "total": null,
                        "pageSize": 20,
                        typemx:"合同借用",
                        "where": {
                            "condition": ""
                        }
                    }
                })}&radio=false`
            })
            getApp().globalData.handleSelect = this.handleCreated.bind(this);
        } else {
            if (this.data.userrole == '业务员') {
                wx.navigateTo({
                    url: `/select/agent/index?params=${JSON.stringify({
                                "id": 20220920083901,
                                "content": {
                                    nocache:true,
                                    "where": {
                                            "condition": "",
                                            "type":9,
                                    }
                                }
                        })}&radio=true`,
                })
                getApp().globalData.handleSelect = this.handleSingle.bind(this);
            } else {
                _Http.basic({
                    "id": 20230116092702,
                    "content": {
                        "sa_orderid": 0
                    }
                }).then(res => {
                    console.log("按个", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    wx.navigateTo({
                        url: '/packageA/borrow/detail?id=' + res.data.sa_orderid,
                    })
                })
            }
        }
    },
    handleSingle({
        item
    }) {
        wx.showModal({
            title: '提示',
            content: `是否确定选择"${item.enterprisename}"创建工具借用单？`,
            complete: (res) => {
                if (res.confirm) _Http.basic({
                    "id": 20230116092702,
                    "content": {
                        "sa_orderid": 0,
                        sys_enterpriseid: item.sys_enterpriseid
                    }
                }).then(res => {
                    console.log("按个", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    wx.redirectTo({
                        url: '/packageA/borrow/detail?id=' + res.data.sa_orderid,
                    })
                })
            }
        })

    },
    handleCreated(e) {
        wx.showModal({
            title: '提示',
            content: `是否确认选择“${e.item.title}”创建借用单`,
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": 20230116092702,
                    "content": {
                        "sa_contractid": e.id[0],
                        "sa_orderid": 0
                    }
                }).then(res => {
                    console.log("按套", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                    _Http.basic({
                        "id": 20230116142602,
                        "content": {
                            "sa_orderid": res.data.sa_orderid,
                            "sys_enterpriseid": res.data.sys_enterpriseid,
                            "sa_contractid": e.id[0]
                        }
                    }, false).then(s => {
                        console.log("复制合同商品", s)
                        wx.redirectTo({
                            url: '/packageA/borrow/detail?id=' + res.data.sa_orderid,
                        })
                    })
                })
            }
        })
    },
    onCancel() {
        this.setData({
            createShow: false
        })
    },
    /* 搜索 */
    onSearch({
        detail
    }) {
        this.setData({
            "content.where.condition": detail
        });
        this.getList(true)
    },
    /* 切换tabs */
    tabsChange(e) {
        let status = "";
        switch (e.detail.title) {
            case '全部':
                status = "";
                break;
            case '待确认':
                status = "交期待确认";
                break;
            default:
                status = e.detail.title
                break;
        }
        this.setData({
            active: status
        });
        this.getList(true);
    },
    onReady() {
        this.setListHeight()
    },
    /* 设置页面高度 */
    setListHeight() {
        this.selectComponent("#ListBox").setHeight(".total", this);
    },
})