const deleteMark = require("../../utils/deleteMark"),
    getTime = require("../../utils/getTime"),
    _Http = getApp().globalData.http,
    QQMapWX = require('../../utils/qqmap-wx-jssdk.min'),
    qqmapsdk = new QQMapWX({
        key: 'QF6BZ-4IQC5-SN2IH-QL5BW-U4IJO-R3BY4' // 必填
    });
Page({
    data: {
        sat_orderclueid: null,
        deleteItem: false,
        actionShow: false,
        date1: getTime.formatTime(new Date(), '-').split(' ')[0],
        time: getTime.formatTime(new Date(), '-').split(' ')[1],
        actions: [],
        disabled: true,
        loading: false,
    },
    onLoad(options) {
        if (options.sat_orderclueid) this.setData({
            sat_orderclueid: options.sat_orderclueid
        })
        this.getFollowType()
        this.init();
    },
    bindDateChange: function (e) {
        this.setData({
            date1: e.detail.value
        })
    },
    bindTimeChange(e) {
        this.setData({
            time: e.detail.value+":00"
        })
    },
    /* 跟进初始化 */
    init() {
        let that = this;
        _Http.basic({
            id: "20221208100602",
            "content": {
                sat_orderclueid: this.data.sat_orderclueid,
                sat_ordercluefollowuplogid: 0,
                "content": "",
                "followupmode": "",
                "logtype": "",
                "competitor": "",
                latitude: "", //经度
                longitude: "", //纬度
                address: "", //逆解析位置
            }
        }).then(res => {
            console.log("初始化", res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: '初始化失败!请稍后再试',
                    icon: "none"
                });
                setTimeout(() => {
                    wx.navigateBack()
                }, 300)
                return
            } else {
                that.setData({
                    content: res.data,
                    deleteItem: true,
                });
                wx.getLocation({
                    type: 'wgs84',
                    altitude: true,
                    isHighAccuracy: true,
                    highAccuracyExpireTime: 5000,
                    success({
                        latitude,
                        longitude
                    }) {
                        that.setData({
                            "content.latitude": latitude,
                            "content.longitude": longitude
                        })
                        qqmapsdk.reverseGeocoder({
                            location: {
                                latitude,
                                longitude
                            },
                            success(s) {
                                console.log("逆解析", s)
                                that.setData({
                                    "content.address": s.result.address,
                                })
                            }
                        })

                    },
                    fail(err) {
                        console.log(err)
                    }
                })
            }
        })
    },
    submit() {
        if (this.data.loading) return;
        let content = this.data.content;
        content.createdate = this.data.date1 + " " + this.data.time
        this.setData({
            loading: true
        })
        content.logtype = '跟进';
        _Http.basic({
            id: "20221208100602",
            content
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                deleteItem: false
            })
            wx.showToast({
                title: '保存成功!',
            })
            setTimeout(() => {
                let page = getCurrentPages().find(v => v.__route__ == 'packageA/saleClue/detail');
                page.selectComponent("#follow").getList(page.data.sat_orderclueid, true);
                page.getDetail();
                wx.navigateBack();
            }, 300)
        })
    },
    /* 绑定媒体 */
    insertImgEdit({
        detail
    }) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                "ownertable": "sat_ordercluefollowuplog",
                "ownerid": this.data.content.sat_ordercluefollowuplogid,
                "usetype": "default",
                "attachmentids": detail
            }
        }).then(res => {
            console.log('跟进记录绑定附件', res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.selectComponent("#Yl_Files").handleFiles(res.data)
        })
    },
    /* 获取跟进方式 */
    async getFollowType() {
        let res = await _Http.basic({
            "classname": "sysmanage.develop.optiontype.optiontype",
            "method": "optiontypeselect",
            "content": {
                "typename": 'followupmode'
            }
        })
        let Arr = res.data.map(item => {
            return {
                name: item.value
            }
        })
        this.setData({
            actions: Arr
        })
    },
    /* 打开选择跟进方式 */
    selectWay() {
        this.setData({
            actionShow: !this.data.actionShow
        })
    },
    /* 选择方式 */
    onSelect(e) {
        this.setData({
            "content.followupmode": e.detail.name
        })
        this.isDisabled();
    },
    /* 选择结果 */
    changeType(e) {
        if (e.target.dataset.name) this.setData({
            'content.logtype': e.target.dataset.name
        })
        this.isDisabled();
    },
    /* 输入框输入内容 */
    inputChange(e) {
        let text = e.type == 'input' ? e.detail.value : e.detail;
        this.setData({
            "content.content": deleteMark.queryStr(text)
        })
        this.isDisabled();
    },
    isDisabled() {
        let data = this.data.content;
        this.setData({
            disabled: (data.followupmode != '' && data.content != '') ? false : true
        })
    },
    onUnload() {
        if (this.data.deleteItem) _Http.basic({
            "id": 20221208112002,
            "content": {
                "sat_ordercluefollowuplogid": this.data.content.sat_ordercluefollowuplogid
            }
        }).then(res => {
            console.log("删除未提交", res)
        })

    }
})