const _Http = getApp().globalData.http,
    {
        formatTime
    } = require("../../../../utils/getTime");

import {
    tianditu
} from "../../../../utils/tianditu.js";
const api = new tianditu();
Page({
    data: {
        disabled: true,
        loading: false,
        form: [],
        list: [{
            label: "目的",
            key: "target",
            value: "",
        }, {
            label: "过程",
            key: "content",
            value: "",
        }, {
            label: "结果",
            key: "results",
            value: "",
        }, {
            label: "下次跟进计划",
            key: "nextplan",
            value: "",
        }],
        "content": {
            dataextend: {
                contactsid: []
            },
            sat_orderclueid: null,
            sat_ordercluefollowuplogid: 0,
            "logtype": "跟进",
            "competitor": "",
            resource: "",
            latitude: "", //经度
            longitude: "", //纬度
            address: "", //逆解析位置
        }
    },
    changeState({
        detail
    }) {
        this.setData({
            loading: detail
        })
    },
    async onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '跟进');
        if (options.resource) this.data.content.resource = options.resource;
        let date = formatTime(new Date(), '-').split(' '),
            time = date[1].split(":"),
            that = this;
        time.pop()
        let form = [{
            label: "跟进日期",
            error: false,
            errMsg: "",
            type: "date",
            value: date[0],
            placeholder: "跟进日期",
            valueName: "createdate",
            checking: "base",
            required: true
        }, {
            label: "跟进时间",
            error: false,
            errMsg: "",
            type: "time",
            value: time.join(":"),
            placeholder: "跟进时间",
            valueName: "time",
            checking: "base",
            required: true
        }, {
            label: "跟进对象",
            error: false,
            errMsg: "",
            params: {
                "id": "2025042110014402",
                "content": {
                    "nocache": true,
                    "sat_orderclueid": options.sat_orderclueid,
                    "pageNumbe": 1,
                    "pageTotal": 1,
                    "total": null,
                    "where": {
                        "conditino": ""
                    }
                }
            },
            type: "route",
            url: "/packageA/select/linkman/select",
            value: [],
            placeholder: "选择跟进对象",
            valueName: "contactsid",
            checking: "base",
            required: true
        }, {
            label: "跟进类型",
            error: false,
            errMsg: "",
            type: "option",
            optionNmae: "followupmode",
            optionType: "radio", //复选   radio 单选
            value: "",
            placeholder: "选择跟进方式",
            valueName: "followupmode",
            checking: "base",
            required: true
        }]
        if (options.sat_orderclueid) {
            this.data.content.sat_orderclueid = options.sat_orderclueid;
            wx.getLocation({
                type: 'wgs84',
                altitude: true,
                isHighAccuracy: true,
                highAccuracyExpireTime: 5000,
                success({
                    latitude,
                    longitude
                }) {
                    api.getPlace(longitude, latitude).then((place) => {
                        console.log("获取逆解析地址", place)
                        that.setData({
                            "content.address": place.result.formatted_address,
                            "content.latitude": latitude,
                            "content.longitude": longitude
                        })
                    })
                },
                fail(err) {
                    console.log(err)
                }
            })
        } else if (options.data) {
            let data = JSON.parse(options.data);
            date = data.createdate.split(' ');
            time = date[1].split(":");
            time.pop()
            form[0].value = date[0];
            form[1].value = time.join(":");
            form[2].value = [data.contactsName,data.dataextend.contactsid]
            console.log(" form[2].value", form[2].value)
            form[3].value = data.followupmode;
            this.setData({
                list: this.data.list.map(v => {
                    v.value = data[v.key]
                    return v
                })
            })
            this.data.content = data;
            if (data.attinfo.length) this.selectComponent("#Yl_files").handleFiles(data.attinfo)
        }

        this.selectComponent("#Form").confirm()
        this.setData({
            form
        })
    },
    onInput(e) {
        const {
            index
        } = e.target.dataset;
        this.data.list[index].value = e.detail.value;
    },
    onVoiceInput(e) {
        let {
            index
        } = e.target.dataset;
        this.setData({
            [`list[${index}].value`]: e.detail
        })
    },
    /* 绑定媒体 */
    insertImgEdit({
        detail
    }) {
        this.handleFileLink(detail)
    },
    handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                ownertable,
                ownerid,
                usetype: 'default',
                attachmentids
            }
        }).then(res => {
            console.log('跟进记录绑定附件', res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            if (ownertable == 'temporary') {
                this.selectComponent("#Yl_files").handleFiles(res.data)
            } else {
                _Http.basic({
                    "classname": "system.attachment.Attachment",
                    "method": "createFileLink",
                    "content": {
                        ownertable: "sat_orderclue",
                        ownerid: this.data.content.sat_orderclueid,
                        usetype: 'default',
                        attachmentids
                    }
                })
                if (res.data.length) data.attinfos = res.data;
                this.changeItem(data)
                setTimeout(() => {
                    wx.navigateBack()
                }, 500)
            }
        })
    },
    changeItem(data) {
        this.setData({
            loading: false
        })
        if (_Http.changeItem) _Http.changeItem(data)
    },
    submit() {
        this.setData({
            loading: true
        })
        let content = Object.assign(this.data.content, this.selectComponent("#Form").submit())
        content.createdate = content.createdate + ' ' + content.time + ':00';
        content.dataextend = {
            contactsid: content.contactsid[1]
        }
        delete(content.time)
        this.data.list.forEach(v => {
            content[v.key] = v.value
        })
        _Http.basic({
            "id": 20221208100602,
            content
        }).then(res => {
            console.log("保存跟进内容", res)
            wx.showToast({
                title: res.code != '1' ? res.msg : content.sat_ordercluefollowuplogid == 0 ? getApp().globalData.Language.getMapText('保存成功') : getApp().globalData.Language.getMapText('修改成功'),
                icon: "none",
                mask: res.code == '1'
            });
            if (res.code != '1') return;
            let attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
            if (attachmentids.length) return this.handleFileLink(attachmentids, 'sat_ordercluefollowuplog', res.data.sat_ordercluefollowuplogid, res.data);
            this.changeItem()
            setTimeout(() => {
                wx.navigateBack()
            }, 500)
        })
    },
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onUnload() {
        if (this.data.content.sat_ordercluefollowuplogid == 0) this.selectComponent("#Yl_files").deleteAll()
    }
})