<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="guardRate">
        <el-input-number v-model="form.guardRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="礼物分成" prop="giftRate">
        <el-input-number v-model="form.giftRate" :precision="2" :step="0.01" :min="0" :max="0.6" style="width: 90%"/>
      </el-form-item>
      <el-form-item label="视频分成" prop="videoRate">
        <el-input-number v-model="form.videoRate" :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/index'
import {getAnchor, updateAnchor} from "@/api/cai/anchor";

export default {
  components: {
    ImageUpload,
  },
  data () {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form:{
        id: undefined,
        usercode: undefined,
        guardRate: undefined,
        giftRate: undefined,
        videoRate: undefined,
      },
      // 表单校验
      rules: {
        guardRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        giftRate: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        videoRate: [
          { 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();
        getAnchor(id).then(response => {
          this.form = response.data;
          this.form.usercode = row.usercode
        });
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          updateAnchor({
            id: this.form.id,
            guardRate: this.form.guardRate,
            giftRate: this.form.giftRate,
            videoRate: this.form.videoRate,
          }).then(data => {
            this.$modal.msgSuccess("修改成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
