<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="type" required :border-bottom="false" right-icon="arrow-right">
          <u-input v-model="forms.type" disabled placeholder="请选择类型" @click="showStstus = true" />

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

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

        <u-form-item label="事主姓名" prop="name" required :border-bottom="false" class="names">
          <u-input v-model="forms.name" placeholder="请输入事主姓名" maxlength="30" />
        </u-form-item>

        <u-form-item label="方式" prop="modeType" required :border-bottom="false" class="modeTypes" right-icon="arrow-right">
          <u-input v-model="forms.modeType" disabled placeholder="请选择类型" @click="showModeType = true" />

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

        <u-form-item label="联系电话" prop="phone" required :border-bottom="false" class="phones">
          <u-input v-model="forms.phone" placeholder="请输入联系电话" maxlength="16" />
        </u-form-item>

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

        <u-form-item label="地址" prop="address" :border-bottom="false" class="addresss">
          <u-input v-model="forms.address" placeholder="请输入地址" />
        </u-form-item>

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

        <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="files" :border-bottom="false" class="avatars" label-position="top">
          <AiUploader :def.sync="forms.files" 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: {
        type: '',
        typeValue: '',
        name: '',
        modeType: '',

        modeTypeValue: '',
        phone: '',
        address: '',
        content: '',

        personType: 1,
        files: [],
      },
      showStstus: false,
      showModeType: false,
      flag: false,
    }
  },
  computed: { ...mapState(['user']) },
  onLoad() {
    this.$dict.load('marriageType', 'modeType')
  },
  onShow() {
    document.title = '婚丧嫁娶'
  },

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

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          if (!this.forms.type) {
            return this.$u.toast('请选择类型')
          }
          if (!this.forms.name) {
            return this.$u.toast('请输入事主姓名')
          }
          if (!this.forms.phone) {
            return this.$u.toast('请输入联系电话')
          }
          if (!this.forms.content) {
            return this.$u.toast('请输入活动详情')
          }

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

          this.flag = true
          this.$http
            .post(`/app/appmarriagefuneralinfo/addOrUpdate`, {
              type: this.forms.typeValue,
              name: this.forms.name,
              modeType: this.forms.modeTypeValue,
              phone: this.forms.phone,

              address: this.forms.address,
              content: this.forms.content,
              createUserId: this.user.id,
              personType: this.forms.personType,

              files: imgs || [],
            })
            .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) {
      if (this.showStstus == true) {
        this.forms.type = e[0].label
        this.forms.typeValue = e[0].value
      }
      if (this.showModeType == true) {
        this.forms.modeType = e[0].label
        this.forms.modeTypeValue = e[0].value
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.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;
      }

      .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,
      .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>
