<template>
  <view class="page" @click="closeName">
    <view class="address">
      <view class="address-top">
        <text space="emsp" class="top_left_text"
          >跨境中转仓库地址 {{ "&emsp;" }}</text
        >
        <text class="top_right_text">请先将包裹寄往这个地址～</text>
      </view>
      <view class="address-content">
        <image
          class="address-icon"
          src="../../static/mine/346.png"
          mode=""
        ></image>
        <view class="">
          <view class="warehouse-phone">
            <text class="warehouse-name">{{ addressDetail.name }}</text>
            <text class="phone" space="emsp"
              >{{ "&emsp;" }} {{ addressDetail.mobile }}</text
            >
          </view>
          <view class="detail"> {{ addressDetail.address }} </view>
        </view>
      </view>
    </view>

    <view class="express">
      <view class="_input-1 _input-2">
        <view class="_label">快递单号</view>
        <u--input
          placeholder="请输入快递单号"
          border="none"
          v-model="expressNum"
          clearable
        ></u--input>
      </view>
      <view class="_input-1" @click.stop="openName">
        <view class="_label">快递公司</view>
        <u--input
          placeholder="请输入快递公司名称"
          border="none"
          @change="searchExpress"
          v-model="expressName"
          clearable
        ></u--input>
      </view>
      <SelectExpress
        :show="nameShow"
        :filterList="filterList"
        @selected="selected"
        className="sending-name"
      />
    </view>
    <view class="footer">
      <button class="btn-2" @click="submit">保存</button>
    </view>
  </view>
</template>

<script>
import SelectExpress from "../components/selectExpress.vue";
export default {
  components: { SelectExpress },
  data() {
    return {
      nameShow: false,
      filterList: [], //过滤之后的快递公司
      expressName: "",
      expressNum: "",
      expressCode: "", //选中的快递公司编号
      expressNameList: [], //快递公司
      orderId: "", //订单id
      addressDetail: {}, //中转地址信息
    };
  },
  onLoad(options) {
    this.orderId = options.orderId;
  },
  methods: {
    //过滤搜索结果
    searchExpress(e) {
      this.nameShow = true;
      this.filterList = this.expressNameList
        .filter((item) => item.name.includes(e))
        .map((item) => item);
    },
    openName() {
      this.nameShow = true;
    },

    //关闭
    closeName() {
      this.nameShow = false;
    },

    //获取选中的快递公司
    selected(value) {
      this.expressName = value.name;
      this.expressCode = value.code;
      this.nameShow = false;
    },

    //保存
    submit() {
      uni.$u.http
        .post(`/api/express-order/delivery/${this.orderId}`, {
          domestic_logistics_company_code: this.expressCode,
          domestic_logistics_no: this.expressNum,
        })
        .then((res) => {
          uni.navigateBack({
            delta: 1,
          });
          uni.showToast({
            title: "已保存",
            icon: "none",
          });
        });
    },
    getExpressNameList() {
      uni.$u.http.get(`/api/express-company`).then((res) => {
        this.expressNameList = res;
        this.filterList = res;
      });
      uni.$u.http.get(`/api/transit`).then((res) => {
        this.addressDetail = res;
      });
    },
  },
  mounted() {
    this.getExpressNameList();
    uni.setNavigationBarTitle({
      title: "寄快递",
    });
  },
};
</script>

<style lang="scss" scoped>
.page {
  padding: 20rpx 24rpx;
  position: relative;

  .address {
    padding: 0 20rpx;
    background-color: #fff;
    border-radius: 16rpx;

    .address-top {
      height: 96rpx;
      line-height: 96rpx;
      border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);

      .top_left_text {
        font-size: 28rpx;
        color: #222;
        font-weight: 600;
      }

      .top_right_text {
        font-size: 24rpx;
        color: #f83224;
      }
    }

    .address-content {
      display: flex;
      align-items: center;
      padding: 22rpx 0 32rpx;

      .address-icon {
        width: 28rpx;
        height: 28rpx;
        margin-right: 24rpx;
      }

      .warehouse-phone {
        display: flex;
        align-items: center;
        margin-bottom: 14rpx;

        .warehouse-name {
          font-size: 30rpx;
          font-weight: 600;
          color: #222222;
        }

        .phone {
          color: #555555;
          font-size: 24rpx;
        }
      }

      .detail {
        font-size: 26rpx;
        color: #777777;
      }
    }
  }

  .express {
    border-radius: 16rpx;
    background-color: #fff;
    padding: 0 20rpx;
    margin-top: 20rpx;
    position: relative;
    ._input-1 {
      display: flex;
      align-items: center;
      height: 100rpx;
      ._label {
        font-size: 28rpx;
        color: #222;
        margin-right: 32rpx;
      }
    }
    ._input-2 {
      border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);
    }
  }
  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 95%;
    background-color: #fff;
    height: 126rpx;
    justify-content: center;
    padding: 20rpx;
    .btn-2 {
      height: 76rpx;
      padding: 0;
      margin: 0;
      font-size: 28rpx;
      color: #fff;
      min-width: 188rpx;
      background-color: #f83224;
      border-radius: 38rpx;
      box-shadow: 0rpx 16rpx 40rpx -12rpx rgba(255, 21, 21, 0.5);
    }
  }
}
</style>
