const _Http = getApp().globalData.http;
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min'),
    qqmapsdk = new QQMapWX({
        key: 'QF6BZ-4IQC5-SN2IH-QL5BW-U4IJO-R3BY4' // 必填
    });
import {
    formatTime
} from "../../utils/getTime";

let timing = null;
Page({
    data: {
        markers: [],
        content: {},
        time: formatTime(new Date(), '-'),
        form: null,
        continue1: null,
    },
    onLoad(options) {
        this.getLocation();
        timing = setInterval(() => {
            this.setData({
                time: formatTime(new Date(), '-')
            })
        }, 1000);
        this.rander();
    },
    submit() {
        let data = this.selectComponent("#Form").submit(),
            content = JSON.parse(JSON.stringify(this.data.content)),
            that = this;
        content.remarks = data.remarks;
        if (data.sys_enterprise) content.sys_enterpriseid = data.sys_enterprise[1][0];
        if (data.sa_project) content.sa_projectid = data.sa_project[1][0];
        console.log(content)
        wx.showModal({
            title: '提示',
            content: '是否确认本次签到',
            complete: ({
                confirm
            }) => {
                if (confirm) that.onConfirm(content);
            }
        })
    },
    onConfirm(content) {
        _Http.basic({
            id: 20221229145901,
            content
        }).then(res => {
            console.log("签到", res)
            wx.showToast({
                title: res.msg == '成功' ? '签到成功' : res.msg,
                icon: "none"
            })
            if (res.msg == '成功') setTimeout(() => {
                getCurrentPages()[getCurrentPages().length - 2].toDate(this.data.time.split(" ")[0]);
                wx.navigateBack();
            }, 500)
        })
    },
    rander() {
        this.setData({
            form: [{
                label: "客户",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/setclient/select",
                value: "",
                params: {
                    id: 20221012164402,
                    content: {
                        nocache: true,
                        type: 0,
                        isExport: 0,
                        where: {
                            condition: "",
                        },
                        sort: [{
                            sortname: "默认",
                            sorted: 1,
                            sortid: 67,
                            reversed: 0
                        }]
                    },
                },
                query: "&radio=true",
                interrupt: true,
                placeholder: "选择客户",
                valueName: "sys_enterprise",
                checking: "base",
                required: false
            }, {
                label: "项目",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/project/select",
                value: "",
                params: {
                    id: 20221020143502,
                    content: {
                        nocache: true,
                        type: 0,
                        where: {
                            condition: "", //模糊搜索
                        },
                    },
                },
                query: "&radio=true",
                interrupt: true,
                placeholder: "选择项目",
                valueName: "sa_project",
                checking: "base",
                required: false
            }, {
                label: "说明",
                error: false,
                errMsg: "",
                type: "textarea",
                value: "",
                placeholder: "请填写",
                valueName: "remarks",
                required: false, //必填
            }]
        });
    },
    /* 打断处理form */
    interrupt(e) {
        console.log(e)
        let {
            data,
            form,
            temporary
        } = e.detail;
        if (temporary.item.value[0] == data.value[0]) return wx.navigateBack()
        temporary.item.value = data.value;
        if (temporary.item.label == '客户') {
            form[0] = temporary.item;
            form[1].label = '客户关联项目';
            form[1].interrupt = false;
            form[1].params = {
                id: 20221020143502,
                content: {
                    nocache: true,
                    type: 0,
                    where: {
                        condition: "", //模糊搜索
                        sys_enterpriseid: data.value[1][0],
                    },
                },
            }
            form[1].value = "";
            this.setData({
                form,
                continue1: "sa_project"
            })
        } else if (temporary.item.label == '项目') {
            form.splice(0, 1, temporary.item);
            form.splice(1, 1, {
                label: "项目关联客户",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/setclient/select",
                value: "",
                params: {
                    "id": 20221012164402,
                    content: {
                        nocache: true,
                        type: 0,
                        isExport: 0,
                        where: {
                            condition: "",
                            sa_projectid: data.value[1][0],
                        },
                        sort: [{
                            sortname: "默认",
                            sorted: 1,
                            sortid: 67,
                            reversed: 0
                        }]
                    },
                },
                query: "&radio=true",
                placeholder: "选择客户",
                valueName: "sys_enterprise",
                checking: "base",
                required: false
            });
            this.setData({
                form,
                continue1: "sys_enterprise"
            })
        };
        wx.navigateBack()
    },
    /* 获取定位 */
    getLocation() {
        let that = this;
        wx.showLoading({
            title: '定位中...',
        });
        wx.getLocation({
            type: 'gcj02',
            isHighAccuracy: true,
            highAccuracyExpireTime: 8000,
            success({
                latitude,
                longitude
            }) {
                wx.hideLoading();
                that.setData({
                    'markers[0]': {
                        id: 1,
                        latitude,
                        longitude,
                    }
                });
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude,
                        longitude
                    },
                    success(s) {
                        console.log("逆解析", s)
                        that.setData({
                            content: {
                                province: s.result.address_component.province,
                                city: s.result.address_component.city,
                                county: s.result.address_component.district,
                                address: s.result.address,
                                longitude,
                                latitude,
                                remarks: "",
                                sa_projectid: 0,
                                sys_enterpriseid: 0
                            }
                        })
                    },
                    fail(err) {
                        console.log(err)
                        wx.showToast({
                            title: '定位失败,请查看手机是否开启定位服务',
                            icon: "none"
                        })
                    }
                })
            },
            fail: function (e) {
                //这里是获取失败的时候
                wx.hideLoading();
                wx.getSetting({
                    success: res => {
                        if (typeof (res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
                            wx.showModal({
                                title: '提示',
                                content: '您拒绝了定位权限，将无法使用签到功能 是否前往开启',
                                success: res => {
                                    if (res.confirm) {
                                        wx.openSetting({
                                            success: res => {
                                                if (res.authSetting['scope.userLocation']) {
                                                    that.getLocation();
                                                } else {
                                                    // 没有允许定位权限
                                                    wx.showToast({
                                                        title: '没有允许定位权限',
                                                        icon: 'none'
                                                    });
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    }
                })
            },
        })
    },
    onUnload() {
        clearInterval(timing)
    },
    onReady() {

    },
})