import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
import {
    TestVerify
} from "../../utils/verify";
const _Verify = new TestVerify();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        fprodname: "", //产品名称
        fprodnum: "", //产品编码
        attinfos: [], //附件列表
        categoryListings: [], //类别列表
        fintroduction: "", //产品说明
        //ftag: [],产品标签
        showSaleprod: "", //标签显示
        tagents_productid: 0, //识别产品ID
        checked: true, //开关控件
        fisonsale: 1, //是否上架
        throttle: false, //保存节流阀
        errTips: {
            fprodname: false,
            fprodnum: false,
            showSaleprod: false,
            fprodclassname: false
        },
        fprodclassname: "", //产品分类
        pupShow: false, //弹出层控制
    },
    selection() {
        this.setData({
            pupShow: !this.data.pupShow
        })
    },
    /* input事件剔除特殊字符 */
    eliminate(value) {
        const {
            name
        } = value.target.dataset;
        this.setData({
            [name]: _Verify.Eliminate(value.detail)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.data)
        if (options.data) {
            const data = JSON.parse(options.data);
            console.log(data)
            let attinfos = [];
            for (let i = 0; i < data.attinfos.length; i++) {
                let arr = {
                    url: data.attinfos[i].fobsurl,
                    ownerid: data.attinfos[i].ownerid,
                    tattachmentid: data.attinfos[i].tattachmentid,
                    ownertable: data.attinfos[i].ownertable,
                    fdocument: data.attinfos[i].fdocument,
                    fprodclassname: data.attinfos[i].fprodclassname
                }
                attinfos.push(arr)
            };
            /* 是否上架 */
            const checked = (data.fisonsale == 1) ? true : false
            let showSaleprod = "";

            /* 空标签转换为数组格式 
            ftag = data.ftag
            if (data.ftag == null || data.ftag == '') {
                ftag = []
            } else {
                //字符串数组转换数组
                for (var i = 0; i < ftag.length; i++) {
                    showSaleprod += ftag[i] + ' ';
                }
            }*/
            /* 渲染数据 */
            this.setData({
                fprodname: data.fprodname, //产品名称
                fprodnum: data.fprodnum, //产品编码
                attinfos, //附件列表
                fintroduction: data.fintroduction, //产品说明
                fisonsale: data.fisonsale, //是否上架
                //ftag: ftag, 产品标签
                tagents_productid: data.tagents_productid, //识别产品ID
                showSaleprod, // 显示标签
                checked, //是否上架
                fprodclassname: data.fprodclassname
            });
        };
    },
    /* 回调 */
    optionChangeCallBack(e) {
        this.setData({
            fprodclassname: e.detail,
            pupShow: false,
            "errTips.fprodclassname": false
        })
    },

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

    },
    /* 添加图片 */
    imageChange(data) {
        this.setData({
            attinfos: data.detail.fileList
        })
    },
    /* 上架开关 */
    switchChange() {
        const that = this;
        wx.showModal({
            title: "提示",
            content: (this.data.checked) ? '是否确认下架该产品' : '是否确认上架该产品',
            success(res) {
                if (res.confirm) {
                    const checked = !that.data.checked,
                        fisonsale = (checked) ? 1 : 0;
                    that.setData({
                        checked,
                        fisonsale
                    });
                }
            }
        })
    },
    /* 表单验证 */
    formVerify() {
        let errTips = this.data.errTips,
            verify = true;
        /* 验证产品名称  */
        if (!_Verify.required(this.data.fprodname)) {
            errTips.fprodname = true;
            verify = false;
        }
        /* 验证产品编码  */
        if (!_Verify.required(this.data.fprodnum)) {
            errTips.fprodnum = true;
            verify = false;
        }

        /* 验证产品分类  */
        if (!_Verify.required(this.data.fprodclassname)) {
            errTips.fprodclassname = true;
            verify = false;
        }
        /* 验证附件列表  */
        if (!_Verify.required(this.data.attinfos, "请上传产品图片")) {
            verify = false;
        }
        this.setData({
            errTips
        })
        return verify;
    },
    /* 提交 */
    submit() {
        if (!this.formVerify() || this.data.throttle) return;
        //请求参数
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.products.products",
            "method": "insertOrModifyProducts",
            "content": {
                "tagents_productid": this.data.tagents_productid,
                "fprodnum": this.data.fprodnum,
                "fprodname": this.data.fprodname,
                "fintroduction": this.data.fintroduction,
                "fisonsale": this.data.fisonsale,
                "fprodclassname": this.data.fprodclassname
            }
        }).then(res => {
            console.log(res)
            if (res.msg != "成功") return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                throttle: true //设置节流阀
            });
            let content = {
                ownerid: res.data[0].tagents_productid,
                ownertable: "tagents_product",
                tattachmentid: 0
            };
            this.selectComponent("#UploadFiles").saveTheChanges(content);
            wx.showToast({
                title: '保存成功',
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1,
                })
            }, 500)
        })
    },
    /* 获取焦点 */
    inputFocus(e) {
        const {
            name
        } = e.currentTarget.dataset;
        let errTips = this.data.errTips;
        errTips[name] = false;
        /* 经营类目提示框 */
        if (name == 'showSaleprod') {
            errTips[name] = true;
        }
        this.setData({
            errTips
        })
    },
    /* 失去焦点 */
    inputBlur(e) {
        const {
            name
        } = e.currentTarget.dataset;
        const {
            value
        } = e.detail;
        let errTips = this.data.errTips;
        if (value.trim() == "") errTips[name] = true;
        /* 经营类目提示框,字符串转化数组 */
        if (name == 'showSaleprod') {
            errTips[name] = false;
            const ftag = this.data.showSaleprod.split(" ");
            this.setData({
                ftag
            })
        };
        this.setData({
            errTips
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

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

    },

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

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

    },

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

    },

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

    }
})