<template>
  <view>
    <uv-popup ref="popup" mode="bottom" bgColor="none" >
      <view class="popup__view">
        <view class="flex header">
          <view class="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>
                  <!-- <formInput v-model="form.phone"></formInput> -->
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="address" :customStyle="formItemStyle">
                <view class="form-item-label">详细地址</view>
                <view class="form-item-content">
                  <formInput v-model="form.address" 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 {
        form: {
          name: null,
          phone: null,
          area: null,
          address: null,
        },
        rules: {
          'name': {
            type: 'string',
            required: true,
            message: '请输入联系人',
          },
          'phone': {
            type: 'string',
            required: true,
            message: '请输入手机号',
          },
          'area': {
            type: 'array',
            required: true,
            message: '请选择省市区',
          },
          'address': {
            type: 'string',
            required: true,
            message: '请输入详细地址',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    methods: {
      open(data) {

        if (data) {
          const {
            name,
            phone,
            area,
            address,
          } = data

          this.form = {
            name,
            phone,
            area,
            address,
          }
        }

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

        try {
          const res = await this.$refs.form.validate()

          console.log('onSave res', res)

          // todo: save

          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%;
    // todo：check
    // height: 214rpx;
    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>