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

    /**
     * 页面的初始数据
     */
    data: {
      type:'',
      table:[]
    },

       /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
       
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (optitons) {
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 1000
          })
        var that = this;
        var method='Getfdc_pm_owner';
        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 += '<Getfdc_pm_owner  xmlns="http://tempuri.org/">';
        datacopy += `<weixinid>${optitons.openid}</weixinid>`;
        datacopy += `<ownerid>${optitons.phone}</ownerid>`;
        datacopy += '</Getfdc_pm_owner >';
        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 Getfdc_pm_ownerResult = JSON.parse(doc.getElementsByTagName("Getfdc_pm_ownerResult")[0].firstChild.nodeValue)
                var talone = [];
                var itemone = {
                  id:0,
                  pname:'',
                  house:[]
                }
                // 判断当前客户只有一套房产直接跳转报事界面
                // if(Getfdc_pm_ownerResult.Table.length==1){
                //   wx.navigateTo({
                //     url: `../baoxiu/baoxiu?ownerid=${Getfdc_pm_ownerResult.Table[0].ownerid}&hname=${Getfdc_pm_ownerResult.Table[0].hname}&tel=${Getfdc_pm_ownerResult.Table[0].tel}&hcode=${Getfdc_pm_ownerResult.Table[0].hcode}&ownername=${Getfdc_pm_ownerResult.Table[0].ownername}`,
                //   })
                // }
                for (let index = 0; index < Getfdc_pm_ownerResult.Table.length; index++) {
                  if(itemone.pname!=Getfdc_pm_ownerResult.Table[index].pname){
                    itemone.id = Getfdc_pm_ownerResult.Table[index].ownerid;
                    itemone.pname = Getfdc_pm_ownerResult.Table[index].pname;
                    for (let l = 0; l < Getfdc_pm_ownerResult.Table.length; l++) {

                      var a = {
                        hname:Getfdc_pm_ownerResult.Table[l].hname,
                        ownerid: Getfdc_pm_ownerResult.Table[l].ownerid,
                        hcode: Getfdc_pm_ownerResult.Table[l].hcode,
                        ownername:Getfdc_pm_ownerResult.Table[l].ownername,
                        tel: Getfdc_pm_ownerResult.Table[l].tel,
                      }
                    itemone.house.push(a);
                    }
                    talone.push(itemone);
                  }
                }
                
                if(optitons.type==undefined){
                  that.setData({
                    table:talone
                  })
                }
                else{
                  that.setData({
                    table:talone,
                    type:optitons.type
                  })
                }
            }
        })
    },
    texturlBX:function(e){
      // 判断是否从【我要报修】页面还是从【房产信息】页面跳转而来
      if(this.data.type==1){
        wx.navigateTo({
          url: `../baoxiu/baoxiu?ownerid=${e.target.dataset.ownerid}&hname=${e.target.dataset.hname}&tel=${e.target.dataset.tel}&hcode=${e.target.dataset.hcode}&ownername=${e.target.dataset.ownername}`,
        })
      }
    },
})