// pages/append/append.js
const { getCategoryAPI, upLoadGoods } = require('../../API/appraise')
const { upLoad, chooseImage } = require('../../utils/util')
const baseURL = "http://192.168.31.27:8084";
import Toast from '@vant/weapp/toast/toast'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        latitude: 0,
        longtitude: 0,
        mapName: "",
        columns: [],
        id: [],
        popShow: false,
        clssify: [],
        valueKeyName: "",
        index: 0,
        fileImg: [],
        // 页面数据们
        title: "",
        desc: "",
        price: 0,
        address: "",
        fileList: [],
        gid: Number,
        maxgoodsURL: 6
    },
    popChange() {
        this.setData({
            popShow: true
        })
        this.getClassifyList()
    },
    onConfirm(e) {
        const { value, index} = e.detail;
        this.setData({
            popShow: false,
            valueKeyName: value,
            index: index
        })
    },
    onCancel() {
        this.setData({
            popShow: false,
            valueKeyName: ""
        })
    },
    titleInput(e) {
        const {value, cursor, keyCode} = e.detail;
        this.setData({
            title: value
        })
    },
    describeInput(e) {
        const {value, cursor, keyCode} = e.detail;
        this.setData({
            desc: value
        })
    },
    numInput(e){
        const {value, cursor, keyCode} = e.detail;
        this.setData({
            price: parseInt(value)
        })
    },
    adressInput(e) {
        const { value, cursor, keyCode} = e.detail;
        this.setData({
            address: value
        })
    },
    upData(){
        this.setData({
            gid: this.data.id[this.data.index]
        })
        upLoadGoods(this.data).then((res) => {
            console.log(res);
            Toast({
                message: '发布商品成功!'
            })
        }).catch((err) => {
            console.log(err); 
        })
        console.log(this.data);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    getClassifyList() {
		getCategoryAPI(this.data)
		.then((res) => {
            const { code, data, mesage } = res.data;
            const name = [];
            const id = [];
            for (let i = 0; i < data.length; i++) {
                name[i] = data[i].Name;
                id[i] = data[i].Id; 
            }
            console.log(id);
            this.setData({
                columns: name,
                id: id
            })
        })
    },  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    afterRead(e) {
        const { file } = e.detail;
        upLoad(file.url).then((res) => {
            const { fileList = [], fileImg = [] } = this.data;
            fileImg.push({ ...file, url: res.data });
            fileList.push(file.url)
            this.setData({
                fileList,
                fileImg
            })
            console.log(this.data.fileList);
        })
    }
})