<!-- 新增修改地址 -->
<template>
    <div class="wrapper">
        <van-address-edit
            :area-list="areaList"
            show-set-default
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            :address-info="AddressInfo"
            @save="onSave"
            save-button-text="保存地址"
            :tel-validator="telValidator"
        />
    </div>
</template>
<script>
    import areaList from "@/assets/js/area/area.js"; //地区码
    export default {
        name: "add-modify-address",
        created() {
            if (this.$route.query.modifyAddress)
                (document.title = "修改收货地址"), (this.isModifyAddress = true);
            this.init();
        },
        data() {
            return {
                areaList,
                AddressInfo: {
                    name: "",
                    tel: "",
                    isDefault: false,
                    areaCode: "",
                    addressDetail: ""
                },
                ModifyAddressId: null, //修改地址 id
                isModifyAddress: false //是否修改地址
            };
        },
        methods: {
            init() {
                if (this.isModifyAddress) {
                    // 获取需要修改的地址信息
                    let res = JSON.parse(sessionStorage.getItem("modifyAddress"));
                    if (res) {
                        // 获取地址
                        this.AddressInfo = {
                            name: res.Name,
                            tel: res.Phone,
                            isDefault: res.IsDefault,
                            areaCode: res.AreaCode,
                            addressDetail: res.Addr
                        };
                        // 地址ID
                        this.ModifyAddressId = res.ID;
                    } else {
                        this.$router.replace({ name: "myAddress" });
                    }
                }
            },
            onSave(content) {
                console.log("content: ", content);
                return;
                let obj = {
                    Name: content.name,
                    Phone: content.tel,
                    AreaCode: content.areaCode,
                    Addr: content.addressDetail,
                    IsDefault: content.isDefault
                };
                this.LJ_LOADING("保存中...");
                if (this.isModifyAddress) {
                    obj.ID = this.ModifyAddressId;
                    // 修改地址
                    this.$http.UpdateAddress(obj).then(res => {
                        if (res.data.IsSuccess) {
                            this.$toast("修改成功");
                            this.$router.replace({ name: "myAddress" });
                        } else {
                            this.$toast(res.data.ResultMsg);
                        }
                    });
                } else {
                    // 保存地址
                    this.$http.AddAddress(obj).then(res => {
                        if (res.data.IsSuccess) {
                            this.$toast("保存成功");
                            this.$router.replace({ name: "myAddress" });
                        } else {
                            this.$toast(res.data.ResultMsg);
                        }
                    });
                }
            },
            // 手机号校验函数
            telValidator(tel) {
                return /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(
                    tel
                );
            }
        },
        beforeDestroy() {
            sessionStorage.removeItem("modifyAddress");
        }
    };
</script>
<style lang='less' scoped>
    .wrapper {
        min-height: 100vh;
        background: #f5f5f5;
        padding-top: 0.2rem;
        /deep/ .van-cell {
            text-align: left;
            font-size: 0.28rem;
            font-weight: 600;
        }
        /deep/ .van-button--danger {
            background: #00a4ff;
            border-color: #00a4ff;
        }
    }
</style>