<template>
    <div class="edit-view">
        <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="title" label="表名">
                        <el-input v-model="formData.title"></el-input>
                    </el-form-item>
                    <el-form-item prop="val1" label="南昌市">
                        <el-input v-model="formData.val1"></el-input>
                    </el-form-item>
                    <el-form-item prop="val2" label="九江市">
                        <el-input v-model="formData.val2"></el-input>
                    </el-form-item>
                    <el-form-item prop="val3" label="景德镇市">
                        <el-input v-model="formData.val3"></el-input>
                    </el-form-item>
                    <el-form-item prop="val4" label="萍乡市">
                        <el-input v-model="formData.val4"></el-input>
                    </el-form-item>
                    <el-form-item prop="val5" label="新余市">
                        <el-input v-model="formData.val5"></el-input>
                    </el-form-item>
                    <el-form-item prop="val6" label="鹰潭市">
                        <el-input v-model="formData.val6"></el-input>
                    </el-form-item>
                    <el-form-item prop="val7" label="赣州市">
                        <el-input v-model="formData.val7"></el-input>
                    </el-form-item>
                    <el-form-item prop="val8" label="宜春市">
                        <el-input v-model="formData.val8"></el-input>
                    </el-form-item>
                    <el-form-item prop="val9" label="上饶市">
                        <el-input v-model="formData.val9"></el-input>
                    </el-form-item>
                    <el-form-item prop="val10" label="吉安市">
                        <el-input v-model="formData.val10"></el-input>
                    </el-form-item>
                    <el-form-item prop="val11" label="抚州市">
                        <el-input v-model="formData.val11"></el-input>
                    </el-form-item>
                    <el-form-item prop="type" label="分类">
                        <el-input v-model="formData.type"></el-input>
                    </el-form-item>
                    <el-form-item prop="yAxisIndex" label="y轴">
                        <el-input-number type="number" v-model="formData.yAxisIndex"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="chartType" label="图表类型">
                        <el-select v-model="formData.chartType" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in chartTypeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                        <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id"
                            >删除
                        </el-button>
                        <el-button @click="$router.go(-1)">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'SummaryStateEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('summaryState/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: {},
            chartTypeOptions: [
                { label: '柱状图', value: 'bar' },
                { label: '折线图', value: 'line' },
                { label: '饼状图', value: 'pie' }
            ]
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/summaryState/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.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/summaryState/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);
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
