<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="userId" label="用户ID">
                <el-input v-model="formData.userId"></el-input>
            </el-form-item>
            <el-form-item prop="houseId" label="房源">
                <el-input v-model="formData.houseId"></el-input>
            </el-form-item>
            <el-form-item prop="housekeeper" label="管家ID">
                <el-input v-model="formData.housekeeper"></el-input>
            </el-form-item>
            <el-form-item prop="contact" label="联系人">
                <el-input v-model="formData.contact"></el-input>
            </el-form-item>
            <el-form-item prop="telephone" label="手机号">
                <el-input v-model="formData.telephone"></el-input>
            </el-form-item>
            <el-form-item prop="leaveMessage" label="留言">
                <el-input v-model="formData.leaveMessage"></el-input>
            </el-form-item>
            <el-form-item prop="orderTime" label="预约时间">
                <template>
                    <div class="block">
                        <el-date-picker v-model="formData.orderTime" type="datetime" placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <el-form-item prop="timeInterval" label="预约时段">
                <el-input v-model="formData.timeInterval"></el-input>
            </el-form-item>
            <el-form-item prop="orderStatus" label="状态">
                <el-select v-model="formData.orderStatus" filterable placeholder="状态" size="small">
                    <el-option v-for="item in orderStatusList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="seeTime" label="看房时间">
                <template>
                    <div class="block">
                        <el-date-picker v-model="formData.seeTime" type="datetime" placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <el-form-item prop="showFlag" label="用户查看">
                <el-input v-model="formData.showFlag"></el-input>
            </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: '/seeOrder/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {
                    res.data.orderStatus = String(res.data.orderStatus);
                    this.formData = res.data;
                }
            })
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                orderStatus: '0',
                showFlag: 'Y'
            },
            rules: {
                userId:
                    [
                        { required: true, message: '请输入 用户ID', trigger: 'blur' },
                    ],
                houseId:
                    [
                        { required: true, message: '请输入 房源', trigger: 'blur' },
                    ],
                orderTime:
                    [
                        { required: true, message: '请选择预约时间', trigger: 'blur' },
                    ],
            },
            orderStatusList: [
                { value: '0', label: '待客服确认' },
                { value: '1', label: '待看房' },
                { value: '2', label: '已完成' },
                { value: '3', label: '已取消' },
            ]
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            data.orderTime = Date.parse(new Date(this.formData.orderTime));
            if (this.formData.seeTime) {

                data.seeTime = Date.parse(new Date(this.formData.seeTime));
            } else {
                delete data.seeTime
            }

            this.$http.post({
                url: this.formData.id ? '/seeOrder/update' : '/seeOrder/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: '/seeOrder/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>
