<template>
  <div class="add">
    <div class="u-forms">
      <u-form class="u-form" :model="forms" ref="uForm" label-width="auto" :border-bottom="false">
        <u-form-item label="申请人" prop="realName" required :border-bottom="false">
          <u-input v-model="user.realName" disabled maxlength="30" />
        </u-form-item>

        <u-form-item label="申请时间" prop="auditTime" required :border-bottom="false">
          <u-input v-model="toDay" disabled placeholder="请选择申请时间" />
        </u-form-item>

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

        <u-form-item label="积分类型" prop="applyIntegralType" required :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="forms.applyIntegralType" disabled placeholder="请选择积分类型" @click="showStstus = true" />

          <u-select v-model="showStstus" :list="$dict.getDict('atWillReportType')" value-name="dictValue" label-name="dictName" @confirm="selectStatus"></u-select>
        </u-form-item>

        <u-form-item label="申请描述" prop="description" required :border-bottom="false" label-position="top" class="contents">
          <u-input v-model="forms.description" placeholder="请输入描述信息" type="textarea" :auto-height="true" maxlength="500" />
        </u-form-item>
        <div class="wordLength">{{ forms.description.length }}/500</div>

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

        <u-form-item label="图片上传 (最多9张)" prop="applyFiles" :border-bottom="false" class="avatars" label-position="top">
          <AiUploader v-model="forms.applyFiles" multiple placeholder="上传图片" :limit="9" action="/admin/file/add2"></AiUploader>
        </u-form-item>

        <div class="line"></div>
      </u-form>
    </div>

    <div class="btn" @click="submit">提交</div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'add',
  components: {},
  props: {},
  data() {
    return {
      forms: {
        name: '',
        auditTime: '',
        applyIntegralType: '',
        applyIntegralTypeValue: '',
        description: '',
        applyFiles: [],
      },
      showStstus: false,
      flag: false,
      toDay: '',
    }
  },
  computed: { ...mapState(['user']) },
  onLoad() {
    this.$dict.load('atWillReportType').then(() => {
      var toDay = new Date()
      var year = toDay.getFullYear()
      var month = toDay.getMonth() + 1 < 10 ? '0' + (toDay.getMonth() + 1) : toDay.getMonth() + 1
      var data = toDay.getDate() < 10 ? '0' + toDay.getDate() : toDay.getDate()
      this.toDay = year + '-' + month + '-' + data
    })
  },
  onShow() {},

  mounted() {},
  methods: {
    submit() {
      if (this.flag) return

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          if (!this.forms.name) {
            return this.$u.toast('请输入申请人')
          }

          if (!this.forms.applyIntegralType) {
            return this.$u.toast('请选择积分类型')
          }
          if (!this.forms.description) {
            return this.$u.toast('请输入描述信息')
          }

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

          this.flag = true
          this.$instance
            .post(`/app/appvillagerintegraldeclare/addOrUpdate`, {
              name: this.user.realName,
              applyIntegralType: this.forms.applyIntegralTypeValue,
              description: this.forms.description,
              applyFiles: imgs || [],
              residentId: this.user.residentId,
            })
            .then((res) => {
              if (res.code == 0) {
                this.flag = false
                this.$u.toast('提交成功')
                uni.$emit('updateList')
                setTimeout(() => {
                  uni.navigateBack()
                }, 600)
              }
            })
            .catch(() => {
              this.$u.toast('提交失败')
            })
            .finally(() => {
              this.flag = false
            })
        }
      })
    },

    selectStatus(e) {
      this.forms.applyIntegralType = e[0].label
      this.forms.applyIntegralTypeValue = e[0].value
    },

    changeCalendar(e) {
      this.forms.auditTime = e.result
    },
  },
}
</script>

<style lang="scss" scoped>
.add {
  height: 100%;

  .u-forms {
    padding-bottom: 112px;
    ::v-deep .u-form {
      background: #fff;
      .u-form-item {
        padding: 0;
        .u-form-item__body {
          padding: 40px 0 !important;
          margin: 0 45px;
          box-shadow: inset 0px -1px 0px 0px #dddddd;
          .u-form-item--right__content__slot {
            padding-bottom: 0;
            .u-input {
              text-align: right !important;
            }
          }
        }
      }

      .contents {
        padding-bottom: 20px !important;
        .u-form-item__body {
          box-shadow: none;
          .u-form-item--right__content__slot {
            .u-input {
              text-align: left !important;
            }
          }
          .default {
            width: 160px;
            height: 160px;
          }
        }
      }

      .avatars {
        ::v-deep .ai-uploader {
          .imgs {
            .img {
              width: 100px;
              height: 100px;
              .image {
                width: 100px;
                height: 100px;
              }
            }
          }
        }
      }

      .u-form-item:last-child {
        // margin-bottom: 40px;
        // padding: 40px 0 !important;
      }

      .line {
        height: 24px;
        background: #f3f6f9;
      }
    }
  }
  .wordLength {
    color: #999;
    text-align: right;
    padding: 10px 40px;
    background-color: #ffffff;
  }

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