<template>
  <view class="main">
    <div class="top" v-if="false">
      <div style="display: inline-block">
        <div class="first" @click="goSelectServer">
          <image class="addressIcon" src="/static/images/home/blackFixed.png" />
          <div class="text">{{ nowServiceObj.SERVERPART_NAME || "" }}</div>
          <image class="moreIcon" src="/static/images/home/rightArrow.png" />
        </div>
      </div>
    </div>
    <div class="headerBox">
      <div
        class="searchBox"
        :style="{
          width: showSearchBtn ? 'calc(100% - 184rpx)' : 'calc(100% - 104rpx)',
        }"
      >
        <image class="searchIcon" src="/static/images/home/searchIcon.png" />
        <input
          class="inputBox"
          placeholder="请输入物品信息"
          style="font-size: 28rpx"
          v-model="searchText"
          @focus="clickSearch"
          @blur="noClickSearch"
        />
        <!-- <image class="searchIcon" src="/static/images/home/searchIcon.png" />
        <span class="searchText">请输入服务区</span> -->
      </div>
      <div class="searchBtn" v-if="showSearchBtn" @click="handleGetListData">
        搜索
      </div>
      <div class="filterBox" @click="showFilter">
        <image class="filterIcon" src="/static/images/home/filterIcon.svg" />
      </div>
    </div>

    <div class="pageList">
      <div
        v-if="dataList && dataList.length > 0"
        style="width: 100%; height: 100%"
      >
        <div class="pageTitle">物品 ({{ dataList.length || "" }})</div>
        <div class="listBox">
          <div class="shopItem" v-for="(item, index) in dataList" :key="index">
            <div class="title">
              <span class="normal">{{ item.beforeOtherName }}</span>
              <span class="light">{{ item.lightName }}</span>
              <span class="normal">{{ item.afterOtherName }}</span>
            </div>
            <div class="timeBox">
              <image class="timeIcon" src="/static/images/home/timeIcon.png" />
              <span class="timeText">{{ item.SUGGESTION_CREATEDATE }}</span>
            </div>
            <div class="phoneBox">
              <image
                class="phoneIcon"
                src="/static/images/home/phoneIcon.svg"
              />
              <span class="phoneText">{{ item.PHONE_NUMBER }}</span>
            </div>
            <div class="callPhoneBox" @click="handleCallPhone(item)">
              <image
                class="callPhoneIcon"
                src="/static/images/home/phone.png"
              />
            </div>
          </div>
        </div>
      </div>
      <div
        v-if="!(dataList && dataList.length > 0)"
        style="width: 100%; height: 100%"
      >
        <no-data text="暂无物品信息" :isShow="true" :type="1"></no-data>
      </div>
    </div>

    <uni-popup
      ref="popup"
      :show="isShow"
      :safe-area="false"
      @close="handleClosePopup"
    >
      <div class="popupBox">
        <div class="popupTitle">
          <image
            class="closeIcon"
            src="/static/images/home/closeIcon.svg"
            @click="handleClosePopup"
          />
          <span class="titleText">更多筛选</span>
          <div class="closeIcon"></div>
        </div>
        <div class="popupList">
          <div class="popupLeftBox">
            <div
              :class="
                leftSelectIndex === index ? 'itemType selectType' : 'itemType'
              "
              v-for="(item, index) in leftTypeList"
              :key="index"
              @click="handleSelectType(index, item.value)"
            >
              {{ item.label }}
            </div>
          </div>
          <scroll-view
            :scroll-y="true"
            :scroll-into-view="scrollTop"
            show-scrollbar="false"
            :scroll-with-animation="true"
            class="popupRightBox"
          >
            <div
              class="rightItem"
              :id="'shop' + item.value"
              v-for="(item, index) in leftTypeList"
              :key="index"
            >
              <template v-if="index !== 0">
                <div class="itemTitle">{{ item.label }}</div>
                <div
                  class="rightTypeList"
                  v-if="item.children && item.children.length > 0"
                >
                  <div
                    :class="
                      subItem.value === rightSelectValue
                        ? 'rightType selectRightType'
                        : 'rightType'
                    "
                    v-for="(subItem, subIndex) in item.children"
                    :key="subIndex"
                    @click="handleChangeRightSelect(index, subItem.value)"
                  >
                    {{ subItem.label }}
                  </div>
                </div>
              </template>
            </div>
          </scroll-view>
        </div>
        <div class="btnBox" @click="handleClickConfirm">
          <div class="btn">确定</div>
        </div>
      </div>
    </uni-popup>
  </view>
</template>
<script>
import UniPopup from "@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
import NoData from "../../components/noData.vue";
export default {
  data() {
    return {
      nowServiceObj: {}, // 当前的服务区信息
      isShow: false, // 悬浮框的是否显示
      dataList: [
        // {
        //   beforeOtherName: "罗马仕",
        //   lightName: "充电宝",
        //   afterOtherName: "",
        //   time: "2024-12-12 10:34:34",
        //   phone: "123456789",
        // },
      ], // 数据列表
      leftTypeList: [], // 悬浮框左侧的列表
      leftSelectIndex: 0, // 左侧的默认选择
      rightSelectValue: 0, // 右侧的改变
      scrollTop: "", // 滚动的锚点
      searchText: "", // 查询的内容
      showSearchBtn: false, // 点击输入框时 出现搜索按钮
    };
  },
  components: {
    UniPopup,
    NoData,
  },
  onLoad() {
    let currentService = uni.getStorageSync("currentService");
    console.log("currentService", currentService);
    this.nowServiceObj = currentService;
    this.handleGetTypeList();
    this.handleGetListData();
  },
  onShow() {
    let currentService = uni.getStorageSync("currentService");
    if (currentService.SERVERPART_ID !== this.nowServiceObj.SERVERPART_ID) {
      this.nowServiceObj = currentService;
      this.handleGetListData();
    }
  },
  methods: {
    // 跳转到地图选择服务区
    goSelectServer() {
      uni.navigateTo({ url: "/pages/newMap/index/index" });
    },
    showFilter() {
      this.isShow = true;
      this.$refs.popup.open("bottom");
    },
    handleClosePopup() {
      this.$refs.popup.close();
      this.isShow = false;
    },
    // 拿到类型枚举
    async handleGetTypeList() {
      const data = await this.$api.$get(
        "/EShangApiMain/FrameWork/GetFieldEnumTree",
        { FieldExplainField: "business_target" }
      );
      let list = this.$utils.wrapTreeNode(data.Result_Data.List);
      let res = [];
      if (list && list.length > 0) {
        list.forEach((item) => {
          if (item.value === 6000) {
            res = item.children;
          }
        });
      }

      res.unshift({ label: "全部", value: 6000 });
      console.log("handleGetTypeList", res);
      this.leftTypeList = res;
    },
    // 左侧类型的选择
    handleSelectType(index, itemValue) {
      this.leftSelectIndex = index;
      this.scrollTop = "shop" + itemValue;
    },
    // 右侧的选择事件 会改变左侧的选中效果
    handleChangeRightSelect(leftIndex, rightValue) {
      if (this.rightSelectValue === rightValue) {
        this.leftSelectIndex = 0;
        this.rightSelectValue = 0;
      } else {
        this.leftSelectIndex = leftIndex;
        this.rightSelectValue = rightValue;
      }
    },
    // 点击悬浮框里面的确定
    handleClickConfirm() {
      this.handleClosePopup();
      this.handleGetListData();
    },
    // 拿到失物招领的数据
    async handleGetListData() {
      const req = {
        SearchParameter: {
          SUGGESTION_TYPES: 4000,
          SERVERPART_IDS: this.nowServiceObj.SERVERPART_ID,
          SUGGESTION_STATES: 1,
          ENUM_LABEL: this.rightSelectValue || "",
        },
        PageIndex: 1,
        PageSize: 999999,
      };
      const data = await this.$api.$post(
        "/EShangApiMain/SiteManage/GetSUGGESTIONList",
        req
      );
      console.log("data", data);
      let list = data.Result_Data.List;
      if (list && list.length > 0) {
        list.forEach((item) => {
          if (this.searchText) {
            let obj = this.extractParts(item.SUGGESTION_TITLE, this.searchText);
            item.beforeOtherName = obj.beforeKeyword || "";
            item.lightName = obj.keyword || "";
            item.afterOtherName = obj.afterKeyword || "";
          } else {
            item.lightName = item.SUGGESTION_TITLE;
          }
        });
      }
      this.dataList = list;
    },
    // 截取关键字前后 关键字的方法
    extractParts(str, keyword) {
      const keywordIndex = str.indexOf(keyword);
      if (keywordIndex === -1) {
        return null; // 如果找不到关键字，则返回 null
      }
      // 截取关键字前的内容
      const beforeKeyword = str.substring(0, keywordIndex);
      // 截取关键字后的内容
      const afterKeyword = str.substring(keywordIndex + keyword.length);
      // 返回结果对象
      return {
        keyword: keyword,
        beforeKeyword: beforeKeyword,
        afterKeyword: afterKeyword,
      };
    },
    // 出现搜索按钮
    clickSearch() {
      this.showSearchBtn = true;
    },
    // 关闭搜索按钮
    noClickSearch() {
      this.showSearchBtn = false;
    },
    // 调起打电话
    handleCallPhone(obj) {
      uni.makePhoneCall({
        phoneNumber: obj.PHONE_NUMBER, //仅为示例
      });
    },
  },
};
</script>
<style scoped lang="less">
.main {
  width: 100%;
  height: 100vh;
  background: #fff;
  .top {
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    padding: 24rpx 32rpx 0;
    .first {
      display: flex;
      align-items: center;
      box-sizing: border-box;
      border: 1rpx solid #e7e7e6;
      border-radius: 48rpx;
      padding: 12rpx 24rpx;
      .addressIcon {
        width: 24rpx;
        height: 24rpx;
        margin-right: 8rpx;
      }
      .text {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 28rpx;
        color: #090c1a;
        line-height: 40rpx;
        text-align: justify;
        font-style: normal;
      }
      .moreIcon {
        margin-left: 8rpx;
        width: 24rpx;
        height: 24rpx;
      }
    }
  }

  .headerBox {
    box-sizing: border-box;
    padding: 24rpx 32rpx 0;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .searchBox {
      width: calc(100% - 104rpx);
      height: 80rpx;
      background: #f5f6f7;
      border-radius: 8rpx;
      box-sizing: border-box;
      padding: 16rpx 20rpx;
      display: flex;
      align-items: center;
      .searchIcon {
        width: 32rpx;
        height: 32rpx;
        margin-right: 8rpx;
      }
      .inputBox {
        width: calc(100% - 40rpx);
      }
      .searchText {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 28rpx;
        color: #9fa1aa;
        line-height: 40rpx;
        text-align: left;
        font-style: normal;
        margin-left: 8rpx;
      }
    }
    .searchBtn {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 28rpx;
      color: #130f05;
      line-height: 44rpx;
      text-align: left;
      font-style: normal;
    }

    .filterBox {
      width: 80rpx;
      height: 80rpx;
      background: #f5f6f7;
      border-radius: 8rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      .filterIcon {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }

  .pageList {
    width: 100%;
    height: calc(100vh - 200rpx);
    box-sizing: border-box;
    padding: 0 32rpx;
    margin-top: 40rpx;
    .pageTitle {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 32rpx;
      color: #130f05;
      line-height: 44rpx;
      text-align: left;
      font-style: normal;
      margin-bottom: 24rpx;
    }
    .listBox {
      width: 100%;
      height: calc(100% - 80rpx);
      overflow-y: auto;
      .shopItem {
        width: 100%;
        margin-bottom: 24rpx;
        background: #ffffff;
        border-radius: 6rpx;
        border: 2rpx solid #f5f6f7;
        box-sizing: border-box;
        padding: 24rpx;
        position: relative;
        .title {
          width: calc(100% - 72rpx);
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          .normal {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 32rpx;
            color: #130f05;
            line-height: 44rpx;
            text-align: left;
            font-style: normal;
          }
          .light {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 32rpx;
            color: #ba922f;
            line-height: 44rpx;
            text-align: left;
            font-style: normal;
          }
        }
        .timeBox {
          margin-top: 8rpx;
          display: flex;
          align-items: center;
          .timeIcon {
            width: 24rpx;
            height: 24rpx;
            margin-right: 8rpx;
          }
          .timeText {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 24rpx;
            color: #716f69;
            line-height: 36rpx;
            text-align: left;
            font-style: normal;
          }
        }
        .phoneBox {
          margin-top: 4rpx;
          display: flex;
          align-items: center;
          .phoneIcon {
            width: 24rpx;
            height: 24rpx;
            margin-right: 8rpx;
          }
          .phoneText {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 24rpx;
            color: #716f69;
            line-height: 36rpx;
            text-align: left;
            font-style: normal;
          }
        }

        .callPhoneBox {
          position: absolute;
          right: 24rpx;
          top: 24rpx;
          width: 64rpx;
          height: 64rpx;
          background: #f5f6f7;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          .callPhoneIcon {
            width: 32rpx;
            height: 32rpx;
          }
        }
      }
    }
    .listBox::-webkit-scrollbar {
      display: none;
    }
  }

  .popupBox {
    width: 100vw;
    height: 80vh;
    background: #fff;

    .popupTitle {
      width: 100%;
      box-sizing: border-box;
      padding: 32rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .closeIcon {
        width: 32rpx;
        height: 32rpx;
      }
      .titleText {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 32rpx;
        color: #130f05;
        line-height: 44rpx;
        text-align: left;
        font-style: normal;
      }
    }
    .popupList {
      width: 100%;
      height: calc(100% - 250rpx);
      display: flex;
      align-items: center;
      .popupLeftBox {
        width: 200rpx;
        height: 100%;
        background: #f5f6f7;
        overflow-y: auto;
        .itemType {
          width: 100%;
          box-sizing: border-box;
          padding: 24rpx 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          color: #130f05;
          line-height: 40rpx;
          text-align: center;
          font-style: normal;
        }
        .selectType {
          color: #ba922f;
          background: #fff;
        }
      }
      .popupRightBox {
        width: calc(100% - 200rpx);
        height: 100%;
        background: #fff;
        box-sizing: border-box;
        padding: 0 50rpx 0 32rpx;
        overflow-y: auto;
        .rightItem {
          width: 100%;
          margin-bottom: 24rpx;
          .itemTitle {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 28rpx;
            color: #130f05;
            line-height: 40rpx;
            text-align: left;
            font-style: normal;
            margin-bottom: 16rpx;
          }
          .rightTypeList {
            width: 100%;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            .rightType {
              display: block;
              width: calc((100% - 48rpx) / 3);
              height: 64rpx;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 28rpx;
              color: #716f69;
              line-height: 40rpx;
              font-style: normal;
              background: #f5f6f7;
              border-radius: 4rpx;
              display: flex;
              align-items: center;
              justify-content: center;
              margin-bottom: 16rpx;
              margin-right: 16rpx;
            }
            .selectRightType {
              color: #ffffff;
              background: #ba922f;
            }
          }
        }
      }
      .popupLeftBox::-webkit-scrollbar {
        display: none;
      }
      .popupRightBox::-webkit-scrollbar {
        display: none;
      }
    }

    .btnBox {
      width: 100%;
      box-sizing: border-box;
      padding: 16rpx 40rpx;
      .btn {
        width: 100%;
        background: #ba922f;
        border-radius: 6rpx;
        text-align: center;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 32rpx;
        color: #ffffff;
        line-height: 44rpx;
        text-align: center;
        font-style: normal;
        padding: 22rpx 0;
      }
    }
  }
}
</style>