<template>
    <el-dialog
        v-loading="saving"
        @close="closeDialog"
        title="设置复审信息"
        center
        :visible.sync="dialogVisible"
        width="600px"
    >
        <div class="edit-view">
            <el-form
                :model="formData"
                :rules="rules"
                ref="form"
                label-width="80px"
                label-position="right"
                size="small"
                style="max-width: 500px;"
            >
                <el-form-item prop="reviewOnline" label="复审形式">
                    <el-radio-group v-model="formData.reviewOnline" class="input1">
                        <el-radio :label="true">线上</el-radio>
                        <el-radio :label="false">线下</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="reviewRatio" label="复审比例">
                    <el-input-number
                        type="number"
                        v-model="formData.reviewRatio"
                        class="input1"
                        @change="count"
                    ></el-input-number>
                    <br />
                    <span>将有{{ pass }}个节目进入复审，{{ total - pass }}个节目被淘汰!</span>
                </el-form-item>
                <el-form-item>
                    <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                    <el-button @click="publish" :loading="saving" type="success">发布</el-button>
                    <el-button @click="closeDialog">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-dialog>
</template>
<script>
export default {
    props: ['dialogVisible', 'total'],
    data() {
        return {
            saving: false,
            showMore: false,
            formData: {},
            rules: {
                reviewOnline: [
                    {
                        required: true,
                        message: '请选择复审形式',
                        trigger: 'blur'
                    }
                ],
                reviewRatio: [
                    {
                        required: true,
                        message: '请输入复审比例',
                        trigger: 'blur'
                    }
                ]
            },
            id: '',
            pass: 0
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        closeDialog() {
            this.$emit('close');
        },
        init(id) {
            this.id = id;
            if (id) {
                this.$http
                    .get('performance/get/' + id)
                    .then(res => {
                        this.formData = res;
                        this.count(res.reviewRatio);
                    })
                    .catch(e => {
                        console.log(e);
                        this.$message.error(e.error);
                    });
            }
        },
        submit() {
            let data = { ...this.formData };
            data.id = this.id;
            this.saving = true;
            this.$http
                .post('/performance/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    // this.getData();
                    this.$forceUpdate();
                    this.closeDialog();
                    // this.dialogVisible = false;
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        publish() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.$confirm('发布后将不能在修改, 确认发布吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.formData.auditTimes += 1;
                        this.formData.status = 'UNDER_REVIEW';

                        this.$http
                            .post('/programme/review', { performanceId: this.id, pass: this.pass })
                            .then(res => {
                                this.submit();
                            })
                            .catch(e => {
                                console.log(e);
                                this.$message.error(e.error);
                            });
                    });
                } else {
                    return false;
                }
            });
        },
        count(val) {
            let pass = (this.total * val) / 100;
            this.pass = Math.round(pass);
        }
    }
};
</script>
<style lang="less" scoped>
.width {
    width: 350px;
}
/deep/.el-input.is-disabled .el-input__inner {
    background-color: #ffffff;
    color: #606266;
}
</style>
