<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="调整账户" prop="rechargeType">
        <el-radio-group v-model="form.rechargeType">
          <el-radio :label="1">余额调整</el-radio>
          <el-radio :label="2">收益调整</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否执行分销" prop="distribution">
        <el-radio-group v-model="form.distribution">
          <el-radio :label="false">不执行</el-radio>
          <el-radio :label="true">执行</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="调整金额" prop="rechargeCoin">
        <el-input v-model="form.rechargeCoin" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入" />
      </el-form-item>
      <el-form-item :label="systemName+'号'" prop="usercode">
        <el-autocomplete
            class="inline-input"
            v-model="form.usercode"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="昵称" v-if="info.nickname">
        {{ info.nickname }} 【{{ info.usercode }}】
      </el-form-item>
      <el-form-item label="头像" v-if="info.avatar">
        <image-avatar :src="info.avatar"/>
      </el-form-item>
      <el-form-item>
        注意：调整金额为负减余额，正则加余额， 请保证余额不能减为负数
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getUserByUsercode, listUserByUserCode} from "@/api/cai/user";
import { listMemberPrice } from '@/api/cai/memberPrice'
import { addUserMember } from '@/api/cai/userMember'
import {updateAccountCoin} from "@/api/cai/account";
import {listGoods} from "@/api/cai/goods";

export default {
  components: {
  },
  data () {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form:{
        usercode: undefined,
        rechargeType: undefined,
        rechargeCoin: undefined,
        remark: undefined,
        distribution: false
      },
      info:{
      },
      // 表单校验
      rules: {
        distribution: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        usercode: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        rechargeType: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        rechargeCoin: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init () {
      this.open = true;
      this.title = "调整用户账号"
      this.info = {};
      this.form.usercode = undefined
      this.form.rechargeCoin = undefined
      this.form.remark = undefined
    },
    querySearch(querySearch,cb){
      listUserByUserCode(querySearch).then(res => {
        cb(res.data.map((terminal) => {
          return {
            value: terminal,
            name: terminal,
          };
        }))
      })
    },
    handleSelect(item){
      getUserByUsercode(item.value).then(res => {
        this.info = res.data
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          updateAccountCoin(this.form).then(data => {
            this.$modal.msgSuccess("操作成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
