<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                删除
            </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="80px"
                    label-position="right"
                    size="small"
                    style="max-width: 500px;"
                >
                    <el-form-item prop="name" label="说明">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="pattern" label="正则">
                        <el-input v-model="formData.pattern"></el-input>
                    </el-form-item>
                    <el-form-item prop="bonus" label="奖金">
                        <el-input-number type="number" v-model="formData.bonus"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="startTime" label="开始时间">
                        <el-date-picker
                            v-model="formData.startTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间"
                            default-time="00:00:00"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="endTime" label="结束时间">
                        <el-date-picker
                            v-model="formData.endTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间"
                            default-time="23:59:59"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item class="form-submit">
                        <el-button @click="preview" :loading="previewing" type="primary">
                            预览
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <el-dialog title="预览" :visible.sync="showPreview" top="5vh">
            <el-table :data="previewList" height="calc(85vh - 190px)">
                <el-table-column label="#" type="index" width="80"></el-table-column>
                <el-table-column label="用户ID" prop="userId" width="100"></el-table-column>
                <el-table-column label="藏品ID" prop="assetId" width="100"></el-table-column>
                <el-table-column label="藏品名称" prop="assetName" show-overflow-tooltip></el-table-column>
                <el-table-column label="销毁时间" prop="destroyTime" width="150"></el-table-column>
                <el-table-column label="销毁ID" prop="destroyId" width="100"></el-table-column>
                <el-table-column label="奖金" prop="bonus" width="100"></el-table-column>
            </el-table>
            <div slot="footer">
                <el-button type="primary" @click="onSave" :loading="saving">确认</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'BonusGiveEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('bonusGive/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入说明',
                        trigger: 'blur'
                    }
                ],
                pattern: [
                    {
                        required: true,
                        message: '请输入正则',
                        trigger: 'blur'
                    }
                ],
                bonus: [
                    {
                        required: true,
                        message: '请输入奖金',
                        trigger: 'blur'
                    }
                ],
                startTime: [
                    {
                        required: true,
                        message: '请输入开始时间',
                        trigger: 'blur'
                    }
                ],
                endTime: [
                    {
                        required: true,
                        message: '请输入结束时间',
                        trigger: 'blur'
                    }
                ]
            },
            showPreview: false,
            previewList: [],
            previewing: false
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.$confirm('确定发放奖励吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.submit();
                    });
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/bonusGive/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$confirm('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/bonusGive/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '删除失败');
                    }
                });
        },
        preview() {
            this.$refs.form.validate().then(() => {
                this.previewing = true;
                this.previewList = [];
                this.$http
                    .post('/bonusGive/preview', this.formData, { body: 'json' })
                    .then(res => {
                        this.previewing = false;
                        this.previewList = res;
                        this.showPreview = true;
                    })
                    .catch(e => {
                        this.previewing = false;
                    });
            });
        }
    }
};
</script>
<style lang="less" scoped></style>
