const ut = require("../../utils/util.js");
var os = getApp().globalData.setting;
Component({
  properties: {
    object: {
      type: Object,
      value: null
    },
    f_user_tool: {
      type: Object,
      value: null
    },
    c_list: {
      type: Object,
      value: null
    },
    qy_data: {
      type: Object,
      value: null
    },
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    //-- 是不是初始化了 --
    is_dengji:{
      type:Number,
      value:0
    },
    //-- 是不是有助力活动 --
    is_assistance:{
      type:Number,
      value:0
    },
    enableMeiye:{
      type:Number,
      value:0
    }
  },

  data: {
    iurl:os.imghost,
    sys_switch:null,
    userInfo:null,
    hiddenCS: true,
  },

  ready: function() {

        var th=this;

        if(getApp().globalData.userInfo){
          this.setData({userInfo:getApp().globalData.userInfo});
        }

        /*-------系统是否开通等级卡，会员是等级卡-----*/
        getApp().getConfig2(function (e) {
          var t_swi = e.switch_list;
          if (t_swi) t_swi = JSON.parse(t_swi)
          if (t_swi) {
            th.setData({sys_switch: t_swi});
          }
        })
       let img_host=this.data.iurl;

  },

  methods: {
      // 这里是一个自定义方法
      goto_nav: function (e) {
        var th = this;
        var url = e.currentTarget.dataset.url;
        if (th.data.userInfo != null) {
          console.log('跳转');
          getApp().goto(url);
        } else {
          wx.navigateTo({
            url: '/packageE/pages/togoin/togoin',
          })
        }
      },
      //跳转到链接
      goto: function (e) {
        console.log(e);
        console.log('跳转');
        var url = e.currentTarget.dataset.url;
        wx.navigateTo({ url: url })
        // getApp().goto(url);
      },
      //--跳转到权益页面--
      go_qy: function () {
        if (!this.data.userInfo) return false;
        var url = "/packageG/pages/user/userqy/userqy";
        getApp().goto(url);
      },

      openCS() {
        let self=this;
        // 判断会员状态
        let user_info = getApp().globalData.userInfo;
        if (user_info == null || user_info.mobile == undefined || user_info.mobile == '' || user_info.mobile == null) {
          wx.navigateTo({
            url: '/packageE/pages/togoin/togoin',
          })
          return false;
        };
        // csType/在线客服设置：
        // 0关闭（如果有设置热线电话，则显示拨打热线电话）、
        // 1小程序客服、
        // 2企业微信客服
        if (this.data.sys_switch) {
          let csType = this.data.sys_switch.weapp_customertype;
          if (csType == 0) {
            // 拨打电话号码
            this.contactService();
          } else {
            this.getTel()
                .then(() => {
                  self.setData({
                    hiddenCS: false,
                  });
                });
          }
        }
      },

      //---------联系客服------------
      contactService: function () {
        getApp().com_call(this);
      },

      //关闭客服操作菜单
      closeCS() {
        this.setData({
          hiddenCS: true,
        });
      },

      //添加卡包
      addcard: function () {
        var res = this.data.add_card_data;
        if (!res) return false;

        var arr = new Array(6)
        arr[0] = res.code;
        arr[1] = res.timestamp;
        arr[2] = res.jsapiTicket;
        arr[3] = res.openid;
        arr[4] = res.cardid;
        arr[5] = res.nonceStr;
        var c = arr.sort();
        var newstr = "";
        for (var i = 0; i < c.length; i++) {
          newstr += c[i];
        }

        var sh1 = ut.sha1(newstr);
        wx.addCard({
          cardList: [
            {
              cardId: res.cardid,
              cardExt: '{"code": "' + res.code + '", "openid": "' + res.openid + '","timestamp": "' + res.timestamp + '","nonce_str":"' + res.nonceStr + '","outer_str":"web", "signature":"' + sh1 + '"}'
            }

          ],
          success: function (res) { },
          cancel: function (res) { }
        });
      },

      // 获取客服热线
      getTel() {
        var self = this;
        return new Promise((resolve, reject) => {
          var s = getApp();
          s.getConfig(function (t) {
            if (t.store_tel == undefined) {
              getApp().request.get("/api/weshop/store/get/" + os.stoid, {
                isShowLoading: 1,
                data: {},
                success: function (rs) {
                  getApp().globalData.config = rs.data.data;
                  if (rs.data.data.store_tel == null && rs.data.data.store_tel == undefined) {
                    getApp().showWarning("商家未设置电话");
                    return false;
                  } else {
                    self.setData({
                      store_tel: rs.data.data.store_tel,
                    });
                    // s.confirmBox("客服热线：" + rs.data.data.store_tel);
                  };

                }
              })
            } else {
              self.setData({
                store_tel: t.store_tel,
              });
              // s.confirmBox("客服热线：" + t.store_tel);
            }
            resolve();
          });
        });
      },



  },

  observers: {
    // 监听 num1 和 num2 数据的变化
    'f_user_tool': function(obj){
      console.log(1111);
      console.log(obj);
      if(obj){
        var f_user_tool=JSON.parse(JSON.stringify(obj));
        var list=this.properties.object.list;

        if(list && list.length) {
          //进行循环
          for (let iu = 0; iu < list.length; iu++) {
            let fd = f_user_tool.find(function (e) {
              return e.id == list[iu].id;
            })
            if (fd) {
              list[iu].icoimg = fd.icoimg;
              list[iu].new_weappurl = fd.new_weappurl;
            } else {
              list.splice(iu, 1);
              iu--;
            }
          }
        }else{
          list=[];
        }
        //-- 如果是有新的tool要添加进去 --
        for (let ig = 0; ig <f_user_tool.length ; ig++) {

          var item=f_user_tool[ig];
          let fd= list.find(function (e){
            return e.id==f_user_tool[ig].id;
          })
          if(fd) continue;
          let ob={
            show:true,id:item.id,icoimg:item.icoimg,name:item.name,new_weappurl:item.new_weappurl
          }
          list.push(ob);
        }


        this.setData({user_tool:list})
      }
    }
  },
})