<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="110px"
                    label-position="right"
                    size="small"
                    style="max-width: 750px"
                >
                    <el-form-item prop="reason" label="拒绝理由" v-if="formData.reason && formData.status != 'SUCCESS'">
                        <el-input
                            type="textarea"
                            v-model="formData.reason"
                            :rows="3"
                            style="width: 500px"
                            disabled
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="sort" label="排序">
                        <el-input-number v-model="formData.sort" :min="0"></el-input-number>
                        <div class="tip">数字越大排序越靠前，相同数值按创建时间倒序排列</div>
                    </el-form-item>
                    <el-form-item prop="maxCollection" label="最多藏品数量">
                        <el-input-number
                            type="number"
                            v-model="formData.maxCollection"
                        ></el-input-number>
                    </el-form-item>
                    <el-form-item prop="userId" label="用户ID">
                        <el-input-number
                            type="number"
                            v-model="formData.userId"
                            style="width: 200px"
                            disabled
                        ></el-input-number>
                    </el-form-item>
                    <el-form-item prop="nickname" label="昵称">
                        <el-input v-model="formData.nickname" style="width: 280px" disabled></el-input>
                    </el-form-item>
                    <el-form-item prop="pic" label="logo">
                        <single-upload v-model="formData.pic" :disabled="true"></single-upload>
                    </el-form-item>
                    <el-form-item prop="introduction" label="简介">
                        <el-input type="textarea" :rows="4" v-model="formData.introduction" disabled></el-input>
                    </el-form-item>
                    <el-form-item prop="headBg" label="背景">
                        <el-image :src="formData.headBg" style="height: 170px"></el-image>
                        <el-image :src="formData.showroomBg" style="height: 170px"></el-image>
                    </el-form-item>
                    <!-- <el-form-item prop="publish" label="发布">
                        <el-radio v-model="formData.publish" :label="true">是</el-radio>
                        <el-radio v-model="formData.publish" :label="false">否</el-radio>
                    </el-form-item> -->
                    <!-- <el-form-item prop="status" label="状态">
                        <el-select v-model="formData.status" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item prop="collections" label="藏品">
                        <el-table :data="formData.collections">
                            <el-table-column type="index" label="#"></el-table-column>
                            <el-table-column prop="collectionId" label="ID" width="110">
                                <template slot-scope="{ row }">
                                    <el-link @click="$router.push('/companyCollectionShelf?id=' + row.collectionId)">{{
                                        row.collectionId
                                    }}</el-link>
                                </template>
                            </el-table-column>
                            <el-table-column prop="pic" label="图" width="110">
                                <template slot-scope="{ row }">
                                    <el-image
                                        style="width: 30px; height: 30px"
                                        :src="row.pic"
                                        fit="cover"
                                        :preview-src-list="[row.pic]"
                                    ></el-image>
                                </template>
                            </el-table-column>
                            <el-table-column prop="name" label="名称"></el-table-column>
                        </el-table>
                    </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>
export default {
    name: 'ShowroomEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('showroom/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: {},
            statusOptions: [
                { label: '未审核', value: 'NOT_AUTH' },
                { label: '认证中', value: 'PENDING' },
                { label: '已认证', value: 'SUCCESS' },
                { label: '失败', value: 'FAIL' }
            ]
        };
    },
    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('/showroom/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(`/showroom/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-textarea.is-disabled .el-textarea__inner {
    color: #7c7e7e;
}
.inline-wrapper {
    .el-form-item {
        display: inline-block;
    }
}
.tip {
    font-size: 12px;
    color: @text3;
    margin-top: 5px;
}
</style>