<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="area" :customStyle="formItemStyle">
                <view class="form-item-label">选择地址</view>
                <view class="form-item-content">
                  <view class="flex row" @click="selectAddr">
                    <view v-if="form.area" class="text">{{ form.area }}</view>
                    <view v-else class="text placeholder">请选择地址</view>
                    <uv-icon name="arrow-right" color="#C6C6C6" size="32rpx"></uv-icon>
                  </view>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="images" :customStyle="formItemStyle">
                <view class="form-item-label">上传图片</view>
                <view class="form-item-content">
                  <formUpload v-model="form.images"></formUpload>
                </view>
              </uv-form-item>
            </view>
          </uv-form>
        </view>
        <view class="footer">
          <button class="flex btn" @click="onPublish">发布</button>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
	import Position from '@/utils/position.js'

  import reloateProjectPopup from '@/pages_order/components/reloateProjectPopup.vue'
  import formTextarea from '@/pages_order/components/formTextarea.vue'
  import formUpload from '@/pages_order/components/formUpload.vue'

  export default {
    components: {
      reloateProjectPopup,
      formTextarea,
      formUpload,
    },
    data() {
      return {
        imageId: null,
        activityId: null,
        form: {
          area: null,
          latitude: null,
          longitude: null,
          images: [],
        },
        rules: {
          'area': {
            type: 'string',
            required: true,
            message: '请选择地址',
          },
          'images': {
            type: 'array',
            required: true,
            message: '请上传图片',
          },
        },
        projects: [],
        questions: [],
      }
    },
    computed: {
    },
    methods: {
      async open(imageId, activityId) {

        this.imageId = imageId
        this.activityId = activityId

        this.form = {
          area: null,
          latitude: null,
          longitude: null,
          images: [],
        }

        this.$refs.popup.open()
      },
      close() {
        this.$refs.popup.close()
      },
			//地图上选择地址
			selectAddr() {
				// Position.getLocation(res => {
				Position.selectAddress(0, 0, success => {
					this.setAddress(success)
				})
				// })
			},
			//提取用户选择的地址信息复制给表单数据
			setAddress(res) {
				//经纬度信息
				this.form.latitude = res.latitude
				this.form.longitude = res.longitude

				if (!res.address && res.name) { //用户直接选择城市的逻辑
					return this.form.area = res.name
				}
				if (res.address || res.name) {
					return this.form.area = res.address + res.name
				}
				this.form.area = '' //用户啥都没选就点击勾选
			},
      async onPublish() {
        try {
          await this.$refs.form.validate()

          const {
            area,
            images,
          } = this.form

          const params = {
            imageId: this.imageId,
            activityId: this.activityId,
            address: area,
            image: images.join(',')
          }

          await this.$fetch('addImageContent', 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 {
    max-height: 75vh;
    padding: 32rpx 40rpx;
    box-sizing: border-box;
    overflow-y: auto;

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

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

        .icon {
          margin-right: 8rpx;
          width: 16rpx;
          height: auto;
        }
      }

      &-content {
    
        .text {
          padding: 2rpx 0;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 32rpx;
          line-height: 1.4;

          &.placeholder {
            color: #C6C6C6;
          }
        }

      }
    }
  }


  .row {
    justify-content: space-between;

    .form-label {
      margin: 0;
    }
  }

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

    .btn {
      width: 100%;
      padding: 14rpx 0;
      box-sizing: border-box;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #FFFFFF;
      background-image: linear-gradient(to right, #21FEEC, #019AF9);
      border: 2rpx solid #00A9FF;
      border-radius: 41rpx;
    }
  }

</style>