const _Http = getApp().globalData.http;
Page({
    data: {
        "content": {
            "sa_projectid": 1,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        }
    },
    onLoad(options) {
        this.data.content.sa_projectid = options.sa_projectid;
        this.getList()
        getApp().globalData.Language.getLanguagePackage(this, '审核授权');
    },
    /* 获取列表 */
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": 20230816124004,
                content
            }).then(res => {
                console.log("授权列表", res)
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                this.setData({
                    loading: false,
                    content: _Http.paging(content, res),
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
                })
            })
        })
    },
    preview(e) {
        const {
            item
        } = e.currentTarget.dataset;
        if (this.compareDates(item.begdate_auth, item.enddate_auth)) return;
        _Http.basic({
            "id": 20221213094501,
            "content": {
                "sys_reportid": 104,
                "dataid": item.sa_projectid,
                sys_enterpriseid: item.enterpriseid_auth,
            }
        }).then(res => {
            console.log("查看授权书", res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            let url = `${_Http.baseUrl+res.data}&enterprisename=${item.enterprisename}&begdate=${item.begdate_auth}&enddate=${item.enddate_auth}`
            wx.navigateTo({
                url: `/packageA/project/impower/webview`,
            })
            this.setData({
                url
            })
        })
    },
    dispose(e) {
        const {
            item
        } = e.target.dataset,
            that = this;
        if (e.target.id == 1 && this.compareDates(item.begdate_auth, item.enddate_auth)) return;
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.joint([{
                v: '该项目授权申请确认',
                t: 1
            }, {
                v: e.target.id == 1 ? '通过' : '不通过',
                t: 1,
                r: '？',
                f: " "
            }]),
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) _Http.basic({
                    "id": "20230810161804",
                    "content": {
                        "sa_project_authorizationid": item.sa_project_authorizationid,
                        "begdate": item.begdate_auth,
                        "enddate": item.enddate_auth,
                        "isPass": e.target.id
                    }
                }).then(res => {
                    console.log(e.target.id == 1 ? '通过' : '不通过', res)
                    wx.showToast({
                        title: res.code == '1' ? getApp().globalData.Language.getMapText('操作成功') : res.msg,
                        icon: "none"
                    });
                    if (res.code == '1') this.setData({
                        list: that.data.list.filter(v => v.sa_project_authorizationid != item.sa_project_authorizationid)
                    })
                })
            }
        })
    },
    onChangeDate(e) {
        const {
            index,
            item,
            name
        } = e.currentTarget.dataset;
        item[name] = e.detail.value;
        this.setData({
            [`list[${index}]`]: item
        })
    },
    compareDates(d1, d2) {
        let date1 = new Date(d1).getTime();
        let date2 = new Date(d2).getTime();
        if (date1 > date2) getApp().globalData.Language.modeBoxPrompts('请检查授权书有效期，开始时间不能晚于结束时间')
        return date1 > date2
    }
})