<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="activityId" :customStyle="formItemStyle">
                <view class="form-item-label">
                  <image class="icon" src="@/pages_order/static/icon-require.png" mode="widthFix"></image>
                  关联项目
                </view>
                <view class="form-item-content">
                  <view class="flex row" @click="openRelatePojectPicker">
                    <view v-if="form.activityId" class="text">{{ projectDesc }}</view>
                    <view v-else class="text placeholder">请选择关联项目</view>
                    <uv-icon name="arrow-right" color="#C6C6C6" size="32rpx"></uv-icon>
                  </view>
                  <reloateProjectPopup ref="reloateProjectPopup" :options="projects" @confirm="onRelateProjectChange"></reloateProjectPopup>
                </view>
              </uv-form-item>
            </view>
            <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 {
        form: {
          activityId: null,
          area: null,
          latitude: null,
          longitude: null,
          images: [],
        },
        rules: {
          'activityId': {
            type: 'string',
            required: true,
            message: '请选择关联项目',
          },
          'area': {
            type: 'string',
            required: true,
            message: '请选择地址',
          },
          'images': {
            type: 'array',
            required: true,
            message: '请上传图片',
          },
        },
        projects: [],
        questions: [],
      }
    },
    computed: {
      projectDesc() {
        const { activityId } = this.form
        const target = this.projects?.find?.(item => item.id === activityId)

        return target?.name || ''
      },
    },
    methods: {
      getData() {
        // todo
        this.projects = [
          {
            id: '001',
            name: '亲子•坝上双草原6日 |乌兰布统+锡林郭勒＋长城',
          },
          {
            id: '002',
            name: '青青草原•云中岭 |5-10公里AB线强度可选',
          },
          {
            id: '003',
            name: '新疆天山行7/9日丨醉美伊犁&吐鲁番双套餐',
          },
          {
            id: '004',
            name: '九色甘南|人间净土6日/7日深度游',
          },
          {
            id: '005',
            name: '北疆全景12日| 入疆首推！阿勒泰＋伊犁＋吐鲁番',
          },
          {
            id: '006',
            name: '塞上江南•神奇宁夏5日|穿越大漠与历史对话',
          },
          {
            id: '007',
            name: '尊享•天山环线9日| 伊犁全景＋独库，头等舱大巴',
          },
        ]

        this.questions = [
          {
            id: '001',
            label: '这次研学之旅，整体给你留下了怎样的印象？用几个词或几句话简单概括一下',
          },
          {
            id: '002',
            label: '在整个行程中，你最喜欢的部分是哪里？为什么？',
          },
          {
            id: '003',
            label: '你觉得这次研学的行程安排是否合理？有没有哪些地方让你觉得特别满意或需要改进的？',
          },
        ]
      },
      async open(id) {
        // todo: auto bind activityId by id?
        await this.getData()

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

        this.$refs.popup.open()
      },
      close() {
        this.$refs.popup.close()
      },
      openRelatePojectPicker() {
        this.$refs.reloateProjectPopup.open(this.form.activityId?.id || null)
      },
      onRelateProjectChange(id) {
        this.form.activityId = id
      },
			//地图上选择地址
			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 {
            activityId,
            area,
            images,
          } = this.form

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

          await this.$fetch('addImage', 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>