<template>
    <div v-loading="loading" element-loading-text="拼命加载中......请稍等">
        <el-form
            ref="expressAddressForm"
            :model="form"
            :rules="rules"
            label-width="170px"
            style="max-width: 800px"
        >
            <el-form-item v-if="!existAddress">
                <div style="background-color: #e6a23c; padding: 10px 10px;text-align: center; ">
                    <span style="color: white;"
                        >不存在邮寄信息，请添加发票邮寄信息</span
                    >
                </div>
            </el-form-item>
            <el-form-item label="企业名称">
                <span v-text="entName"></span>
            </el-form-item>
            <el-form-item label="企业税号">
                <span v-text="entTaxId"></span>
            </el-form-item>
            <el-form-item label="票据接收方式：" prop="getWay">
                <el-radio-group v-model="form.getWay">
                    <el-radio
                        v-for="item in invoiceGetWayOptions"
                        :key="item.value"
                        :label="item.value"
                        :value="item.value"
                        >{{ item.label }}</el-radio
                    >
                </el-radio-group>
            </el-form-item>
            <el-form-item label="收件人：" prop="receiverName" v-if="isGetway2">
                <el-input v-model="form.receiverName"></el-input>
            </el-form-item>
            <el-form-item
                label="移动电话："
                prop="mobilePhoneNumber"
                v-if="isGetway2"
            >
                <el-input v-model="form.mobilePhoneNumber"></el-input>
            </el-form-item>
            <el-form-item
                label="固定电话："
                prop="fixedPhoneNumber"
                v-if="isGetway2"
            >
                <el-input v-model="form.fixedPhoneNumber"></el-input>
            </el-form-item>
            <el-form-item
                label="快递地址："
                prop="receiverAddress"
                v-if="isGetway2"
            >
                <el-input v-model="form.receiverAddress"></el-input>
            </el-form-item>
            <el-form-item label="邮政编码：" prop="zipCode" v-if="isGetway2">
                <el-input v-model="form.zipCode"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-finished" @click="handleSaveOrUpdate"
                    >保存</el-button
                >
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import SEND from "@/api/send";
import cache from '@/utils/cache'

// 发票申领领购方式
const invoiceGetWayOptions = [
    {
        value: 1,
        label: "自行领取",
    },
    {
        value: 2,
        label: "快递配送",
    },
];

export default {
    name: "ExpressAddress",
    data() {
        return {
            loading: false,
            existAddress: false, // 是否存在地址
            entName: "",
            entTaxId: "",
            invoiceGetWayOptions: invoiceGetWayOptions,
            sendId: "", // 修改地址信息时需要
            form: {
                getWay: 2, // 领取方式：1=自行领取，2=邮件快递
                receiverName: "", // 收件人姓名
                mobilePhoneNumber: "", // 移动电话
                fixedPhoneNumber: "", // 固定电话
                receiverAddress: "", // 快递地址
                zipCode: "", // 邮编
            },
            rules: {
                getWay: [
                    {
                        required: true,
                        message: "请选择领票方式",
                        trigger: "change",
                    },
                ],
                receiverName: [
                    {
                        required: true,
                        message: "请输入收件人",
                        trigger: "change",
                    },
                ],
                mobilePhoneNumber: [
                    {
                        required: true,
                        message: "请输入移动电话",
                        trigger: "change",
                    },
                ],
                fixedPhoneNumber: [
                    {
                        required: true,
                        message: "请输入固定电话",
                        trigger: "change",
                    },
                    {
                        min: 1,
                        max: 15,
                        message: "固定电话长度最多为15个字符",
                        trigger: "change",
                    },
                ],
                receiverAddress: [
                    {
                        required: true,
                        message: "请输入快递地址",
                        trigger: "change",
                    },
                ],
                zipCode: [
                    {
                        required: true,
                        message: "请输入邮政编码",
                        trigger: "change",
                    },
                ],
            },
        };
    },
    created() {
        let company = JSON.parse(cache.getSession("company"));
        this.entTaxId = company.entTaxId;
        this.entName = company.entName;
        let _this = this;
        SEND.getSendInfo()
            .then((val) => {
                _this.loading = false;
                _this.existAddress = true;
                _this.sendId = val.data.sendId;
                _this.form.getWay = val.data.getWay;
                _this.form.receiverName = val.data.receiverName;
                _this.form.receiverAddress = val.data.receiverAddress;
                _this.form.mobilePhoneNumber = val.data.mobilePhoneNumber;
                _this.form.zipCode = val.data.zipCode;
                _this.form.fixedPhoneNumber = val.data.fixedPhoneNumber;
            })
            .catch((error) => {
                if (error.code == 10166) {
                    _this.existAddress = false;
                } else {
                    _this.existAddress = true;
                    _this.$message.error(error.msg);
                }
                _this.loading = false;
            });
    },
    computed: {
        // 是否是快递配送
        isGetway2() {
            return this.form.getWay == 2;
        },
    },
    methods: {
        handleSaveOrUpdate() {
            let _this = this;
            _this.$refs.expressAddressForm.validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    SEND.saveOrUpdate({
                        sendId: _this.sendId,
                        getWay: _this.form.getWay,
                        receiverName: _this.form.receiverName,
                        receiverAddress: _this.form.receiverAddress,
                        mobilePhoneNumber: _this.form.mobilePhoneNumber,
                        zipCode: _this.form.zipCode,
                        fixedPhoneNumber: _this.form.fixedPhoneNumber,
                    })
                        .then((val) => {
                            _this.loading = false;
                            location.reload();
                        })
                        .catch((error) => {
                            _this.loading = false;
                            this.$message.error(error);
                        });
                } else {
                    _this.$message.error(
                        "输入信息不合法，请检查您的输入信息！"
                    );
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="stylus" scoped></style>