<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="systemName+'号'" prop="usercode">
        <el-input v-model="form.usercode"  disabled />
      </el-form-item>
      <el-form-item label="好友守护分成" prop="guardIncomeRate">
        <el-input-number v-model="form.guardIncomeRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="好友礼物分成" prop="giftIncomeRate">
        <el-input-number v-model="form.giftIncomeRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="好友视频分成" prop="videoIncomeRate">
        <el-input-number v-model="form.videoIncomeRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="好友充值分成" prop="payIncomeRate">
        <el-input-number v-model="form.payIncomeRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="好友积分充值分成" prop="pointRate">
        <el-input-number v-model="form.pointRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </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 ImageUpload from '@/components/ImageUpload'
import {getUserInfo, updateUserInfo} from "@/api/cai/userInfo";

export default {
  components: {
    ImageUpload,
  },
  data () {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form:{
        id: undefined,
        usercode: undefined,
        guardIncomeRate: undefined,
        giftIncomeRate: undefined,
        videoIncomeRate: undefined,
        payIncomeRate: undefined,
        pointRate: undefined,
      },
      // 表单校验
      rules: {
        pointRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        guardIncomeRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        giftIncomeRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        videoIncomeRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        payIncomeRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (row) {
      let id = row.id
      this.form.id = id || undefined;
      this.title = "修改用户好友提成";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        getUserInfo(id).then(response => {
          this.form = response.data;
          this.form.usercode = row.usercode
        });
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          updateUserInfo(this.form).then(data => {
            this.$modal.msgSuccess("修改成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
