const _Http = getApp().globalData.http;
let sa_projectid = 0;
import {
    formatTime
} from "../../../utils/getTime";
Page({
    data: {
        disabled: true,
        loading: false
    },
    onLoad(options) {
        this.setData({
            form: [{
                label: "项目名称",
                error: false,
                errMsg: "",
                type: "number",
                value: options.projectname,
                placeholder: "项目名称",
                valueName: "projectname",
                checking: "base",
                required: true,
                disabled: true
            }, {
                label: "客户名称",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/setclient/select",
                value: "",
                params: {
                    "id": 20221027143702,
                    "content": {
                        sa_projectid: options.sa_projectid,
                        "pageSize": 20,
                        "where": {
                            "condition": "",
                        }
                    },
                },
                query: "&radio=true&idname=sys_enterpriseid",
                placeholder: "选择客户",
                valueName: "sys_enterpriseid",
                checking: "base",
                required: true
            }, {
                label: "有效期",
                error: false,
                errMsg: "",
                type: "dateRange",
                start: "",
                value: ["", ""],
                placeholder: ['生效日期', '截止日期'],
                valueName: "invaliddate",
                checking: "base",
                required: true
            }]
        })
        sa_projectid = options.sa_projectid;
        getApp().globalData.Language.getLanguagePackage(this, '发起授权');
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    cancel() {
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确定取消发起授权'),
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: (res) => {
                if (res.confirm) wx.navigateBack()
            }
        })
    },
    preview() {
        let data = this.selectComponent("#Form").submit();
        _Http.basic({
            "id": 20221213094501,
            "content": {
                "sys_reportid": 104,
                "dataid": sa_projectid,
                sys_enterpriseid: data.sys_enterpriseid[1][0],
            }
        }).then(res => {
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            let url = `${_Http.baseUrl+res.data}&enterprisename=${data.sys_enterpriseid[0]}&begdate=${data.invaliddate[0]}&enddate=${data.invaliddate[1]}`
            wx.navigateTo({
                url: `/packageA/project/impower/webview`,
            })
            this.setData({
                url
            })
        })
    },
    submit() {
        let that = this;
        this.setData({
            loading: true
        })
        wx.showModal({
            title: getApp().globalData.Language.getMapText('提示'),
            content: getApp().globalData.Language.getMapText('是否确定发起授权'),
            cancelText: getApp().globalData.Language.getMapText('取消'),
            confirmText: getApp().globalData.Language.getMapText('确定'),
            complete: ({
                confirm
            }) => {
                if (confirm) {
                    let data = this.selectComponent("#Form").submit();
                    _Http.basic({
                        "id": 20230810161704,
                        "content": {
                            sa_projectid,
                            "begdate": data.invaliddate[0],
                            "enddate": data.invaliddate[1],
                            "sys_enterpriseid": data.sys_enterpriseid[1][0]
                        }
                    }).then(res => {
                        console.log("发起授权", res)
                        that.setData({
                            loading: false
                        })
                        wx.showToast({
                            title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('发起成功'),
                            icon: "none"
                        });
                        if (res.code == '1') return setTimeout(() => {
                            wx.navigateBack()
                        }, 500)
                    })
                }
            }
        })
    }
})