<template>
  <div class="MonitorAddLog">
    <AiGroup v-if="isBangFu">
      <AiItem label="帮扶类型" required>
        <AiSelect dict="fpAssistanceMeasures" v-model="operationDesc"/>
      </AiItem>
    </AiGroup>
    <!--走访日志-->
    <AiGroup v-else>
      <AiItem label="走访日期" required>
        <AiDate v-model="createDate"/>
      </AiItem>
    </AiGroup>
    <AiGroup>
      <AiItem :label="isBangFu ? '帮扶内容' : '走访内容'" required topLabel>
        <textarea maxlength="500" v-model="detail" :placeholder="isBangFu ? '请输入帮扶内容' : '请输入走访内容'"></textarea>
        <div class="hint">{{ detail.length }}/500</div>
      </AiItem>
    </AiGroup>
    <AiGroup>
      <AiItem label="图片" topLabel>
        <span slot="sub" class="color-999" v-text="`（最多9张）`"/>
        <AiUploader :def.sync="files" multiple placeholder="上传图片" :limit="9" action="/admin/file/add2"></AiUploader>
      </AiItem>
    </AiGroup>
    <div class="btn" @click="submit">提交</div>
  </div>
</template>

<script>
export default {
  name: "MonitorAddLog",
  data() {
    return {
      detail: '',
      pid: '',
      files: [],
      id: '',
      type: '',
      operationDesc: '',
      flag: false,
      createDate: ""
    }
  },
  computed: {
    isBangFu: v => v.type == 1
  },
  onLoad(query) {
    this.$dict.load('fpAssistanceMeasures')
    this.pid = query.pid
    this.type = query.type
    if (query.id) {
      this.id = query.id
      this.getInfo()
    }else{
      this.createDate = this.$dateFormat(this.$dayjs())
    }
  },
  onShow() {
    if (this.isBangFu) {
      document.title = this.id ? '编辑帮扶措施' : '添加帮扶措施'
    } else {
      document.title = this.id ? '编辑走访日志' : '添加走访日志'
    }

  },
  methods: {
    getInfo() {
      this.$http.post(`/app/apppreventionreturntopovertylog/queryDetailById?id=${this.id}`).then(res => {
        if (res.code == 0) {
          this.operationDesc = res.data.operationDesc
          this.detail = res.data.detail
          this.files = res.data.files || []
        }
      })
    },
    submit() {
      if (this.flag) return
      if (this.isBangFu) {
        if (!this.operationDesc) {
          return this.$u.toast('请选择帮扶类型')
        }
        if (!this.detail) {
          return this.$u.toast('请输入帮扶内容')
        }
      } else {
        if (!this.detail) {
          return this.$u.toast('请输入走访内容')
        }
      }

      this.flag = true
      this.$http.post('/app/apppreventionreturntopovertylog/addOrUpdate', {
        detail: this.detail,
        files: this.files,
        pid: this.pid,
        id: this.id,
        type: this.type,
        operationDesc: this.operationDesc
      }).then(res => {
        if (res.code === 0) {
          this.$u.toast('提交成功')
          uni.$emit('reload')
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 300)

        }
      })
    }
  }
}
</script>

<style lang="scss">
.MonitorAddLog {
  padding-bottom: 120px;

  .btn {
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 11;
    width: 100%;
    height: 112px;
    line-height: 112px;
    margin: 0;
    text-align: center;
    color: #FFFFFF;
    font-size: 32px;
    background: #3192F4;
  }

  .hint {
    text-align: right;
    color: #999;
  }
}
</style>
