<template>
  <div>
    <div class="shipments_address_edit" :class="{'fix-sa-edit': inFix}">
      <div class="form_line">
        <ul class="list-unstyled" ref="addressContent">
          <li class="clearfix">
            <div class="com_left pull-left"><span>*</span>收货人姓名：</div>
            <div class="form_input">
              <input type="text" placeholder="请输入您的姓名" v-model="params.name" maxlength="10">
            </div>
          </li>
          <li class="clearfix">
            <div class="com_left pull-left"><span>*</span>联系电话：</div>
            <div class="form_input">
              <input type="tel" placeholder="请输入您的联系电话" v-model="params.tel" maxlength="11">
            </div>
          </li>
          <li class="clearfix">
            <div class="com_left pull-left">邮箱：</div>
            <div class="form_input">
              <input type="email" placeholder="请输入正确邮箱，用于接收订单提醒" v-model="params.email">
            </div>
          </li>
          <li class="clearfix">
            <div class="com_left pull-left"><span>*</span>所在地区：</div>
            <div class="form_input" @click="addressShow = true">
              <span v-text="params.area || '选择地区'">选择地区</span>
              <i class="iconfont icon-xiangyou"></i>
            </div>
          </li>
          <li class="clearfix">
            <div class="com_left pull-left"><span>*</span>详细地址：</div>
            <div class="form_input">
              <input type="text" placeholder="请您填写详细地址，街道名、门牌号等" v-model="params.detailAddress" maxlength="30">
            </div>
          </li>
          <li class="clearfix">
            <div class="com_left pull-left"><span>*</span>默认地址：</div>
            <div class="form_input">
              <el-switch
                v-model="isActive"
                on-text="ON"
                off-text="OFF">
              </el-switch>
            </div>
          </li>
        </ul>
      </div>
      <div class="control clearfix">
        <div class="cancel" @click="storeInfosave('cancel')">取消</div>
        <div class="save" @click="storeInfosave()">保存</div>
      </div>
    </div>
    <select-address :className="className" :inFix="inFix" :isShow="addressShow" @closeAction="addressData"></select-address>
    <remind-box :title="collectResult" :timeoutCount="timeoutCount"></remind-box>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  import { RemindBox } from '~components/mobile/common'
  import SelectAddress from './SelectAddress.vue'
  export default {
    name: 'AddressEditView',
    props: {
      data: {
        type: Object,
        default: {}
      },
      isSend: {
        default: true,
        type: Boolean
      },
      isPersonal: {
        default: true,
        type: Boolean
      },
      inFix: {
        type: Boolean,
        default: false
      },
      className: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        params: {
          area: '',
          detailAddress: '',
          email: '',
          tel: '',
          name: '',
          province: '',
          city: '',
          district: ''
        },
        isActive: false,
        addressShow: false,
        timeoutCount: 0,
        collectResult: ''
      }
    },
    watch: {
      'data': {
        handler: function (newVal) {
          if (newVal) {
            newVal.num === 1 ? this.isActive = true : this.isActive = false
            this.params = this.baseUtils.deepCopy(newVal)
          }
        },
        immediate: true
      }
    },
    methods: {
      editClick (data) {
        this.$emit('isEditEvent', data || {}, false)
      },
      addressData (type) {
        this.addressShow = false
        if (type) {
          this.params.area = `${type.province},${type.city},${type.area}`
          this.params.province = type.province
          this.params.city = type.city
          this.params.district = type.area
        }
      },
      setRemindText (str) {
        this.collectResult = str
        this.timeoutCount++
      },
      storeInfosave(_tp) {
        this.BScroll.refresh()
        if (_tp === 'cancel') {
          this.editClick()
          return false
        }
        if (!this.params.name || this.params.name === '') {
          this.setRemindText('收货人姓名不能为空')
          return false
        }
        if (!this.params.tel || this.params.tel === '' || !/^1[3|4|5|6|7|8|9][0-9]{9}$/.test(this.params.tel)) {
          this.setRemindText('请输入正确的手机号码')
          return false
        }
        if (this.params.email && !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(this.params.email)) {
          this.setRemindText('请输入正确的邮箱')
          return false
        }
        if (!this.params.area || this.params.area === '') {
          this.setRemindText('请选择所在地区')
          return false
        }
        if (!this.params.detailAddress || this.params.detailAddress === '') {
          this.setRemindText('请填写详细地址信息')
          return false
        }
        this.$http.post(`/trade/address/shipping/save?isPersonal=${this.isPersonal}&isSetTop=${this.isActive}&send=${this.isSend}`, this.params)
          .then(res => {
            this.setRemindText('保存成功')
            this.editClick(res.data)
          }).catch(err => {
            this.collectResult = err.response.data
            this.timeoutCount++
            this.params = this.data
          })
      }
    },
    components: {
      RemindBox,
      SelectAddress
    },
    mounted() {
      this.$nextTick(() => {
        if (this.BScroll) {
          this.BScroll.refresh()
        } else {
          this.BScroll = new BScroll(this.$refs.addressContent, {
            click: true
          })
        }
      })
    }
  }
</script>

<style lang="scss" scoped>
  .shipments_address_edit{
    .control {
      width: 90%;
      margin: 0.4rem auto 0rem;
      height: .88rem;
      line-height: 0.88rem;
      .save {
        border-radius: 3px;
        width: 48%;
        color: #fff;
        text-align: center;
        height: .88rem;
        line-height: 0.88rem;
        background: #3e82f5;
        float: right;
      }
      .cancel {
        border-radius: 3px;
        width: 48%;
        background: #acabab;
        color: #fff;
        text-align: center;
        height: .88rem;
        line-height: 0.88rem;
        float: left;
      }
    }
    .form_line{
      margin:.2rem;
      padding:.2rem;
      background: #ffffff;
      border-radius:.05rem;
      ul{
        li{
          line-height: 1.2rem;
          height:1.2rem;
          border-bottom:1px solid #d3d3d3;
          vertical-align: middle;
          &:last-child{
            border:none;
          }
          .com_left{
            width:1.85rem;
            color:#4c8cf7;
            text-align: right;
            font-size: .28rem;
            span{
              color:#ff3333;
            }
          }
          .form_input{
            position:relative;
            margin-left:1.85rem;
            padding-right:.2rem;
            span{
              line-height: .3rem;
              word-break: break-all;
              display: inline-block;
              margin-right: .2rem;
              vertical-align: middle;
            }
            i{
              position:absolute;
              top:0;
              right:0;
              color: #c1c1c6;
              font-size: .28rem;
            }
            input{
              width:100%;
              line-height: .3rem;
              height:.3rem;
              border:none;
              font-size: .28rem;
            }
          }
        }
      }
    }
  }
  .fix-sa-edit {
    .form_line {
      margin-top: 0;
    }
  }
</style>
