const Parser = require('../xml/xmldom/dom-parser')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hname:'',//房产
        datatimestart:'',//日期起始
        datatimeend:'',//日期结束
        dfdapplydate:'',//派单时间
        txtdrequiredate:'',//完成时间
        txtvcontent:'',// 报事内容
        hddhcode:'',//房产编号
        ownerid:'',
        txtvplace:'',//报事地址
        txtvtel:'',
        owername:'',
        table:[],//上传图片的对象
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var timestamp = Date.parse(new Date());
        var date = new Date(timestamp);
        var Y =date.getFullYear();
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
        var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
        var that = this;
        that.setData({
            datatimestart:Y + '-'  + M+ '-' + D,
            dfdapplydate:Y + '-'  + M+ '-' + D,
            txtdrequiredate:Y + '-'  + M+ '-' + D,
            datatimeend:Y + 2 + '-' + M + '-' + D,
            hname:options.hname,
            ownerid:options.ownerid,
            txtvtel:options.tel,
            owername:options.ownername,
            hddhcode:options.hcode,
        })
    },
    bindDateChangeb:function(e){
        this.setData({
            txtdrequiredate: e.detail.value
        })
    },
    // 选择日期
    bindDateChange: function(e) {
        this.setData({
            dfdapplydate: e.detail.value
        })
      },
    // 删除图片
    removeImage(e) {
        var index = e.target.dataset.index;
        var imgs = this.data.table;
        imgs.splice(index,1);
        this.setData({
            table:imgs,
        })
    },
    // 选择图片
    chooseImage:function(e){
        var that = this;
        wx.chooseImage({
            count:3,
            sizeType:['original','compressed'],
            sourceType:['album','camera'],
            success(res){
                that.setData({
                table: res.tempFilePaths
                });
            }
        })
    },
    ceShibtnJiaoFei:function(e){
    },
    // 点击提交报修 
    btnJiaoFei: function (e) {
        var that= this;
        if(this.data.txtvtel==''){
            wx.showToast({
                title: '请填写手机号',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if(this.data.txtvplace==''){
            wx.showToast({
                title: '请填写报事地址',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        let fileurl = "";// 图片名称
        if(this.data.table.length>0){
            // 拼接多(单)个图片名称
            for (let index = 0; index < this.data.table.length; index++) {
                if(this.data.table[index].substring(0,11)=='http://tmp/'){
                    fileurl += this.data.table[index].substring(11)+',';
                }
                if(this.data.table[index].substring(0,9)=='wxfile://'){
                    fileurl += this.data.table[index].substring(9)+',';
                }
            }    
        }
        var method='Insertfdc_pm_repair ';
        var wsdlurl= app.webUrl;
        var targetNamespace='http://tempuri.org/';
        var datacopy= '<?xml version="1.0" encoding="utf-8"?>';
        datacopy += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
        datacopy += '<soap:Header><MySoapHeader xmlns="http://tempuri.org/">';
        datacopy += `<UserName>${app.UserName}</UserName>`;
        datacopy += `<PassWord>${app.PassWord}</PassWord>`;
        datacopy += '</MySoapHeader></soap:Header>';
        datacopy += '<soap:Body>';
        datacopy += '<Insertfdc_pm_repair xmlns="http://tempuri.org/">';
        datacopy += `<dfdapplydate>${this.data.dfdapplydate}</dfdapplydate>`;
        datacopy += `<txtdrequiredate>${this.data.txtdrequiredate}</txtdrequiredate>`;
        datacopy += `<hddhcode>${this.data.hddhcode}</hddhcode>`;
        datacopy += `<hddownerid>${this.data.ownerid}</hddownerid>`;
        datacopy += `<txtvcontent>${this.data.txtvcontent}</txtvcontent>`;
        datacopy += `<txtvplace>${this.data.txtvplace}</txtvplace>`;
        datacopy += `<txtvtel>${this.data.txtvtel}</txtvtel>`;
        datacopy += `<fileurl>${fileurl}</fileurl>`;
        datacopy += '</Insertfdc_pm_repair>';
        datacopy += '</soap:Body>';
        datacopy += '</soap:Envelope>';
        wx.request({
            url: wsdlurl,
            data: datacopy, 
            method: 'POST', 
            header: { 
                'Content-Type':'text/xml; charset=utf-8',
                'SOAPAction':targetNamespace+method,
            }, 
            success:function(res){
                var XMLParser = new Parser.DOMParser();
                var doc = XMLParser.parseFromString(res.data);
                var Insertfdc_pm_repairResult = doc.getElementsByTagName("Insertfdc_pm_repairResult")[0].firstChild.nodeValue;
                if(Insertfdc_pm_repairResult=='保存成功！'){
                    wx.showToast({
                        title: '提交成功!',
                        icon: 'success',
                        duration: 1000
                    })
                    // 图片上传
                    for (let index = 0; index < that.data.table.length; index++) {
                        wx.uploadFile({
                            filePath:  that.data.table[index],
                            name: 'name',
                            url: app.coreUrl+ 'InsertPicture',
                            header:{
                                'content-type':'multipart/form-data'
                            },
                            name:'upload',
                            success:function(res){
                                if(res.data=='true'){
                                    wx.navigateBack({
                                        delta: 2
                                    })
                                }
                            },
                        })
                    }
                } else {
                    wx.showToast({
                        title: Insertfdc_pm_repairResult,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    // 修改报事地址
    updateTxtvplace: function (e) {
        this.setData({
            txtvplace: e.detail.value
        })
    },
    // 编辑报事人电话
    updateTxtvtel: function (e) {
        this.setData({
            txtvtel: e.detail.value
        })
    },
    // 编辑报事人电话
    updateTxtvcontent: function (e) {
        this.setData({
            txtvcontent: e.detail.value
        })
    },
    // 预览图片
    handleImagePreview: function (e) {
        var idx = e.target.dataset.index;
        var imag = this.data.table;
        wx.previewImage({
        current: imag[idx],  //当前预览的图片
        urls: imag,  //所有要预览的图片
        })
    },

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

    },

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

    },

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

    },

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

    }
})