<template>
  <div class="add">
    <div class="header-description">
      <u-form :model="form" ref="uForm" label-width="auto">
        <u-form-item label="主题" prop="content" required label-position="top" class="contents">
          <u-input v-model="form.content" placeholder="请输入主题(500字以内)" type="textarea" auto-height height="280" maxlength="500" />
        </u-form-item>

        <u-form-item label="图片(最多9张)" prop="images" class="avatars" label-position="top">
          <AiUploader :def.sync="form.images" multiple placeholder="上传图片" :limit="9" action="/admin/file/add2"></AiUploader>
        </u-form-item>

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

        <u-form-item label="议事截止时间" prop="discussDeadline" required :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="form.discussDeadline" disabled placeholder="请选择议事截止时间" @click="showStartTime = true" />

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

        <u-form-item label="公示截止时间" prop="publicityDeadline" :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="form.publicityDeadline" disabled placeholder="请选择公示截止时间" @click="showEndTime = true" />

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

        <u-form-item label="议事类型" prop="type" required label-position="top">
          <div v-for="op in $dict.getDict('discussType')" :key="op.dictValue" class="discussType" @click="form.type = op.dictValue" :class="{ current: form.type == op.dictValue }">
            {{ op.dictName }}
          </div>
        </u-form-item>
      </u-form>

      <div class="options" v-if="form.type == 1">
        <div class="oneopt">
          <!-- <div class="items info" v-for="(item, index) in list" :key="index">
            <img src="./img/22.png" alt="" @click="del(index)" />
            <div class="rightopts" style="color: #666">{{ item }}</div>
          </div> -->
          <div class="items" v-for="(item, index) in list" :key="index">
            <img src="./img/22.png" alt="" @click="del(index)" />
            <div class="rightopts">
              <u-input :height="40" type="textarea" v-model="item.content" placeholder="选项" maxlength="200" :clearable="false" />
            </div>
          </div>
          <div class="items" style="padding-top: 20px" @click="addOpts">
            <img src="./img/11.png" alt="" />

            <div class="addopts">添加选项</div>
          </div>
        </div>

        <div class="submitType">投票方式</div>
        <div class="summitOpt">
          <div class="leftopt" @click="opts = 0" :class="!opts ? 'current' : ''">单选</div>

          <div class="rightopt" @click="opts = 1" :class="opts ? 'current' : ''">多选</div>
        </div>

        <div class="lines"></div>

        <div class="switchs">
          <span>匿名提交</span>

          <u-switch v-model="checked"></u-switch>
        </div>
      </div>
    </div>
    <div class="btn" @click="submit">保存</div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Add',
  props: {},
  data() {
    return {
      id: '',
      form: {
        type: 0,
        areaId: '',
        areaName: '',
      },
      flag: false,
      showStartTime: false,
      showEndTime: false,
      params: {
        year: true,
        month: true,
        day: true,
        hour: true,
        minute: true,
        second: false,
        timestamp: true,
      },
      opts: 0,
      checked: false,
      list: [],
      contents: '',
      keys: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    }
  },
  computed: { ...mapState(['user']) },
  onLoad(o) {
    if (o.id) {
      this.id = o.id
      this.getDetail()
    }
    this.form.areaId = this.user.areaId
    this.$dict.load('discussType').then(() => {
      // this.areaId = this.user.areaId
    })
  },
  onShow() {
    document.title = '新增议事'
  },
  methods: {
    getDetail() {
      this.$http.post(`/app/appvillagediscuss/queryDetailById?id=${this.id}`).then((res) => {
        if (res?.data) {
          this.form = { ...res.data }
          if (res.data.images) {
            this.form.images = JSON.parse(res.data.images || '[]')
          }
        }
      })
    },

    submit() {
      let list = []
      if (this.flag) return
      if (!this.form.content) {
        return this.$u.toast('请输入主题')
      }
      if (!this.form.areaId) {
        return this.$u.toast('请输入发布地区')
      }
      if (!this.form.discussDeadline) {
        return this.$u.toast('请选择议事截止时间')
      }
      if (this.form.type === '1') {
        if (!this.list.length) {
          return this.$u.toast('请添加选项')
        }
        if (this.list.length < 2) {
          return this.$u.toast('选项不能少于2个')
        }

        list = this.list.map((v, index) => {
          return {
            content: v.content,
            item: this.keys[index]
          }
        })

        for (let i = 0; i < list.length; i ++) {
          if (!list[i].content) {
            return this.$u.toast(`请输入第${i + 1}个选项的内容`)
          }
        }
      }

      var params = {
        ...this.form,
        // voteItems: this.list,
        areaId: this.form.areaId,
        areaName: this.form.areaName,
        voteItems: list,
        anonymous: this.checked == true ? 1 : 0,
        createUserId: this.user.id,
        createUserName: this.user.name,
        images: JSON.stringify(this.form.images),
        voteType: this.opts,
      }

      this.$http.post(`/app/appvillagediscuss/addOrUpdate`, params).then((res) => {
        if (res.code == 0) {
          uni.$emit('update')
          this.$u.toast('发布成功')
          this.flag = true
          setTimeout(() => {
            uni.navigateBack({})
          }, 600)
        }
      })
    },

    addOpts() {
      // if (!this.contents) {
      //   return this.$u.toast('请输入选项内容')
      // }
      this.list.push({
        content: ''
      })
    },

    del(index) {
      this.list.splice(index, 1)
    },

    areaSelect(e) {
      this.form.areaId = e
    },

    confirmTime(e) {
      if (this.showStartTime == true) {
        var nowTime = new Date().getTime() * 1

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

        if (nowTime > discussDeadlineTimes) {
          this.form.discussDeadline = ''
          return this.$u.toast('议事截止时间应大于当前时间')
        } else {
          this.form.discussDeadline = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
        }
      }
      if (this.showEndTime == true) {
        if (!this.form.discussDeadline) {
          return this.$u.toast('请先选择议事截止时间')
        }
        var beginTimes = new Date(this.form.discussDeadline.replaceAll('-', '/')).getTime() * 1

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

        if (publicityDeadlineTimes > beginTimes && publicityDeadlineTimes != beginTimes) {
          this.form.publicityDeadline = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + '00'
        } else {
          this.form.publicityDeadline = ''
          return this.$u.toast('公示截止时间应大于议事截止时间')
        }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.add {
  padding-bottom: 200px;
  background: #f5f5f5;

  .header-description {
    ::v-deep .u-form {
      .u-form-item {
        .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: 0;
        padding-bottom: 20px !important;
      }

      .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;
                    }
                  }
                }
              }
            }
          }
        }
      }

      .avatars {
        margin: 16px 0;

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

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

    .options {
      background: #fff;
      .oneopt,
      .moreopt {
        .items {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 20px 40px 0 !important;
          width: 100%;
          // height: 120px;
          // line-height: 120px;
          box-sizing: border-box;

          ::v-deep .u-input__textarea {
            padding-bottom: 20px;
          }

          img {
            width: 36px;
            height: 36px;
            vertical-align: middle;
          }
          .rightopts {
            width: 100%;
            margin-left: 12px;
            border-bottom: 1px solid #eee;
            font-size: 28px;
            color: #cdcdcf;
          }
          .addopts {
            width: 100%;
            margin-left: 12px;
            font-size: 30px;
            color: #2270f1;
          }
        }
      }

      .submitType {
        padding: 34px 0 0 52px;
        font-size: 32px;
      }

      .summitOpt {
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
        margin: 54px 32px 38px 32px;
        .leftopt {
          // background: pink;
          width: 320px;
          height: 112px;
          line-height: 112px;
          background: #f5f5f5;
          border-radius: 4px;
          text-align: center;
        }
        .rightopt {
          // background: brown;
          width: 320px;
          height: 112px;
          line-height: 112px;
          background: #f5f5f5;
          border-radius: 4px;
          text-align: center;
        }
      }

      .lines {
        height: 16px;
        background: #f5f5f5;
      }

      .switchs {
        display: flex;
        justify-content: space-between;
        padding: 32px 56px 30px 40px;
      }
    }
  }

  .pad-b112 {
    padding-bottom: 224px;
  }

  .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: 999;
  }

  .right {
    width: 100%;
    text-align: right;

    .right-icon {
      margin-left: 8px;
    }
  }

  .discussType {
    width: 320px;
    height: 112px;
    background: #f5f5f5;
    border-radius: 4px;
    font-size: 30px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: bold;
    color: #333333;
    letter-spacing: 1px;
    text-align: center;
    line-height: 112px;

    & + .discussType {
      margin-left: 42px;
    }

    &.current {
      color: #1174fe;
      background: #e7f1fe;
      position: relative;

      &:before {
        position: absolute;
        display: block;
        content: ' ';
        bottom: 0;
        right: 0;
        border: 24px solid #1576fe;
        border-top-color: transparent;
        border-left-color: transparent;
        border-radius: inherit;
        z-index: 1;
      }

      &:after {
        position: absolute;
        display: block;
        content: '✓';
        bottom: 0;
        right: 0;
        color: #fff;
        z-index: 2;
        line-height: normal;
        font-weight: normal;
      }
    }
  }

  .color-999 {
    color: #999;
  }

  .current {
    color: #1174fe;
    background: #e7f1fe !important;
    position: relative;

    &:before {
      position: absolute;
      display: block;
      content: ' ';
      bottom: 0;
      right: 0;
      border: 24px solid #1576fe;
      border-top-color: transparent;
      border-left-color: transparent;
      border-radius: inherit;
      z-index: 1;
    }

    &:after {
      position: absolute;
      display: block;
      content: '✓';
      bottom: 0;
      right: 0;
      color: #fff;
      z-index: 2;
      line-height: normal;
      font-weight: normal;
    }
  }
}
</style>
