<template>
  <section class="giveIntegral">
    <AiGroup>
      <AiItem label="选择对象" required>
        <AiSelect v-model="form.objectType" dict="integralGiveObjType"/>
      </AiItem>
      <AiItem label="选择人员" required v-if="form.objectType">
        <AiPagePicker v-if="form.objectType==0" single @select="handleSelectUser">
          <AiMore v-model="form.userName"/>
        </AiPagePicker>
        <AiPagePicker v-if="form.objectType==1" single @select="handleSelectUser" type="sysUser">
          <AiMore v-model="form.userName"/>
        </AiPagePicker>
      </AiItem>
    </AiGroup>
    <AiGroup>
      <AiItem label="赠送分值" required topLabel>
        <div slot="sub" class="color-999">(积分总额{{ info.sysUserIntegral || 0 }}分)</div>
        <input v-model.number="form.integral"/>
      </AiItem>
    </AiGroup>
    <AiBottomBtn text="确认赠送" @click="submit"/>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "giveIntegral",
  appName: "积分赠送",
  computed: {
    ...mapState(['user'])
  },
  data() {
    return {
      info: {},
      form: {userName: ""}
    }
  },
  methods: {
    getInfo() {
      let {user: {id: userId}} = this
      this.$http.post('/app/appvillagerintegraldetail/sysUserIntegralList', null, {
        params: {userId, current: 1, size: 10}
      }).then(res => {
        if (res?.data) {
          this.info = res.data
        }
      })
    },
    submit() {
      let {objectType, userId, integral} = this.form
      if (!objectType) {
        return this.$u.toast("请选择对象")
      }
      if (!userId) {
        return this.$u.toast("请选择人员")
      }
      if (isNaN(integral)) {
        return this.$u.toast("请输入赠送分值")
      }
      if(!/^\d*[.\d]\d?$/.test(integral)){
        return this.$u.toast("请输入正数,最多保留一位小数")
      }
      this.$http.post("/admin/user/giveIntegral", null, {
        params: {...this.form}
      }).then(res => {
        if (res?.code == 0) {
          this.$u.toast("提交成功!")
          uni.navigateBack({
            success: () => uni.$emit('reachBottom', true)
          })
        }
      })
    },
    handleSelectUser(v) {
      console.log(v)
      this.form.userId = v?.[0]?.id || ""
      this.form.userName = v?.[0]?.name || ""
    }
  },
  created() {
    this.getInfo()
    this.$dict.load("integralGiveObjType")
  }
}
</script>

<style lang="scss" scoped>
.giveIntegral {
  width: 100vw;
  min-height: 100vh;

  .color-999 {
    color: #999;
  }
}
</style>
