// pages/groupmeallist/addinvoices/addinvoices.js
import invoice from '../../../api/invoice'
const cache = require('../../../utils/cache.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mobile: '',
        invoiceId: '',
        invoiceTitle: '',
        invoiceNo: '',
        companyAddress: '',
        companyPhone: '',
        bankName: '',
        bankAccount: '',

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let type = options.type;
        let invoiceId = options.invoiceId;

        console.log(type +invoiceId)
        if (type == 'edit') {
            invoice.getinvoicebyid({
                mobile: cache.getMobile(),
                invoiceId:2
            }).then(res => {
                console.log(res.data)
                
                this.setData({
                    mobile:res.data.mobile,
                    invoiceId:res.data.id,
                    invoiceTitle:res.data.invoiceTitle,
                    invoiceNo:res.data.invoiceNo,
                    companyAddress:res.data.companyAddress,
                    companyPhone:res.data.companyPhone,
                    bankName:res.data.bankName,
                    bankAccount:res.data.bankAccount,
                })
            })

        }


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    invoiceTitle: function (e) {

        let invoiceTitle = e.detail.value;
        this.data.lock = false;
        this.setData({
            invoiceTitle,
        })

    },
    invoiceNo: function (e) {

        let invoiceNo = e.detail.value;
        this.data.lock = false;
        this.setData({
            invoiceNo,
        })

    },

    companyAddress: function (e) {

        let companyAddress = e.detail.value;
        this.data.lock = false;
        this.setData({
            companyAddress,
        })

    },

    companyPhone: function (e) {

        let companyPhone = e.detail.value;
        this.data.lock = false;
        this.setData({
            companyPhone,
        })

    },

    bankName: function (e) {

        let bankName = e.detail.value;
        this.data.lock = false;
        this.setData({
            bankName,
        })

    },


    bankAccount: function (e) {

        let bankAccount = e.detail.value;
        this.data.lock = false;
        this.setData({
            bankAccount,
        })

    },
//保存
    save(){
        invoice.addoreditinvoice({
            mobile:cache.getMobile(),
            invoiceId:this.data.invoiceId,
            invoiceTitle:this.data.invoiceTitle,
            invoiceNo:this.data.invoiceNo,
            companyAddress:this.data.companyAddress,
            companyPhone:this.data.companyPhone,
            bankName:this.data.bankName,
            bankAccount:this.data.bankAccount,

        }).then(res=>{

            wx.redirectTo({
              url: '../invoicesList/invoicesList',
            })

        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

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

    }
})