let app = getApp();
//替换成开发者后台设置的安全域名
let domain = app.globalData.domain;
//获取用户信息的url

let getSignListUrl = domain + "/dingding/getSignList";

let setSeatUrl = domain + "/dingding/setSeat";
Page({
  data: {

    drag:{
     display:"none",
     x:0,
     y:0,
     name:"",
     id:"",
     isSet:false
    },
    seatNum:null,
   // centerSeatList: null,
    leftSeatList:[],
    rightSeatList:[],
    bottomSeat:null,
    topSeat:null,
    visitorList:[],
    mid:null
  },
  longTap:function(e){
    var _this = this;
    var id = e.currentTarget.id;  
   // console.log(id);
    dd.createSelectorQuery().select('#'+id).boundingClientRect().exec((ret) => {
     // console.log(ret);
     
      _this.setData({
        "drag.x": ret[0].left ,
        "drag.y": ret[0].top,
        "drag.isSet":true,
        "drag.display": "block",
        "drag.name": e.currentTarget.dataset.name,
        "drag.id": e.currentTarget.dataset.id
      });
     
    })
   
   // console.log("长按事件");
  
    
  },
  move:function(e){
    this.setData({
      "drag.x": e.changedTouches[0].clientX,
      "drag.y": e.changedTouches[0].clientY,
     
    });
   
    //console.log("移动"+e);
  },
  stop: function(e) {
   // console.log("停止");
    this.setData({
      
      "drag.display": "none"
    });
    var _this = this;
    var isSet=this.data.drag.isSet;
    var sid = this.data.drag.id;
   // console.log("是否可设置"+isSet);
    if (isSet==true){
    dd.createSelectorQuery().select('#bottom').
      boundingClientRect().selectAll('.seatleft').boundingClientRect().selectAll('.seatright').boundingClientRect().
          select('#top').boundingClientRect().exec((ret) => {
      //console.log(ret);
      if(ret!=null){
        // console.log("进入查询节点方法");
        var x = e.changedTouches[0].clientX;
        var y = e.changedTouches[0].clientY;
       // console.log("x坐标"+x);
      //  console.log("y坐标" + y);
        if (x < ret[0].left + ret[0].width  && x > ret[0].left  && y < ret[0].top + ret[0].height  && y > ret[0].top ){
        // console.log("在底部");
          var name =this.data.drag.name;

          var topSeat = this.data.topSeat;
          var bottomSeat = this.data.bottomSeat;


          var rightSeatList = this.data.rightSeatList;
          var leftSeatList = this.data.leftSeatList;
          //var rightSeatList = this.data.rightSeatList;
          for (var j = 0; j < leftSeatList.length; j++) {
            if (name == leftSeatList[j].name) {
              leftSeatList[j].name = "未设置";
              leftSeatList[j].color = "#999";
            }
          }

          for (var j = 0; j < rightSeatList.length; j++) {
            if (name == rightSeatList[j].name) {
              rightSeatList[j].name = "未设置";
              rightSeatList[j].color = "#999";
            }
          }
          if (name == topSeat.name) {
            this.setData({
              "topSeat.name": "未设置",
              "topSeat.color": "#999"
            })
          }
          if (name == bottomSeat.name) {
            this.setData({
              "bottomSeat.name": "未设置",
              "bottomSeat.color": "#999"
            })
          }
          this.setData({
            leftSeatList,
            rightSeatList

          })
         this.setData({
           "bottomSeat.name":name,
           "bottomSeat.color": "#1696db" 
         });
          dd.httpRequest({
            url: setSeatUrl,
            method: 'POST',
            data: {
              mid: _this.data.mid,
              sid: sid,
              seatNumber: _this.data.bottomSeat.num
            },
            dataType: 'json',
            success: function(res) {
              //   console.log('success----', res)
            },
            fail: function(res) {
              console.log("httpRequestFail---", res)
              dd.alert({ content: JSON.stringify(res) });
            },
            complete: function(res) {
              dd.hideLoading();
            }

          });
        }
        var leftList = ret[1];
        for (var i = 0; i < leftList.length;i++){
         // console.log("在左部");
         // console.log("宽度" + leftList[i].width + "高度" + leftList[i].height);
        //  console.log("x" + x + "y" + y);
        //  console.log("这个点x" + leftList[i].x + "这个点y" + leftList[i].y);
          if (x < leftList[i].left + leftList[i].width  && x > leftList[i].left && y < leftList[i].top + leftList[i].height && y > leftList[i].top ) {
          //  console.log("在左部");
          //  console.log("宽度"+leftList[i].width + "高度"+leftList[i].height);
          //  console.log("x" + x + "y" + y);
           // console.log("这个点x" + leftList[i].x + "这个点y" + leftList[i].y);
            var name = this.data.drag.name;
            var topSeat = this.data.topSeat;
            var bottomSeat = this.data.bottomSeat;


            var rightSeatList = this.data.rightSeatList;
            var leftSeatList = this.data.leftSeatList;
            //var rightSeatList = this.data.rightSeatList;
            for (var j = 0; j < leftSeatList.length; j++) {
              if (name == leftSeatList[j].name) {
                leftSeatList[j].name = "未设置";
                leftSeatList[j].color = "#999";
               
              }
            }


            for (var j = 0; j < rightSeatList.length; j++) {
              if (name == rightSeatList[j].name) {
                rightSeatList[j].name = "未设置";
                rightSeatList[j].color = "#999";
              }
            }
            if (name == topSeat.name) {
              this.setData({
                "topSeat.name": "未设置",
                "topSeat.color": "#999"
              })
            }
            if (name == bottomSeat.name) {
              this.setData({
                "bottomSeat.name": "未设置",
                "bottomSeat.color": "#999"
              })
            }
            this.setData({
              leftSeatList,
              rightSeatList

            })
            //赋值
            leftSeatList[i].name=name;
            leftSeatList[i].color = "#1696db";
            
            this.setData({
              leftSeatList

            })
            dd.httpRequest({
              url: setSeatUrl,
              method: 'POST',
              data: {
                mid: _this.data.mid,
                sid: sid,
                seatNumber: leftSeatList[i].num
              },
              dataType: 'json',
              success: function(res) {
                //   console.log('success----', res)
              },
              fail: function(res) {
                console.log("httpRequestFail---", res)
                dd.alert({ content: JSON.stringify(res) });
              },
              complete: function(res) {
                dd.hideLoading();
              }

            });
        

        }
        }
        var rightList = ret[2];
        for (var i = 0; i < rightList.length; i++) {
          if (x < rightList[i].left + rightList[i].width  && x > rightList[i].left  && y < rightList[i].top + rightList[i].height  && y > rightList[i].top ) {
           // console.log("在右部");
           // console.log("宽度" + leftList[i].width + "高度" + leftList[i].height);
           // console.log("x" + x + "y" + y);
            //console.log("这个点x" + leftList[i].x + "这个点y" + leftList[i].y);
            var name = this.data.drag.name;

            var topSeat = this.data.topSeat;
            var bottomSeat = this.data.bottomSeat;


            var rightSeatList = this.data.rightSeatList;
            var leftSeatList = this.data.leftSeatList;
            //var rightSeatList = this.data.rightSeatList;
            for (var j = 0; j < leftSeatList.length; j++) {
              if (name == leftSeatList[j].name) {
                leftSeatList[j].name = "未设置";
                leftSeatList[j].color = "#999";
              }
            }
         
            for (var j = 0; j < rightSeatList.length; j++) {
              if (name == rightSeatList[j].name) {
                rightSeatList[j].name = "未设置";
                rightSeatList[j].color = "#999";
              }
            }
            if (name == topSeat.name){
              this.setData({
                "topSeat.name": "未设置",
                "topSeat.color": "#999"
              })
            }
            if (name == bottomSeat.name) {
              this.setData({
                "bottomSeat.name": "未设置",
                "bottomSeat.color": "#999"
              })
            }
            this.setData({
              leftSeatList

            })
            rightSeatList[i].name = name;
            rightSeatList[i].color = "#1696db";
            this.setData({
              rightSeatList

            })
            dd.httpRequest({
              url: setSeatUrl,
              method: 'POST',
              data: {
                mid: _this.data.mid,
                sid: sid,
                seatNumber: rightSeatList[i].num
              },
              dataType: 'json',
              success: function(res) {
                //   console.log('success----', res)
              },
              fail: function(res) {
                console.log("httpRequestFail---", res)
                dd.alert({ content: JSON.stringify(res) });
              },
              complete: function(res) {
                dd.hideLoading();
              }

            });


          }
        }
        if (ret[3]!=null){
          if (x < ret[3].left + ret[3].width  && x > ret[3].left && y < ret[3].top + ret[3].height  && y > ret[3].top ) {
         // console.log("在头部");
          var name = this.data.drag.name;
          var topSeat = this.data.topSeat;
          var bottomSeat = this.data.bottomSeat;


          var rightSeatList = this.data.rightSeatList;
          var leftSeatList = this.data.leftSeatList;
          //var rightSeatList = this.data.rightSeatList;
          for (var j = 0; j < leftSeatList.length; j++) {
            if (name == leftSeatList[j].name) {
              leftSeatList[j].name = "未设置";
              leftSeatList[j].color = "#999";
            }
          }

          for (var j = 0; j < rightSeatList.length; j++) {
            if (name == rightSeatList[j].name) {
              rightSeatList[j].name = "未设置";
              rightSeatList[j].color = "#999";
            }
          }
          if (name == topSeat.name) {
            this.setData({
              "topSeat.name": "未设置",
              "topSeat.color": "#999"
            })
          }
          if (name == bottomSeat.name) {
            this.setData({
              "bottomSeat.name": "未设置",
              "bottomSeat.color": "#999"
            })
          }
          this.setData({
            leftSeatList,
            rightSeatList

          })

          this.setData({
            "topSeat.name": name,
            "topSeat.color": "#1696db"

          })

            dd.httpRequest({
              url: setSeatUrl,
              method: 'POST',
              data: {
                mid: _this.data.mid,
                sid: sid,
                seatNumber: _this.data.topSeat.num
              },
              dataType: 'json',
              success: function(res) {
                //   console.log('success----', res)
              },
              fail: function(res) {
                console.log("httpRequestFail---", res)
                dd.alert({ content: JSON.stringify(res) });
              },
              complete: function(res) {
                dd.hideLoading();
              }

            });
        }
        }
       // ret[0].x + ret[0].width   ret[0].x - ret[0].width
 // ret[0].y + ret[0].height   ret[0].y - ret[0].height
      }
     

    })
    }
    _this.setData({
    

      "drag.isSet": false,

    });
   // console.log("停止" + e);
  },
  onLoad(query) {
    var visitorList = this.data.visitorList;

    var _this=this;
   
    // var centerSeatList = this.data.leftSeatList;
    var leftSeatList = this.data.leftSeatList;
    var rightSeatList = this.data.rightSeatList;
   console.log("座位",query.seatNumber)
    this.setData({
      mid:query.mid,
      seatNum: query.seatNumber
    });

    var seatNum = this.data.seatNum;
    dd.httpRequest({
      url: getSignListUrl,
      method: 'POST',
      data: {
        mid: query.mid
      },
      dataType: 'json',
      success: function(res) {
        visitorList=res.data.result;
        _this.setData({
          visitorList
        });
        leftSeatList = [];
        rightSeatList = [];
        _this.setData({
          leftSeatList,
          rightSeatList
        })

        if (seatNum % 2 != 0) {
          var bottom = {};
          bottom["name"] = "未设置";
          bottom["num"] = seatNum;
          bottom["color"] = "#999";
          _this.setData({
            bottomSeat: bottom
          });
          for (var i = 1; i <= seatNum - 1; i++) {
            if (i % 2 == 0) {
              var right = {};
              right["name"] = "未设置";
              right["num"] = i;
              right["color"] = "#999";
              rightSeatList.push(right);
            } else {
              var left = {};
              left["name"] = "未设置";
              left["num"] = i;
              left["color"] = "#999";
              leftSeatList.push(left);

            }

          }

          _this.setData({ leftSeatList });
          _this.setData({ rightSeatList });
          // console.log(this.data.leftSeatList);
        } else {
          var bottom = {};
          bottom["name"] = "未设置";
          bottom["num"] = seatNum;
          bottom["color"] = "#999";
          var top = {};
          top["name"] = "未设置";
          top["num"] = seatNum - 1;
          top["color"] = "#999";
          for (var j = 0; j < visitorList.length; j++) {
            if (visitorList[j].seatNumber == seatNum-1) {
              top["name"] = visitorList[j].signName;
              top["color"] = "#1696db";
            }
            if (visitorList[j].seatNumber == seatNum) {
              bottom["name"] = visitorList[j].signName;
              bottom["color"] = "#1696db";
            }
          }
          _this.setData({
            bottomSeat: bottom,
            topSeat: top
          });

          for (var i = 1; i <= seatNum - 2; i++) {
            if (i % 2 == 0) {
              var right = {};
              right["name"] = "未设置";
              right["num"] = i;
              right["color"] = "#999";
              for (var j = 0; j < visitorList.length; j++) {
                if (visitorList[j].seatNumber == i) {
                  right["name"] = visitorList[j].signName;
                  right["color"] = "#1696db";
                }
              }
              rightSeatList.push(right);
            } else {
              var left = {};
              left["name"] = "未设置";
              left["num"] = i;
              left["color"] = "#999";
              for (var j = 0; j < visitorList.length; j++) {
                if (visitorList[j].seatNumber == i) {
                  left["name"] = visitorList[j].signName;
                  left["color"] = "#1696db";
                }
              }
              leftSeatList.push(left);

            }
          }

          _this.setData({ leftSeatList });
          _this.setData({ rightSeatList });
          // console.log(this.data.leftSeatList);

        }
 
      },
      fail: function(res) {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: function(res) {
        dd.hideLoading();
      }

    });
    //   for(var i=0;i<16;i++){
    //          var visitor={};
    //          visitor["name"]="张三三三三三"+i;
    //         visitorList.push(visitor);         

    //   }
    // this.setData({
    //   visitorList
    // });

    // 页面加载
 


  },
 
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
     

  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
  },
  viewTap() {
    // 事件处理
    this.setData({
      text: 'Set data for update.'
    })
  },
  go() {
    // 带参数的跳转，从 page/index 的 onLoad 函数的 query 中读取 xx
   // dd.navigateTo({ url: '/page/index?xx=1' })
  },
 
})