<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="names">
          <u-input v-model="forms.title" placeholder="请输入标题" maxlength="30" />
        </u-form-item>

        <u-form-item label="发布组织" prop="publishUnitName" required :border-bottom="false" class="phones">
          <u-input v-model="forms.publishUnitName" placeholder="请输入发布单位" maxlength="16" />
        </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="请输入正文" type="textarea" auto-height height="100" maxlength="500" />
        </u-form-item>

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

        <u-form-item label="图片上传 (最多9张)" prop="images" :border-bottom="false" class="avatars" label-position="top">
          <AiUploader :def.sync="forms.images" multiple placeholder="上传图片" :limit="9" action="/admin/file/add2"></AiUploader>
        </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: '',
        publishUnitName: '',
        content: '',
        images: [],
      },
      flag: false,
    }
  },
  computed: { ...mapState(['user']) },
  onLoad(o) {
    document.title =  '新增通知公告' 
    this.id = o.id
    this.getDetail()
  },

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

    submit() {
      if (this.flag) return

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          if (!this.forms.title) {
            return this.$u.toast('请输入标题')
          }
          if (!this.forms.publishUnitName) {
            return this.$u.toast('请输入发布组织')
          }

          if (!this.forms.content) {
            return this.$u.toast('请输入正文')
          }

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

          this.flag = true
          this.$http
            .post(`/app/appmininotice/addOrUpdate`, {
              title: this.forms.title,
              publishUnitName: this.forms.publishUnitName,
              content: this.forms.content,
              images: JSON.stringify(imgs) || [],
              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('失败')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
}
.add {
  height: 100%;

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

      .names,
      .modeTypes,
      .phones {
        .u-form-item__body {
          border-bottom: 1px solid #dddddd;
        }
      }

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

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

      .line {
        height: 24px;
        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>
