<template>
    <view>
        <view class="inputField">
            <view class="title">抬头类型</view>
            <input
                type="text"
                disabled
                :placeholder="is_company == 1 ? '企业单位' : '个人/非企业单位'"
                :value="is_company == 1 ? '企业单位' : '个人/非企业单位'"
                @input="titleInput"
            />
        </view>
        <view class="line">
            <view class="inputField">
                <view class="title">抬头名称</view>
                <input type="text" :placeholder="invoice_title" :value="invoice_title" @input="titleInput" />
            </view>
            <view class="inputField" v-if="is_company">
                <view class="title">纳税人识别号</view>
                <input type="text" :placeholder="nsrsbh" :value="nsrsbh" @input="nsrsbhInput" />
            </view>
            <view class="inputField" style="border: none">
                <view class="title">发票接收邮箱</view>
                <input type="text" :placeholder="email" :value="email" @input="emailInput" />
            </view>
        </view>

        <view class="_conf">
            <button class="_button" :disabled="!_edit" @tap="_conf">修改信息</button>
            <view class="_b" @tap="_delete">删除信息</view>
        </view>
    </view>
</template>

<script>
// pages/user/invoice/invoice_edit/invoice_eidt_content.js
import { http } from '../../../../utils/util';
export default {
    data() {
        return {
            edit: false,
            is_company: '',
            invoice_title: '',
            nsrsbh: '',
            email: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.current_item) {
            let info = JSON.parse(options.current_item);
            this.setData({
                is_company: info.is_company,
                invoice_title: info.invoice_title,
                nsrsbh: info.nsrsbh,
                email: info.email
            });
        } else {
            uni.navigateTo({
                url: '/pages/user/invoice/invoice_edit/invoice_edit'
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        titleInput(event) {
            const value = event.detail.value;
            this.setData({
                invoice_title: value,
                _edit: true
            });
        },

        nsrsbhInput(event) {
            const value = event.detail.value;
            this.setData({
                nsrsbh: value,
                _edit: true
            });
        },

        emailInput(event) {
            const value = event.detail.value;
            this.setData({
                email: value,
                _edit: true
            });
        },

        _conf: function () {
            let data1 = {};

            if (this.is_company == 1 && this.nsrsbh && this.invoice_title && this.email) {
                data1 = {
                    is_company: 1,
                    invoice_title: this.invoice_title,
                    nsrsbh: this.nsrsbh,
                    email: this.email
                };
                http('/api/v1/invoice-save-title', 'post', data1).then((res) => {
                    uni.navigateBack();
                    var pages = getCurrentPages(); //获取页面栈

                    if (pages.length > 1) {
                        //上一个页面实例对象
                        var prePage = pages[pages.length - 2]; //调用上一个页面的onShow方法

                        prePage.onLoad({});
                    }
                });
            } else {
                if (this.is_company == 0 && this.invoice_title && this.email) {
                    data1 = {
                        is_company: 0,
                        invoice_title: this.invoice_title,
                        email: this.email
                    };
                    http('/api/v1/invoice-save-title', 'post', data1).then((res) => {
                        uni.navigateBack();
                        var pages = getCurrentPages(); //获取页面栈

                        if (pages.length > 1) {
                            //上一个页面实例对象
                            var prePage = pages[pages.length - 2]; //调用上一个页面的onShow方法

                            prePage.onLoad({});
                        }
                    });
                } else {
                    uni.showToast({
                        title: '请输入完整信息！',
                        icon: 'fail',
                        duration: 2000
                    });
                }
            }
        },

        _delete: function () {
            uni.showModal({
                content: '是否删除这条发票信息',

                success(res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        http('/api/v1/invoice-title-delete', 'post').then(() => {
                            uni.navigateBack();
                            var pages = getCurrentPages(); //获取页面栈

                            if (pages.length > 1) {
                                //上一个页面实例对象
                                var prePage = pages[pages.length - 2]; //调用上一个页面的onShow方法

                                prePage.onLoad({});
                            }
                        });
                    } else {
                        if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                }
            });
        }
    }
};
</script>
<style>
.inputField {
    display: flex;
    align-items: center;
    width: 100%;
    font-size: 28rpx;
    color: #333333;
    border-bottom: 1rpx solid #eeeeee;
    padding: 30rpx;
}
.inputField .title {
    width: 200rpx;
}
.inputField input {
    width: 100%;
    flex: 1;
}

._conf {
    position: fixed;
    bottom: 15%;
    text-align: center;
    width: 40%;
    left: 30%;
}
._button {
    width: 312rpx;
    background: linear-gradient(90deg, #ff4284 0%, #ff1d42 100%);
    border-radius: 44rpx;
    color: #fff;
    font-size: 28rpx;
}
._b {
    font-size: small;
    color: red;
    padding-top: 20rpx;
}
</style>
