<template>
    <div class="page" :class="{ 'page--iphoneX': iphoneX }">
        <div class="form-card">
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    店铺名称：
                </div>
                <div class="box">
                    <input
                        type="text"
                        v-model="form.appName"
                        placeholder="请填写店铺名称"
                    />
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    联系方式：
                </div>
                <div class="box">
                    <input
                        type="text"
                        v-model="form.mobile"
                        placeholder="请填写联系方式"
                    />
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    姓名：
                </div>
                <div class="box">
                    <input
                        type="text"
                        v-model="form.name"
                        placeholder="请填写姓名"
                    />
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    提货点地址：
                </div>
                <div class="box" @click="mapClick">
                    <input
                        type="text"
                        v-model="form.pickUpAddress"
                        :disabled="true"
                        placeholder="请选择地址"
                    />
                </div>
                <div class="tool" @click="mapClick">
                    <my-image
                        class="address-icon"
                        src="/static/common/address.png"
                    ></my-image>
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    详细地址：
                </div>
                <div class="box">
                    <input
                        type="text"
                        v-model="form.address"
                        placeholder="请填写详细地址"
                    />
                </div>
            </section>
            <!-- <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    身份证照 （正反面）：
                </div>
                <div class="box">
                    <easy-upload
                        :types="'image'"
                        v-model="mainImage"
                        :uploadCount="2"
                        @change="minImgChange"
                    ></easy-upload>
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    店铺营业执照：
                </div>
                <div class="box">
                    <easy-upload
                        :types="'image'"
                        v-model="mainImage2"
                        :uploadCount="1"
                        @change="minImgChange2"
                    ></easy-upload>
                </div>
            </section>
            <section class="form-item">
                <div class="label">食品许可证：</div>
                <div class="box">
                    <easy-upload
                        :types="'image'"
                        v-model="mainImage3"
                        :uploadCount="1"
                        @change="minImgChange3"
                    ></easy-upload>
                </div>
            </section> -->
            <!-- <section class="form-item">
                <div class="label">
                    推荐人：
                </div>
                <div class="box">
                    <input type="text" v-model="form.recommendName" />
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    邀请码：
                </div>
                <div class="box">
                    <input type="text" v-model="form.invitedCode" />
                </div>
            </section> -->
        </div>
        <div class="btns">
            <div class="btn" @click="saveOk">保存</div>
        </div>
    </div>
</template>

<script>
import MyImage from "../../components/image/index";
import DateTimePicker from "../../components/biaofun-datetime-picker/biaofun-datetime-picker";
import easyUpload from "../../components/easy-upload/easy-upload";

export default {
    name: "",
    components: { MyImage, DateTimePicker, easyUpload },
    // 数据
    data() {
        return {
            form: {
                appName: "",
                mobile: "",
                name: "",
                pickUpAddress: "",
                address: "",
                foodLicense: "",
                businessLicense: "",
                iDCard: "",
                recommendName: "",
                invitedCode: "",
                latitude: "",
                longitude: "",
                province: "",
                city: "",
                county: "",
            },
            mainImage: [],
            mainImage2: [],
            mainImage3: [],
            submitLoading: false,
        };
    },

    onLoad(opts) {
        this.getGroupInfo();
    },

    onShow() {},

    // 函数
    methods: {
        getGroupInfo() {
            this.api
                .get("/Group/getGroupInfo", {}, { pass: true })
                .then((res) => {
                    if (res.success && res.data) {
                        this.form.appName = res.data.shopName || "";
                        this.form.province = res.data.province || "";
                        this.form.city = res.data.city || "";
                        this.form.county = res.data.district || "";
                        this.form.address = res.data.detialAddr || "";
                        this.form.latitude = res.data.latitude || "";
                        this.form.longitude = res.data.longitude || "";
                        if (this.form.province) {
                            this.form.pickUpAddress = `${this.form.province}${this.form.city}${this.form.county}`;
                        }
                    }
                });
        },

        mapClick() {
            let self = this;
            // 判断定位权限
            uni.getSetting({
                success: function (res) {
                    if (res.authSetting["scope.userLocation"] === false) {
                        self.fn
                            .showModal({
                                content: "请授权位置信息后使用",
                                showCancel: false,
                            })
                            .then((res) => {
                                uni.openSetting({});
                            });
                    } else {
                        self.userLocation = true;
                        self.chooseLocation();
                    }
                },
            });
        },

        chooseLocation() {
            let self = this;
            uni.chooseLocation({
                type: "gcj02", //返回可以用于uni.openLocation的经纬度
                success: function (res) {
                    self.form.pickUpAddress = res.address;
                    self.form.province = "";
                    self.form.city = "";
                    self.form.county = "";
                    self.form.latitude = res.latitude.toFixed(6) || "";
                    self.form.longitude = res.longitude.toFixed(6) || "";
                    console.log("定位ok", res);
                },
                fail(e) {
                    console.log("定位no", e);
                },
            });
        },

        minImgChange(e) {
            this.form.iDCard = e;
        },
        minImgChange2(e) {
            this.form.businessLicense = e[0] || "";
        },
        minImgChange3(e) {
            this.form.foodLicense = e[0] || "";
        },
        saveOk() {
            if (this.submitLoading) {
                return;
            }
            if (!this.form.appName) {
                return this.fn.showToast("请输入店铺名称");
            }
            if (!this.form.mobile) {
                return this.fn.showToast("请输入联系方式");
            }
            if (!this.form.name) {
                return this.fn.showToast("请输入姓名");
            }
            if (!this.form.pickUpAddress) {
                return this.fn.showToast("请选择提货地址");
            }
            if (!this.form.address) {
                return this.fn.showToast("请输入详细地址");
            }
            // if (!this.form.iDCard) {
            //     return this.fn.showToast("请上传身份证照");
            // }
            // if (this.form.iDCard.length !== 2) {
            //     return this.fn.showToast("请分别上传身份证照正反面");
            // }
            if (this.user.parentInviteCode) {
                this.form.invitedCode = this.user.parentInviteCode;
            }
            this.submitLoading = true;
            uni.showLoading({
                title: "提交中...",
            });
            let data = { ...this.form };
            this.api
                .post("/Shop/SubmitApply", data, { pass: true })
                .then((res) => {
                    this.submitLoading = false;
                    uni.hideLoading();
                    if (res.success) {
                        this.fn.showToast("保存成功");
                        this.fn.applyShopsubscribe(res.data);
                        this.router.back();
                    } else {
                        this.fn.showModal({
                            content:
                                "错误码:" +
                                res.code +
                                ",错误信息:" +
                                res.message,
                            showCancel: false,
                        });
                    }
                });
        },
    },

    // 数据计算
    computed: {
        user() {
            return this.$store.state.user.user;
        },
    },

    // 数据监听
    watch: {},
};
</script>


<style lang="scss" scoped>
.page {
    padding-bottom: px(120);
    min-height: 100vh;
    background-color: #fff;
}
.page--iphoneX {
    padding-bottom: px(150);
    .btn {
        padding-bottom: px(30);
        background-color: rgb(0, 188, 38);
    }
}
.form-card {
    background-color: #fff;
    padding: 0 px(30);
    & ~ .form-card {
        margin-top: px(25);
    }
    .head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: px(140);
        background-color: #fbfbfb;
        border-bottom: 1px solid #f1f1f1;
        border-top: 1px solid #f1f1f1;
        font-size: px(44);
    }
}
.form-item {
    display: flex;
    min-height: px(140);
    padding: px(30) 0;
    box-sizing: border-box;
    align-items: center;
    font-size: px(44);
    border-top: 1px solid #f1f1f1;
    &.dis-item {
        color: #999;
        background: #f9f9f9;
    }
    .label {
        width: px(280);
        flex-shrink: 0;
        color: #999;
        margin-right: px(10);
    }
    .box {
        width: 100%;
        position: relative;
        input {
            width: 100%;
        }
    }
    .tool {
        flex-shrink: 0;
        color: #999;
        margin-left: px(10);
    }
    textarea,
    input {
        max-width: 100%;
        width: 100%;
    }
}
.list {
    // min-height: 100vh;
    // background-color: #fff;
    // border-bottom: 1px solid #f1f1f1;
    li {
        padding: px(0) px(35);
        height: px(140);
        line-height: px(140);
        font-size: px(44);
        border-top: 1px solid #f1f1f1;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .tools {
        display: flex;
        align-items: center;
        flex-shrink: 0;
        span {
            display: inline-block;
            border: 1px solid #f1f1f1;
            font-size: px(36);
            height: px(80);
            width: px(120);
            display: flex;
            justify-content: center;
            align-items: center;
            & ~ span {
                margin-left: px(30);
            }
        }
        .remove {
            color: #f00;
        }
    }
}
.btn {
    width: 100%;
    height: px(120);
    background-color: rgb(0, 188, 38);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.btns {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
}
.layout {
    background-color: #eee;
    color: #666;
}
.add-btn {
    height: px(120);
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: px(50) px(35) 0;
    border: 1px solid #ccc;
    border-radius: px(10);
}
.code-tool {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .scan-img {
        flex-shrink: 0;
        width: px(80);
        height: px(80);
        /deep/ img {
            width: px(80);
            height: px(80);
        }
    }
    .generate {
        color: #0097d1;
        margin-right: px(30);
        flex-shrink: 0;
    }
}
.required {
    color: #f00;
    font-size: px(44);
    &.hide {
        visibility: hidden;
    }
}
.inp-dis {
    color: #999;
}

.img-card {
    padding-top: px(30);
    padding-bottom: px(30);
}
.form-img-item {
    .tit {
        padding: px(30);
        font-size: px(44);
    }
}

.spec-list {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    li {
        width: 33.333333%;
        font-size: px(40);
        height: px(100);
        border: 1px solid #f1f1f1;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #999;
        & ~ li {
            // border-left: none;
        }
        &.on {
            color: rgb(0, 188, 38);
            border-color: rgb(0, 188, 38);
        }
    }
}
.mo-date {
    margin-top: px(20);
    border-bottom: 1px solid #f1f1f1;
    background: #fbfbfb;
    font-size: px(40);
    padding: px(10);
}
.n-date-box {
    margin-top: px(20);
    font-size: px(40);
}
.n-date {
    border-bottom: 1px solid #f1f1f1;
    background: #fbfbfb;
    padding: 0 px(20);
    display: inline-block;
    width: px(200);
    font-size: px(44);
    vertical-align: bottom;
}
.address-icon {
    /deep/ img {
        width: px(60);
        height: px(60);
    }
}
</style>