import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel;
import {
    TestVerify
} from "../../utils/verify";
const _Verify = new TestVerify();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showCalendar: false, //日历显示
        tsupplyanddemandid: 0, //ID 0为新增
        popups: false, //弹出层控制
        ftype: "", //供需类型
        ftitle: "", //供需标题
        fcontent: "", //需求内容
        throttle: false, //节流阀
        /* 必填 */
        errTips: {
            ftype: false,
            ftitle: false,
            fcontent: false
        },
        dateStart: null, //日期选择开始时间 当前时间+1天
        dateEnd: null, //日期选择结束时间 当前时间+6个月
        optionDate: "选择日期", //选择日期
        checked: true, //开关
        fstatus: '', //状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        /* 设置日期选择器 */
        const time = new Date(Date.parse(new Date()) + 86400000),
            End = new Date(Date.parse(time) + 15724800000);
        this.setData({
            dateStart: time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate(),
            dateEnd: End.getFullYear() + '-' + (End.getMonth() + 1) + '-' + End.getDate()
        });
        /* 修改参数 */
        if (options.data != undefined) {
            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
                }
                attinfos.push(arr)
            };
            let optionDate = this.data.optionDate;
            /* 格式化时间 */
            if (data.fenddate != null && data.fenddate != '') {
                optionDate = data.fenddate
                /* const i = data.fenddate.lastIndexOf(' ');
                let end = data.fenddate.slice(1, i).split(" ");
                optionDate = end[0]; */
            };
            /* 修改开关状态 */
            let checked = true;
            if (data.fstatus == "已过期" || data.fstatus == "已解决" || data.fstatus == "新建") checked = false;
            this.setData({
                ftype: data.ftype, //供需类型
                ftitle: data.ftitle, //供需标题
                fcontent: data.fcontent, //需求内容	
                fenddate: data.fenddate, //截止日期
                attinfos, //附件列表
                tsupplyanddemandid: data.tsupplyanddemandid, //ID 0为新增
                optionDate,
                fstatus: data.fstatus,
                checked
            })
        };
    },
    /* input事件剔除特殊字符 */
    eliminate(value) {
        const {
            name
        } = value.target.dataset;
        this.setData({
            [name]: _Verify.Eliminate(value.detail)
        })
    },
    /* 日期选择 */
    dateChange({
        detail
    }) {
        this.setData({
            optionDate: detail.value
        })
    },
    /* 添加图片 */
    imageChange(data) {
        this.setData({
            attinfos: data.detail.fileList
        })
    },
    /* 表单验证 */
    formVerify() {
        let errTips = this.data.errTips,
            verify = true;
        /* 验证分类 */
        if (!_Verify.required(this.data.ftype)) {
            errTips.ftype = true;
            verify = false;
        }
        /* 验证标题  */
        if (!_Verify.required(this.data.ftitle)) {
            errTips.ftitle = true;
            verify = false;
        }
        /* 验证内容  */
        if (!_Verify.required(this.data.fcontent)) {
            errTips.fcontent = true;
            verify = false;
        }
        this.setData({
            errTips
        })
        return verify;
    },
    /* 提交 */
    submit() {
        if (!this.formVerify()) return wx.showToast({
            title: '请检查表单内容',
            icon: "error"
        });
        if (this.data.throttle) return;
        this.addOrModify()
    },
    /*  */
    checkedOnChange({
        detail
    }) {
        const that = this;
        wx.showModal({
            title: '提示',
            content: (detail) ? '是否确认上架' : "是否确认下架",
            success: function (res) {
                if (res.confirm) {
                    that.setData({
                        checked: detail
                    })
                }
            }
        })
    },
    /* 新增或修改 */
    addOrModify() {
        let fenddate = this.data.optionDate;
        if (this.data.optionDate == '选择日期') fenddate = '';
        /* 发送请求 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "insertormodify",
            "content": {
                "tsupplyanddemandid": this.data.tsupplyanddemandid,
                "ftype": this.data.ftype,
                "ftitle": this.data.ftitle,
                "fcontent": this.data.fcontent,
                "fenddate": fenddate,
                "fissupply": 0
            }
        }).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].tsupplyanddemandid,
                ownertable: "tsupplyanddemand",
                tattachmentid: 0
            };
            this.selectComponent("#UploadFiles").saveTheChanges(content);
            let httpData = {
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "customer.supplyanddemand.supplyanddemand",
                "method": "updatesupplyanddemandstatus",
                "content": {
                    "tsupplyanddemandid": res.data[0].tsupplyanddemandid,
                    "fstatus": "发布"
                }
            }
            //判断是否上架
            if ((res.data[0].fstatus == '新建' || res.data[0].fstatus == '已解决' || res.data[0].fstatus == '已过期')) {
                if (this.data.checked) {
                    _Http.basic(httpData).then(res => {
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        })
                    })
                }
            };
            //判断是否下架
            if ((res.data[0].fstatus == '待对接' || res.data[0].fstatus == '正在对接')) {
                if (!this.data.checked) {
                    httpData.content.fstatus = '新建';
                    _Http.basic(httpData).then(res => {
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        })
                    })
                }
            };

            wx.showToast({
                title: '保存成功',
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1,
                })
            }, 500)
        })
    },
    /* 弹出层 */
    showPop() {
        this.setData({
            popups: !this.data.popups
        })
    },
    /* 单选改变 */
    radioChange(value) {
        this.setData({
            ftype: value.detail,
            popups: false,
            "errTips.ftype": false
        })
    },
    /* 获取焦点 */
    inputFocus(e) {
        const {
            name
        } = e.currentTarget.dataset;
        let errTips = this.data.errTips;
        errTips[name] = false;
        this.setData({
            errTips
        })
    },
    /* 失去焦点 */
    inputBlur(e) {
        const {
            name
        } = e.currentTarget.dataset;
        const {
            value
        } = e.detail;
        if (value.trim() == "") {
            let errTips = this.data.errTips;
            errTips[name] = true;
            this.setData({
                errTips
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

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

    },

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

    },

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

    }
})