<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" :focus="true" 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" /> -->
          <AiEditor v-model="forms.content" placeholder="请输入活动详情(500字以内)" :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="user.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" disabled 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" disabled 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: '',
      indexDetail: '',
      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.indexDetail = o.index
    this.id = o.id ? o.id : ''
    this.$dict.load('realityStatus').then(() => {
      this.areaId = this.user.areaId
      this.forms.areaId = this.user.areaId

      this.getDetail()
    })
    if (!this.indexDetail) {
      this.forms.contactPhone = this.user.phone
      this.forms.contactPerson = this.user.name
    }
  },
  onShow() {
    document.title = '发布活动'
  },

  mounted() {},
  methods: {
    getDetail() {
      if (this.indexDetail) {
        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,
              createUserName: this.user.name,
              createUserId: this.user.id,
              avatar: this.user.avatar,
              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) {
        var nowTime = new Date().getTime() * 1

        var beginTimes = new Date(e.year + '/' + e.month + '/' + e.day + ' ' + e.hour + ':' + e.minute).getTime() * 1

        if (nowTime > beginTimes) {
          this.forms.beginTime = ''
          return this.$u.toast('开始时间应大于当前时间')
        } else {
          this.forms.beginTime = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
        }
      }
      if (this.showEndTime == true) {
        if (!this.forms.beginTime) {
          return this.$u.toast('请先选择开始时间')
        }
        var beginTimes = new Date(this.forms.beginTime.replaceAll('-', '/')).getTime() * 1
        var endTimes = new Date(e.year + '/' + e.month + '/' + e.day + ' ' + e.hour + ':' + e.minute).getTime() * 1
        if (endTimes > beginTimes && endTimes != beginTimes) {
          this.forms.endTime = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
        } else {
          this.forms.endTime = ''
          return this.$u.toast('结束时间应大于开始时间')
        }
      }
    },

    areaSelect(e) {
      this.forms.areaId = e
    },
  },
}
</script>

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

  .aiArea {
    display: flex;
    align-items: center;

    .label {
      max-width: 480px;
      height: 112px;
      line-height: 112px;
      overflow: hidden;
      text-overflow:ellipsis;
      white-space: nowrap;
      color: #303133 !important;
      font-size: 30px;
    }

    i {
      margin-left: 4px;
      font-style: normal;
      color: #999999;
      font-size: 30px;
    }
  }

  .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;
    z-index: 99;
  }
}
</style>
