<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="houseId" label="房源id">
                <el-input v-model="formData.houseId"></el-input>
            </el-form-item>
            <el-form-item prop="imageType" label="类型">
                <el-select placeholder="类型" size="small" v-model="formData.imageType" class="filter-item">
                    <el-option label="图片" value="0">
                    </el-option>
                    <el-option label="封面" value="1">
                    </el-option>
                </el-select>

            </el-form-item>
            <el-form-item prop="imageName" label="名称">
                <el-input v-model="formData.imageName"></el-input>
            </el-form-item>
            <el-form-item prop="imageUrl" label="地址">
                <single-upload v-model="formData.imageUrl"></single-upload>
            </el-form-item>

            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    created() {
        if (this.$route.query.id) {
            this.$http.get({
                url: '/houseImage/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {

                    res.data.imageType = String(res.data.imageType);
                    this.formData = res.data;
                }
            })
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                houseId: this.$route.query.houseId,
                imageType: '0'
            },
            rules: {
                houseId:
                    [
                        { required: true, message: '请输入房源ID', trigger: 'blur' },
                    ],
                imageUrl:
                    [
                        { required: true, message: '请上传图片', trigger: 'blur' },
                    ],
            },
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/houseImage/update' : '/houseImage/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/houseImage/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
