<template>
  <div class="add">
    <div class="header-description">
      <u-form :model="forms" ref="uForm" label-width="auto" :border-bottom="false">
        <u-form-item label="标题" prop="title" required :border-bottom="false" class="titles" label-position="top">
          <u-input v-model="forms.title" placeholder="请输入标题(30字以内)" type="textarea" auto-height height="60" maxlength="30" />
        </u-form-item>

        <u-form-item label="活动详情" prop="content" required :border-bottom="false" label-position="top" class="contents">
          <u-input v-model="forms.content" placeholder="请输入活动详情(500字以内)" type="textarea" auto-height height="100" maxlength="500" />
        </u-form-item>

        <div class="line"></div>

        <u-form-item label="活动封面图" prop="url" required :border-bottom="false" class="avatars" label-position="top">
          <AiUploader :def.sync="forms.url" multiple placeholder="上传图片" :limit="1" action="/admin/file/add2"></AiUploader>
        </u-form-item>

        <div class="line"></div>

        <u-form-item label="发布地区" prop="areaId" required :border-bottom="false" right-icon="arrow-right" class="addresss">
          <AiAreaPicker v-model="forms.areaId" :areaId="areaId" @select="areaSelect" style="color: #333"></AiAreaPicker>
        </u-form-item>

        <div class="line"></div>

        <u-form-item label="开始时间" prop="beginTime" required :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="forms.beginTime" placeholder="请选择开始时间" @click="showStartTime = true" />

          <u-picker mode="time" :params="params" v-model="showStartTime" @confirm="confirm"></u-picker>
        </u-form-item>

        <u-form-item label="结束时间" prop="endTime" required :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="forms.endTime" placeholder="请选择结束时间" @click="showEndTime = true" />

          <u-picker mode="time" :params="params" v-model="showEndTime" @confirm="confirm"></u-picker>
        </u-form-item>

        <u-form-item label="活动地点" prop="address" required :border-bottom="false" label-position="top" class="areaNmaes">
          <u-input v-model="forms.address" placeholder="请输入活动地点(30字以内)" type="textarea" auto-height height="70" maxlength="30" />
        </u-form-item>

        <div class="line"></div>

        <u-form-item label="联系人" prop="contactPerson" required :border-bottom="false">
          <u-input v-model="forms.contactPerson" placeholder="请输入联系人" maxlength="30" />
        </u-form-item>

        <u-form-item label="联系方式" prop="contactPhone" required :border-bottom="false">
          <u-input v-model="forms.contactPhone" placeholder="请输入联系方式" maxlength="16" />
        </u-form-item>
      </u-form>
    </div>

    <div class="btn" @click="submit">保存</div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Add',
  components: {},
  props: {},
  data() {
    return {
      id: '',
      forms: {
        title: '',
        content: '',
        url: [],
        areaId: '',

        beginTime: '',
        endTime: '',
        address: '',
        contactPerson: '',

        contactPhone: '',
      },

      showStartTime: false,
      showEndTime: false,
      flag: false,
      areaId: '',
      params: {
        year: true,
        month: true,
        day: true,
        hour: true,
        minute: true,
        second: false,
        timestamp: true,
      },
    }
  },
  computed: { ...mapState(['user']) },
  onLoad(o) {
    this.id = o.id ? o.id : ''
    this.$dict.load('realityStatus').then(() => {
      this.areaId = this.user.areaId
      this.forms.areaId = this.user.areaId

      this.user.phone = this.forms.contactPhone
      this.user.name = this.forms.contactPerson
      this.getDetail()
    })

    console.log(this.user)
  },
  onShow() {
    document.title = '发布活动'
  },

  mounted() {},
  methods: {
    getDetail() {
      this.$http.post(`/app/appvillageactivityinfo/queryDetailById?id=${this.id}`).then((res) => {
        if (res?.data) {
          this.forms = res.data
          if (res.data) {
            if (res.data.url) {
              this.forms.url = JSON.parse(res.data.url || '[]')
            }
          }
        }
      })
    },

    submit() {
      if (this.flag) return

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          if (!this.forms.title) {
            return this.$u.toast('请输入标题')
          }
          if (!this.forms.content) {
            return this.$u.toast('请输入活动详情')
          }
          if (this.forms.url.length == 0) {
            return this.$u.toast('请选择活动封面图')
          }
          if (!this.forms.beginTime) {
            return this.$u.toast('请选择开始时间')
          }
          if (!this.forms.endTime) {
            return this.$u.toast('请选择结束时间')
          }
          if (!this.forms.address) {
            return this.$u.toast('请输入活动地点')
          }
          if (!this.forms.contactPerson) {
            return this.$u.toast('请输入联系人')
          }
          if (!this.forms.contactPhone) {
            return this.$u.toast('请输入联系方式')
          }

          const imgs = []
          if (this.forms.url) {
            this.forms.url.map((e) => {
              imgs.push({ url: e.url, id: e.id })
            })
          }

          this.flag = true
          this.$http
            .post(`/app/appvillageactivityinfo/addOrUpdate`, {
              title: this.forms.title,
              content: this.forms.content,
              url: JSON.stringify(imgs) || [],
              areaId: this.forms.areaId,

              beginTime: this.forms.beginTime,
              endTime: this.forms.endTime,
              address: this.forms.address,
              contactPerson: this.forms.contactPerson,

              contactPhone: this.forms.contactPhone,
              id: this.id,
            })
            .then((res) => {
              if (res.code == 0) {
                this.flag = false
                this.$u.toast('发布成功')
                uni.$emit('updateList')
                setTimeout(() => {
                  uni.navigateBack()
                }, 600)
              }
            })
        } else {
          this.$u.toast('失败')
        }
      })
    },

    confirm(e) {
      if (this.showStartTime == true) {
        this.forms.beginTime = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
      }
      if (this.showEndTime == true) {
        this.forms.endTime = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
      }
    },

    areaSelect(e) {
      if (e.type == 5) {
        this.forms.areaId = e.id
      }
      if (e.type == 4) {
        this.forms.areaId = e.id
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.add {
  height: 100%;
  padding-bottom: 112px;

  .header-description {
    padding-bottom: 112px;
    ::v-deep .u-form {
      .u-form-item {
        padding: 0 45px !important;
        .u-form-item__body {
          .u-form-item--right__content__slot {
            padding-bottom: 0;
            .u-input {
              text-align: right !important;
            }
          }
        }
      }

      .u-form-item:last-child {
        margin-bottom: 40px;
      }

      .addresss {
        .u-form-item__body {
          .u-form-item--right {
            .u-form-item--right__content {
              .u-form-item--right__content__slot {
                .AiAreaPicker {
                  width: 100%;
                  display: flex;
                  justify-content: flex-end;
                  .areaSelector {
                    .location {
                      opacity: 0;
                    }
                  }
                }
              }
            }
          }
        }
      }

      .titles,
      .contents,
      .areaNmaes {
        .u-form-item__body {
          .u-form-item--right__content__slot {
            .u-input {
              text-align: left !important;
            }
          }
        }
      }

      .avatars,
      .areaNmaes,
      .contents {
        padding-bottom: 20px !important;
      }

      .avatars {
        .u-form-item__body {
          .default {
            width: 160px;
            height: 160px;
          }
        }
      }

      .line {
        height: 16px;
        background: #f3f6f9;
      }
    }
  }

  .btn {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 112px;
    line-height: 112px;
    background: #1365dd;
    text-align: center;
    font-size: 32px;
    font-weight: 500;
    color: #ffffff;
  }
}
</style>
