<template>
  <view class="scan-code" :class="{ 'scan-code--elder': $uiStyle === 'elder' }">
    <view class="header">
      <view class="text1">欢迎使用</view>
      <view class="text2">"数字门牌 . 一码到家"</view>
      <view class="text3">奉化区民生服务嵌入二维码门牌信息集成系统</view>
    </view>
    <view class="content">
      <view class="scan">
        <view class="scan-btn" @click="scanCode">
          <image style="width: 42px; height: 42px; margin-bottom: 3px" mode="scaleToFill"
            src="/subPages/static/images/common/scan.png" />
          <view class="text">点击扫码</view>
        </view>
        <view>
          <view class="prompt">
            <text>1.点击扫码按钮&emsp;2.扫描门牌二维码</text>
          </view>
          <view class="search">
            <!-- <view class="button" @tap="searchDoorplate">搜索门牌 ></view> -->
            <view class="button">
              <text class="text">附近的门牌</text>
              <text @tap="searchDoorplate">搜索门牌</text>
            </view>
            <view class="search-list">
              <scanItem v-for="item in nearbyAddressList" :key="item.id" :text="item.houseNumberAddress"
                @tap="newAddress(item.id)" />
            </view>
          </view>
        </view>
      </view>
      <custom-footer></custom-footer>
    </view>
  </view>
</template>

<script>
const app = getApp();
import { getNearbyHouseNumber } from '@/api/scanCode.js';

export default {
  name: "scanCode",
  data() {
    return {
      latitude: 29.8172,
      longitude: 121.547,
      nearbyAddressList: []
    };
  },
  onLoad(options) {
    // console.log(options);
//    ZWJSBridge.getLocation({}).then(res => {
//      console.log(res)
//      this.getVicinity(res);
//    }).catch(err => {
//      console.log(err)
//    })
    // wx.getLocation().then(res => {
    //   // console.log(res);
    //   this.getVicinity(res);
    // })
  },
  methods: {
    // 跳转搜索门牌页面
    searchDoorplate() {
      uni.navigateTo({
        url: "/subPages/pages/scanCode/searchDoorplate",
      });
    },
    // 扫描门牌二维码
    scanCode() {
      console.log("触发扫码");
      const _this = this;
      uni.scanCode({
        success: function (res) {
          console.log("二维码内容：", res);
          const params = _this.getQuery(res.result);
          if (/* params.Type && params.NBID && */ params.code) {
            app.globalData.options = {
              /*   NBID: params.NBID,
                Type: Number(params.Type), */
              code: params.code,
            };
            // app.globalData.NBID = params.NBID;
            uni.switchTab({
              url: '/pages/home/home'
            });
          } else {
            uni.showToast({
              title: "无效的门牌二维码",
            });
          }
        },
      });
    },
    // 解析URL中的参数
    getQuery(url) {
      const str = url.substr(url.indexOf("?") + 1);
      const arr = str.split("&");
      const result = {};
      for (let i = 0; i < arr.length; i++) {
        const item = arr[i].split("=");
        result[item[0]] = item[1];
      }
      return result;
    },
    getVicinity(obj) {
      getNearbyHouseNumber({
        latitude: obj.latitude,
        longitude: obj.longitude
      }).then(res => {
        console.log(res);
        this.nearbyAddressList = res.data;
      })
    },
    newAddress(id) {
      // console.log(app.globalData.options);
      app.globalData.options = {
        code: id
      }
      uni.switchTab({
        url: '/pages/home/home'
      });
    }
  },
};
</script>

<style lang="scss" scoped>
.scan-code {
  width: 100%;

  .header {
    width: 100%;
    height: 20vh;
    padding: 15px;
    box-sizing: border-box;
    /* 避免左右可以滑动 */
    background-image: url("data:image/png;base64,[encoded data, 120908 characters]");
    background-repeat: no-repeat;
    background-size: 100% 100%;

    .text1 {
      text-align: left;
      font-size: 15px;
      font-weight: 600;
      color: #fff;
      opacity: 0.5;
      margin-bottom: 20px;
    }

    .text2 {
      text-align: center;
      font-size: 18px;
      font-weight: 800;
      color: #fff;
      margin-bottom: 5px;
    }

    .text3 {
      text-align: center;
      font-size: 15px;
      font-weight: 600;
      color: #fff;
      opacity: 0.5;
    }
  }

  .content {
    position: absolute;
    bottom: 0;
    z-index: 999;
    width: 100%;
    height: 80vh;
    background-color: #fff;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;

    .scan {
      height: calc(100% - 100px);
      display: flex;
      flex-direction: column;

      .scan-btn {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        background-color: #3a78f5;
        margin: 40px auto 15px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .text {
          margin-top: 5px;
          font-size: 15px;
          font-weight: 500;
          color: #fff;
          opacity: 0.8;
        }
      }

      .prompt {
        width: 280px;
        text-align: center;
        padding: 7px 0;
        background-color: #f1eaea;
        border-radius: 15px;
        margin: 0 auto 38px;
        font-size: 14px;
        color: #999;
      }

      .search {
        width: 100%;
        padding: 0 16px;
        box-sizing: border-box;
        flex: auto;
        display: flex;
        flex-direction: column;

        .button {
          color: #1492FF;
          margin-bottom: 10px;

          .text {
            font-weight: 600;
            margin-right: 14px;
          }
        }

        .search-list {
          flex: auto;
        }



        // .button {
        //   text-align: right;
        //   padding: 0 20px;
        //   color: #3a78f5;
        //   font-size: 15px;
        //   font-weight: 400;
        // }
      }
    }

    .rights {
      height: 80px;
      text-align: center;
      font-size: 13px;
      font-weight: 200;
      color: #999;
      padding-top: 15px;

      .text1 {
        margin-bottom: 5px;
      }

      .text2 {
        .phone-number {
          color: #3a78f5;
        }
      }
    }
  }
}

.scan-code--elder {
  .header {

    /* 	.text1,.text3{
          font-size: calc(13px * $zlb-elder-font-scale);
			} */
    .text2 {
      font-size: calc(18px * $zlb-elder-font-scale);
    }
  }

  .content {
    .text {
      font-size: calc(15px * $zlb-elder-font-scale);
    }

    .scan {
      .scan-btn {
        .text {
          font-size: 19px;
        }
      }

      .prompt {
        font-size: calc(12px * $zlb-elder-font-scale);
      }

      .search {
        .button {
          font-size: calc(12px * $zlb-elder-font-scale);
        }
      }
    }

    .rights {
      font-size: calc(13px * $zlb-elder-font-scale);
    }
  }
}</style>
