<template>
  <div class="address_info">
    <div class="add_rule">
      <a @click="editClick()"><i class="iconfont icon-add"></i>{{isSend ? '新增发货地址' : '新增收货地址'}}</a>
    </div>
    <div class="deleteKuang" v-if="showLogout">
      <div class="kuangContent">
        <div class="title">系统提示</div>
        <div class="titleinfo">是否删除？</div>
        <div class="K_btn">
          <div class="cancelBtn" @click="showLogout = false">取消</div>
          <div class="answerBtn" @click="goNext">确定</div>
        </div>
      </div>
    </div>
    <ul class="list-unstyled">
      <li v-for="(item, index) in addressList">
        <div class="wrapper-line clearfix">
          <div class="name pull-left">{{isSend ? '发货地址：' : '收货地址：'}}</div>
          <div class="name-text">{{item.area}}{{item.detailAddress}}</div>
        </div>
        <div class="wrapper-line clearfix">
          <div class="name pull-left">{{isSend ? '发货人：' : '收货人：'}}</div>
          <div class="name-text">{{item.name}}</div>
        </div>
        <div class="wrapper-line clearfix">
          <div class="name pull-left">电话：</div>
          <div class="name-text">{{item.tel}}</div>
        </div>
        <div class="item-bottom clearfix">
          <div class="pull-left" >
            <label class="bottom-modal-check mobile-cart-check" :class="{active: item.num === 1}">
              <input type="checkbox" @change="setActiveClick(item.id)">
              <span>默认地址</span>
            </label>
          </div>
          <div class="pull-right" >
            <span @click="editClick(item)"><i class="iconfont icon-edit"></i>修改</span>
            <span @click="deleteClick(item.id, index)"><i class="iconfont icon-lajitong"></i>删除</span>
          </div>
        </div>
      </li>
    </ul>

    <pull-up
      :searchMore="fetching"
      :allPage="allPage"
      :page="param.page"
      :fixId="'logisticsContent'"
      @pullUpAction="getPullAddress">
    </pull-up>
  </div>
</template>

<script type="text/javascript">
  import { PullUp } from '~components/mobile/common'
  export default {
    name: 'AddressInfoView',
    props: {
      isSend: {
        default: true,
        type: Boolean
      }
    },
    data() {
      return {
        addressList: [],
        showLogout: false,
        // 保存操作数据的序号
        saveId: {
          id: '',
          active: 0
        },
        param: {
          count: 10,
          page: 1,
          sorting: { 'num': 'asc' }
        },
        isChange: false
      }
    },
    watch: {
      'listInfo': {
        handler: function (val) {
          val.content = val.content.sort((a, b) => { return a.num - b.num })
          if (this.isChange) {
            this.addressList = val.content
            this.isChange = false
          } else {
            this.addressList = [...this.addressList, ...val.content]
          }
          if (this.addressList.length <= 0) {
            this.editClick()
          }
        },
        immediate: true
      }
    },
    methods: {
      // 设置默认地址
      setActiveClick (type) {
        this.$http.put(`/trade/address/settop/${type}`).then(() => {
          this.initList()
        })
      },
      // 删除事件
      deleteClick (type, index) {
        this.saveId.id = type
        this.saveId.active = index
        this.showLogout = true
      },
      goNext() {
        this.$http.put(`/trade/address/delete/${this.saveId.id}`)
          .then(() => {
            this.showLogout = false
            if (this.saveId.active === 0 && this.addressList.length > 1) {
              this.setActiveClick(this.addressList[1].id)
            } else {
              this.initList()
            }
          })
      },
      // 编辑事件
      editClick (type) {
        this.$emit('isEditEvent', type || {}, true)
      },
      // 加载更多
      getPullAddress() {
        this.param.page++
        this.reloadList()
      },
      // 初始化数据页面
      initList () {
        this.isChange = true
        this.param.page = 1
        this.reloadList()
      },
      // 发送请求数据
      reloadList () {
        this.$store.dispatch('mobileAddress/loadAddressData', {count: this.param.count, page: this.param.page, isSend: this.isSend, sorting: this.param.sorting})
      }
    },
    computed: {
      listInfo() {
        return this.baseUtils.deepCopy(this.$store.state.mobileAddress.address.data)
      },
      fetching () {
        return this.$store.state.mobileAddress.address.fetching
      },
      allPage() {
        return this.listInfo.totalPages
      }
    },
    components: {
      PullUp
    }
  }
</script>

<style lang="scss" scoped>
  @mixin overFlowHidden {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  @mixin lineHeight($value) {
    height: $value;
    line-height: $value;
  }
  .deleteKuang {
    position: fixed;
    background: rgba(0,0,0,0.5);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    .kuangContent {
      border-radius: 5px;
      background: #fff;
      width: 5rem;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate3d(-50%, -50%, 0);
      overflow: hidden;
      .titleinfo {
        font-size: .3rem;
        color: #666;
        text-align: center;
        margin-top: 0.5rem;
        margin-bottom: 0.1rem;
      }
      .title {
        background: #5078cb;
        height: .7rem;
        line-height: .7rem;
        font-size: .3rem;
        color: #fff;
        text-align: center;
      }
      .info {
        color: #f00;
        text-align: center;
      }
      .K_btn {
        margin-top: 0.4rem;
        line-height: 0.7rem;
        height: 0.7rem;
        &::after{
          clear: both;
          display: block;
          content: ' ';
          visibility: hidden;
          zoom: 1;
        }
        div {
          float: left;
          width: 50%;
          font-size: 0.3rem;
          text-align: center;
          &.cancelBtn {
            background: #b4b5b9;
            color: #333;
          }
          &.answerBtn {
            background: #5078cb;
            color: #fff;
          }
        }
      }
    }
  }
  .add_rule{
    position:absolute;
    bottom:.2rem;
    left:0;
    right:0;
    a{
      display:block;
      margin: 0 auto;
      width:95%;
      height:.78rem;
      line-height: .78rem;
      text-align: center;
      font-size: .32rem;
      color:#fff;
      background: #3f84f6;
      border-radius:.05rem;
      i{
        margin-right:.05rem;
      }
    }
  }
  .address_info{
    padding: 0 .2rem 1.2rem;
    ul {
      li {
        margin-top: 0.2rem;
        padding: 0.3rem 0.24rem 0;
        border: 1px solid #e4e6e9;
        border-radius: 5px;
        background: #fefefe;
        .wrapper-line {
          margin-bottom: 0.16rem;
          .name{
            display:inline-block;
            text-align: right;
            width:1.5rem;
            font-size: .28rem;
            color: #4c8cf7;
          }
          .name-text {
            margin-left:1.5rem;
            word-break: break-all;
            word-wrap: break-word;
            font-size: 0.28rem;
            color: #333;
          }
        }
        .item-bottom {
          color: #333;
          @include lineHeight(0.8rem);
          font-size: 0.26rem;
          text-align: center;
          border-top: 1px solid #d3d3d3;
          .pull-left {
            text-align: left;
          }
          .pull-right {
            width: 50%;
            text-align: right;
          }
          span{
            font-size: 0.28rem;
            margin-right:.2rem;
            i {
              font-size: 0.34rem;
              vertical-align: middle;
            }
          }

        }
      }
    }
  }
</style>
