<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="100px">
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" placeholder="" disabled />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="" disabled />
      </el-form-item>
      <el-form-item :label="systemName+'号'" prop="usercode">
        <el-input v-model="form.usercode" placeholder="" disabled />
      </el-form-item>
      <el-form-item label="开启提成" prop="enableRate">
        <el-radio-group v-model="form.enableRate">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="视频提成" prop="videoDivide">
        <el-input v-model="form.videoDivide" />
      </el-form-item>
      <el-form-item label="礼物提成" prop="giftDivide">
        <el-input v-model="form.giftDivide" />
      </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 {getUnionUser, updateUnionUser} from "@/api/cai/unionUser";

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