const _Http = getApp().globalData.http;
Page({
    data: {
        /* {
            label: "联系人",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "请填写",
            valueName: "name",
            required: true,
            checking: "base"
        }, {
            label: "手机号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "收货人手机号码",
            valueName: "phonenumber",
            required: true,
            checking: "phone"
        },  */
        form: [{
            label: "省市县",
            error: false,
            errMsg: "",
            type: "region",
            value: [],
            placeholder: "省,市,县",
            valueName: "region",
            required: false
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "例: 科创园11栋1103室",
            valueName: "address",
            required: false,
            checking: "base"
        }],
        disabled: true,
        "content": {
            "contactsid": 0, //地址id
            "sys_enterpriseid": 0, //绑定数据
            "sex": "",
            "depname": "",
            "position": "",
            "isleader": 0,
            "birthday": "",
            "email": "",
            "remarks": "",
            "workaddress": 1,
            "isdefault": 0, //是否默认地址
            "isprimary": 0 //是否为主地址
        },
        tags: []
    },
    onLoad(options) {
        if (options.sys_enterpriseid) this.setData({
            "content.sys_enterpriseid": options.sys_enterpriseid
        })
        if (options.data) {
            let item = JSON.parse(options.data)
            this.setData({
                content: {
                    ...this.data.content,
                    ...item
                },
                "content.isdefault": item.isdefault,
                'form[0].value': [item.province, item.city, item.county],
                'form[1].value': item.address,
                tags: item.tag,
                'content.isdefault': item.isdefault,
                "disabled": false
            })
        };
        _Http.basic({
            "id": 20220929085401,
            "content": {
                "ownertable": "sys_enterprise_contacts",
                "ownerid": 0
            }
        }).then(res => {
            console.log("数据标签", res)
            this.setData({
                tagList: res.data.option
            })
        })
    },
    selectTag(e) {
        const {
            tag
        } = e.currentTarget.dataset.item;
        let i = this.data.tags.findIndex(v => tag == v);
        if (i == -1) {
            this.data.tags.push(tag);
        } else {
            this.data.tags.splice(i, 1)
        };
        this.setData({
            tags: this.data.tags
        })

    },
    /* 提交数据 */
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (!data || this.data.disabled) return;
        const content = {
            ...this.data.content,
            ...data,
            "province": data.region[0],
            "city": data.region[1],
            "county": data.region[2],
        };
        delete(content.region);
        _Http.basic({
            "id": "20221018141802",
            content
        }).then(res => {
            console.log('新建地址', res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            _Http.basic({
                "id": 20220929090901,
                "content": {
                    "ownertable": "sys_enterprise_contacts",
                    "ownerid": res.data.contactsid,
                    "datatag": this.data.tags
                }
            }).then(s => {
                console.log("设置地址标签", s)
                wx.showToast({
                    title: '保存成功',
                    icon: "none"
                });
                res.data.tag = this.data.tags;
                setTimeout(() => {
                    getCurrentPages().forEach(v => {
                        //更新列表
                        if (v.selectComponent("#Address")) {
                            let page = v.selectComponent("#Address"),
                                list = res.data.isdefault == 0 ? page.data.list : page.data.list.map(v => {
                                    v.isdefault = 0;
                                    return v;
                                }),
                                index = list.findIndex(value => value.contactsid == res.data.contactsid);
                            if (index != -1) {
                                //列表中存在说明是编辑，返回上一级页面并更新数据
                                list[index] = res.data;
                                page.setData({
                                    list
                                });
                            } else {
                                //列表中不存在说明是新增，返回上一级页面更新数据 并进入详情
                                list.push(res.data);
                                page.setData({
                                    list,
                                    "content.total": page.data.content.total + 1
                                });
                            }
                            if (res.data.isdefault == 1) setTimeout(() => {
                                page.selectComponent("#List").initRadio();
                            }, 200)
                            wx.navigateBack();
                        };
                        if ("packageA/setclient/modules/address/search/index" == v.__route__) v.getList(true);
                    })
                }, 300)
            })



        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})