<template>
  <view>
    <uv-popup ref="popup" mode="bottom" bgColor="none" >
      <view class="popup__view">
        <view class="flex header">
          <view class="title">{{ title }}</view>
          <button class="btn" @click="close">关闭</button>
        </view>
        <view class="form">
          <uv-form 
            ref="form"
            :model="form"
            :rules="rules"
            errorType="toast"
          >
            <view class="form-item">
              <uv-form-item prop="name" :customStyle="formItemStyle">
                <view class="form-item-label">联系人</view>
                <view class="form-item-content">
                  <formInput v-model="form.name"></formInput>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="phone" :customStyle="formItemStyle">
                <view class="form-item-label">手机号</view>
                <view class="form-item-content">
                  <formInput v-model="form.phone"></formInput>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="phone" :customStyle="formItemStyle">
                <view class="form-item-label">所在地区</view>
                <view class="form-item-content">
                  <picker mode="region" @change="onAreaChange" :value="form.area">
                    <view class="flex region">
                      <view v-if="form.area">{{ form.area.join('') }}</view>
                      <view v-else class="placeholder">选择省市区街道</view>
                    </view>
                  </picker>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="detail" :customStyle="formItemStyle">
                <view class="form-item-label">详细地址</view>
                <view class="form-item-content">
                  <formInput v-model="form.detail" placeholder="小区楼栋、门牌号、村等"></formInput>
                </view>
              </uv-form-item>
            </view>
          </uv-form>
        </view>
        <view class="footer">
          <button class="flex btn" @click="onSave">保存</button>
        </view>
      </view>
		</uv-popup>
  </view>
</template>

<script>
  import formInput from '@/pages_order/components/formInput.vue'

  export default {
    components: {
      formInput,
    },
    data() {
      return {
        id: null,
        title: null,
        form: {
          name: null,
          phone: null,
          area: null,
          detail: null,
        },
        rules: {
          'name': {
            type: 'string',
            required: true,
            message: '请输入联系人',
          },
          'phone': {
            type: 'string',
            required: true,
            message: '请输入手机号',
          },
          'area': {
            type: 'array',
            required: true,
            message: '请选择省市区',
          },
          'detail': {
            type: 'string',
            required: true,
            message: '请输入详细地址',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    methods: {
      async fetchAddressDetail(id) {
        try {
          const result = await this.$fetch('getAddressById', { id })
          
          const {
            name,
            phone,
            province,
            city,
            district,
            detail,
          } = result

          this.form = {
            name,
            phone,
            area: [province, city, district].filter(val => val),
            detail,
          }

        } catch (err) {

        }
      },
      open(id) {

        if (id) {
          this.id = id
          this.title = '编辑地址'
          this.fetchAddressDetail(id)
        } else {
          this.id = null
          this.title = '新建地址'
          this.form = {
            name: null,
            phone: null,
            area: null,
            detail: null,
          }
        }

        this.$refs.popup.open()
      },
      close() {
        this.$refs.popup.close()
      },
      onAreaChange(e) {
        this.form.area = e.detail.value
      },
      async onSave() {

        try {
          await this.$refs.form.validate()

          const {
            name,
            phone,
            area,
            detail,
          } = this.form

          const [province, city, district] = area

          const params = {
            name,
            phone,
            province,
            city,
            district,
            detail,
          }

          if (this.id) {
            params.id = this.id

            await this.$fetch('updateAddress', params)
            
            uni.showToast({
              icon: 'success',
              title: '修改地址成功',
            });

          } else {

            await this.$fetch('addAddress', params)
            
            uni.showToast({
              icon: 'success',
              title: '新建地址成功',
            });

          }

          this.$emit('submitted')

          this.close()

        } catch (err) {
          console.log('onSave err', err)
        }

      },
    },
  }
</script>

<style lang="scss" scoped>

  .popup__view {
    width: 100vw;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background: #FFFFFF;
    border-top-left-radius: 32rpx;
    border-top-right-radius: 32rpx;
  }

  .header {
    position: relative;
    width: 100%;
    padding: 24rpx 0;
    box-sizing: border-box;
    border-bottom: 2rpx solid #EEEEEE;

    .title {
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 34rpx;
      line-height: 1.4;
      color: #181818;
    }

    .btn {
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 32rpx;
      line-height: 1.4;
      color: #8B8B8B;
      position: absolute;
      top: 26rpx;
      left: 40rpx;
    }

  }
  
  .form {
    padding: 32rpx 40rpx;

    &-item {
      padding: 8rpx 0 6rpx 0;
      
      & + & {
        padding-top: 24rpx;
        border-top: 2rpx solid #EEEEEE;
      }

      &-label {
        margin-bottom: 14rpx;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 26rpx;
        line-height: 1.4;
        color: #181818;
      }

      &-content {
        .placeholder {
          color: #C6C6C6; 
          font-size: 32rpx; 
          font-weight: 400;
        }

        .region {
          min-height: 44rpx;
          justify-content: flex-start;
        }
      }
    }
  }

  .footer {
    width: 100%;
    padding: 32rpx 40rpx;
    box-sizing: border-box;
    border-top: 2rpx solid #F1F1F1;

    .btn {
      width: 100%;
      padding: 16rpx 0;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #FFFFFF;
      background-image: linear-gradient(to right, #4B348F, #845CFA);
      border-radius: 41rpx;
    }
  }

</style>