<template>
  <view
    class="addressStyle"
    v-if="!isloading"
    :style="{ height: contentHeight + 'px' }"
  >
    <view
      style="width: 100%; height: 50px; background: #fff; padding: 9px 15px"
      @click="show = true"
    >
      <u-search
        placeholder="根据相关内容搜索"
        v-model="msg.Name"
        :show-action="false"
		@search="init"
      ></u-search>
    </view>

    <scroll-view
      :scroll-y="true"
      :enable-back-to-top="true"
      :enable-flex="true"
      :style="{ height: contentHeight - 50 + 'px' }"
    >
      <view
        v-for="(item, index) in list"
        :key="index"
        class="addresbox"
        @click="chosenHandler(item)"
      >
        <view class="addresclo">
          <Text>收件人：{{ item.Name }}</Text>
          <Text>{{ item.Mobile }}</Text>
        </view>
        <Text style="width: 100%">收货地址：{{ item.Address }}</Text>
        <view class="addresclo"> 备注：{{ item.Remarks }} </view>
        <view
          style="
            width: 100%;
            height: 2rpx;
            background: #f5f5f5;
            margin-top: 15rpx;
          "
        ></view>
        <view class="addresclo">
          <view></view>
          <view style="display: flex; align-items: center">
            <view
              style="display: flex; flex-direction: row; align-items: center"
              @click.stop="btn_edit(item)"
            >
              <u-icon name="edit" color="#c8c9cc" size="40"></u-icon>
              <Text style="color: #c8c9cc">编辑</Text>
            </view>
            <view
              style="
                display: flex;
                flex-direction: row;
                align-items: center;
                margin: 0 30rpx;
              "
              @click.stop="delAddress(item.ID)"
            >
              <u-icon name="delete" color="#c8c9cc" size="40"></u-icon>
              <Text style="color: #c8c9cc">删除</Text>
            </view>
          </view>
        </view>
      </view>
      <view class="bottom_btn">
        <view
          class="bottom_btn_item"
          :style="{ background: mainColor }"
          @click="ManualAdd"
        >
          <u-icon name="add-o" color="#fff" size="30"></u-icon>
          <Text style="color: #fff; margin-left: 10rpx">手动添加</Text>
        </view>
      </view>
    </scroll-view>
    <!-- 删除的弹框 -->
    <u-modal
      v-model="showModal"
      content="确实删除收货地址"
      :show-cancel-button="true"
      :show-title="false"
      @confirm="confirm"
    ></u-modal>
    <u-toast ref="uToast" />
    <auth
      v-if="showAuth"
      @changeuserinfo="reloadUserinfo"
      @gbAuth="gbAuth"
    ></auth>
  </view>
</template>

<script>
import auth from "../../components/auth/index.vue";

export default {
  data() {
    return {
      pageTitle: "退货地址",
      loading: true,
      list: [],
      showAuth: false,
      mainColor: "",
      u: {},
      contentHeight: 0,
      showModal: false,
      deleteID: 0,
      show: false,
      cHeight: 0,
      msg: {
        pageIndex: 1,
        pageSize: 20,
        Name: "",
      },
      list: [],
    };
  },
  components: {
    auth,
  },
  created() {
    this.contentHeight = this.$utils.calcContentHeight(-40);
    this.mainColor = this.$uiConfig.mainColor;
    this.cHeight = this.$uiConfig.is_bang ? 60 : 52;
  },
  mounted() {
    let currentPages = getCurrentPages();
    let u = "/" + currentPages[currentPages.length - 1].route;
    let pages = wx.getStorageSync("basedata")
      ? wx.getStorageSync("basedata").bar_title
      : [];
    pages.forEach((x) => {
      if (x.value == u) {
        this.pageTitle = x.new_name ? x.new_name : x.name;
      }
    });
    uni.setNavigationBarTitle({
      title: this.pageTitle,
    });
  },
  onLoad(option) {
    this.u = uni.getStorageSync("mall_UserInfo");
    if (!this.u) {
      this.u = {
        nickName: "未登录",
        avatarUrl: "",
      };
      this.showAuth = true;
    } else {
      this.init();
    }
  },
  methods: {
    chosenHandler(item) {
      console.log("chosenHandler", item);
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.$vm.formData.address_id = item.Id;
      prevPage.$vm.formdata.AddressId = item.Id;
      prevPage.$vm.adressInfo = item;
      uni.navigateBack({
        delta: 1,
      });
    },
    reloadUserinfo() {
      this.u = uni.getStorageSync("mall_UserInfo");
      // this.showAuth = false;
      this.init();
    },
    //关闭登录窗口
    gbAuth() {
      uni.navigateBack();
    },
    // 修改地址
    btn_edit(item) {
      uni.navigateTo({
        url: "/pages/MallMange/returnModifyAddress?Id=" + item.ID,
      });
    },
    // 删除地址
    delAddress(id) {
      let that = this;
      uni.showModal({
        title: "提示",
        content: "确认删除？",
        success: function (res) {
          if (res.confirm) {
            that.requestAdmin(
              "/api/MallBase/DelDelivery",
              {
                Id: id,
              },
              (res) => {
                uni.showToast({
                  title: "操作成功",
                  duration: 2000,
                });
                that.init();
              }
            );
          }
        },
      });
    },
    ManualAdd() {
      //手动添加
      uni.navigateTo({
        url: "/pages/MallMange/returnModifyAddress",
      });
    },
    init() {
      uni.showNavigationBarLoading();
      this.requestAdmin(
        "/api/MallBase/GetLogisticsDeliveryPage",
        this.msg,
        (res) => {
          uni.hideNavigationBarLoading();
          this.list = res.data.pageData;
        }
      );
    },
  },
};
</script>

<style>
.addressStyle {
  background: #f3f4f6;
}
.addressStyle .addresbox {
  width: 94%;
  margin-left: 3%;
  border-radius: 10rpx;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  padding: 0 20rpx;
  margin-top: 30rpx;
}
.addressStyle .addresclo {
  display: flex;
  flex-direction: row;
  width: 100%;
  padding: 20rpx 0;
  align-items: center;
  justify-content: space-between;
}
.addressStyle .bottom_btn {
  width: 94%;
  height: 90rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;

  margin: 30rpx 3%;
}
.addressStyle .bottom_btn .bottom_btn_item {
  width: 48%;
  height: 80rpx;
  border-radius: 40rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
.addressStyle .popupbox {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}
.addressStyle .popupbox_text {
  font-size: 12px;
  color: #111111;
  margin-left: 5%;
}
.addressStyle .popup_item {
  width: 90%;
  height: 35px;
  margin-left: 5%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 5px;
  margin-top: 10px;
  background: #f1f1f1;
}
.addressStyle .inputM {
  width: 100%;
}
.addressStyle .popup_b {
  width: 100%;
  position: absolute;
  left: 0;
  bottom: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  color: #ffffff;
}
.addressStyle .popup_b_item {
  width: 50%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: center;
  font-size: 14px;
  padding-top: 15px;
}
</style>
