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

<script>
export default {
  data() {
    return {
      name: null,
      areaId: null,
      areaName: null,
      detail: '',
      pid: '',
      files: [],
      id: '',
      type: '',
      operationDesc: '',
      flag: false,
      form: {detail: ""}
    }
  },
  computed: {
    isBangFu: v => v.type == 1,
    isNormalFarmer: v => v.type == 2
  },
  onLoad(query) {
    this.$dict.load('fpAssistanceMeasures')
    this.pid = query.pid
    this.type = query.type
    this.visitDate = this.$dateFormat(this.$dayjs())
    if (query.id) {
      this.id = query.id
      this.getInfo()
    }
  },
  onShow() {
    this.pageTitle()
  },
  methods: {
    pageTitle() {
      const action = this.id ? '编辑' : '添加', app = this.type == 1 ? '帮扶措施' : this.type == 2 ? '一般农户走访日志' : '走访日志'
      document.title = action + app
    },
    getInfo() {
      this.$http.post(`/app/apppreventionreturntopovertylog/queryDetailById?id=${this.id}`).then(res => {
        if (res?.data) {
          this.form = res.data
        }
      })
    },
    submit() {
      if (this.flag) return
      if (this.type == 1) {
        if (!this.form.operationDesc) {
          return this.$u.toast('请选择帮扶类型')
        }
      } else if (this.type == 2) {
        if (!this.form.areaId) {
          return this.$u.toast('请选择现住址')
        }
        if (!this.form.name) {
          return this.$u.toast('请输入走访对象')
        }
      } else {
        if (!this.form.detail) {
          return this.$u.toast('请输入走访内容')
        }
      }
      this.flag = true
      const {form, type, pid} = this
      this.$http.post('/app/apppreventionreturntopovertylog/addOrUpdate', {...form, type, pid}).then(res => {
        if (res.code == 0) {
          this.$u.toast('提交成功')
          uni.$emit('reload')
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 300)
        }
      })
    }
  }
}
</script>

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

  .help {
    padding: 32px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    background: #fff;
    margin-bottom: 16px;
  }

  .form-item {
    padding: 32px;
    background: #fff;
    margin-bottom: 16px;

    .form-item__title {
      display: flex;
      align-items: center;
      margin-bottom: 34px;

      em {
        margin-right: 4px;
        font-style: normal;
        color: #FF4466;
      }

      h2 {
        color: #333333;
        font-weight: normal;
        font-size: 32px;
      }

      i {
        color: #999999;
        font-size: 28px;
        font-style: normal;
      }
    }

    textarea {
      width: 100%;
    }
  }

  .form-type {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 112px;
    padding: 0 32px;

    h2 {
      font-weight: normal;
      color: #333333;
      font-size: 32px;
    }

    span {
      color: #999999;
      font-size: 28px;
    }
  }

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

  ::v-deep uni-textarea {
    width: 100%;
  }
}
</style>
