<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: 600px;"
                >
                    <el-form-item prop="userId" label="用户信息">
                        <el-input-number type="number" v-model="formData.userId" disabled></el-input-number>
                        <el-input
                            v-model="formData.nickname"
                            disabled
                            style="margin-left: 6px; width: 180px"
                        ></el-input>
                        <!-- <el-input v-model="formData.phone" disabled style="margin-left: 6px; width: 180px"></el-input> -->
                    </el-form-item>
                    <el-form-item prop="detail" label="详情">
                        <el-input type="textarea" :rows="6" v-model="formData.detail" disabled></el-input>
                    </el-form-item>
                    <el-form-item prop="pic" label="图片">
                        <!-- <multi-upload v-model="formData.pic"></multi-upload> -->
                        <el-image
                            v-for="item in formData.pic"
                            :key="item"
                            :src="item"
                            style="width: 100px; height: 100px; margin-right: 1px"
                            :preview-src-list="formData.pic"
                        />
                    </el-form-item>
                    <!-- <el-form-item prop="reply" label="是否回复">
                        <el-switch v-model="formData.reply"></el-switch>
                    </el-form-item> -->
                    <el-form-item prop="replyDetail" label="回复详情">
                        <el-input type="textarea" :rows="6" v-model="formData.replyDetail"></el-input>
                    </el-form-item>
                    <el-form-item prop="replyPic" label="回复图片">
                        <multi-upload v-model="formData.replyPic"></multi-upload>
                    </el-form-item>
                    <el-form-item prop="repliedAt" label="回复时间" v-if="formData.reply">
                        <el-date-picker
                            v-model="formData.repliedAt"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item class="form-submit">
                        <el-button @click="onSave" :loading="saving" type="primary">
                            保存
                        </el-button>
                        <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                            删除
                        </el-button>
                        <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import MultiUpload from '../components/MultiUpload.vue';
export default {
  components: { MultiUpload },
    name: 'MessageEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('message/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: {}
        };
    },
    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('/message/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(`/message/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>
/deep/.el-input.is-disabled .el-input__inner {
    color: #7c7e7e;
}
/deep/.el-radio__input.is-disabled + span.el-radio__label {
    color: #7c7e7e;
}
/deep/.el-textarea.is-disabled .el-textarea__inner {
	color: #7c7e7e;
}
</style>
