// packageI/invoice/invoiceOperation/invoiceOperation.js
const app = getApp();
Page({
    data: {
        bearerId: '',
        bearerType: 0,
        checked:false,
        inputList: [],
        formData:{},
    },

    onLoad: function (options) {
        // 有传id为修改，不传为新增
        if (options.id !== undefined) {
            let id = options.id;
            this.setData({bearerId: id})
            this.getDetailData(id)
        }
    },

    onShow: function () {
        this.inputListFun(this.data.bearerType);
    },

    getDetailData (id) {
        let urlStr = app.getNetAddresss("plugin.invoice.frontend.rise.edit");
        app._getNetWork({
            url: urlStr,
            data: { id },
            success: ({data:res}) => {
                if (res.result != 1) {
                    return app.tips(res.msg);
                }
                this.setData({
                    formData: res.data,
                    bearerType: res.data.type
                })
                this.inputListFun(res.data.type);
            } 
        })
    },

    changeRoadio (e) {
        this.setData({
            bearerType: e.detail,
            formData: {},
        })
        this.inputListFun(e.detail);
    },

    // 输入框列表-工厂
    inputListFun(type){
        let comList = [
            {place:"请填写单位名称",label:"单位",model:"collect_name"},
            {place:"请填写纳税人识别号",label:"税号",model:"tax_num"},
            {type: 'divider', label: "分割线"},
            {place:"请填写单位开户银行（选填）",label:"开户银行",model:"bank"},
            {place:"请填写单位银行账号（选填）",label:"银行账号",model:"bank_admin"},
            {place:"请填写注册地址（选填）",label:"注册地址",model:"address"},
            {place:"请填写注册电话（选填）",label:"注册电话",model:"mobile"}
        ];
        let perList = [{place:"请填写需要开具发票的姓名",label:"发票抬头",model:"collect_name"}]
        let inputList = type == 1 ? comList : perList;
        this.setData({
            inputList
        })
    },

    chagenInput (e) {
        let {model} = e.currentTarget.dataset;
        let value = e.detail.replace(/\s+/g, '')
        this.setData({
            ['formData.'+model]: value
        });
    },

    changeSwitch (e) {
        this.setData({
            ['formData.is_default']: e.detail
        })
    },

    // 保存
    saveData () {
        if (app._isTextEmpty(this.data.formData.collect_name)) {
            if (this.data.bearerType == '0') {
                return app.tips("发票抬头不能为空！");
            } else {
                return app.tips("单位名称不能为空！");
            }
        }
        if (this.data.bearerType == 1 && app._isTextEmpty(this.data.formData.tax_num)) {
            return app.tips("纳税识别号不能为空！");
        }
        let urlStr = "";
        let requestData = {};
        let json = this.filterFormData();
        if (this.data.bearerId) { // 修改发票抬头
            urlStr = app.getNetAddresss("plugin.invoice.frontend.rise.edit");
            requestData.id = this.data.bearerId;
            requestData.data = json;
        } else { // 新增发票抬头
            urlStr = app.getNetAddresss("plugin.invoice.frontend.rise.store");
            requestData.data = json;
        }
        app._postNetWork({
            url: urlStr,
            data: requestData,
            success: ({data:res}) => {
                app.tips(res.msg);
                if (res.result == 1) {
                    wx.redirectTo({
                      url: '/packageI/invoice/invoiceCenter/invoiceCenter?type=-1',
                    })
                }
            }
        })
    },

    // 筛选表单数据
    filterFormData () {
        let json = {};
        let {collect_name, tax_num, bank, bank_admin, address, mobile, is_default } = this.data.formData;
        if (this.data.bearerType == 1) {
            json = {
                type: this.data.bearerType,
                collect_name, tax_num, bank, bank_admin, address, mobile,
                is_default
            }
        } else {
            json = {
                type: this.data.bearerType,
                collect_name,
                is_default
            }
        }
        return json;
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})