<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            size="small"
            style="max-width: 500px;"
        >
            <el-form-item prop="address" label="活动地点">
                <el-input type="textarea" :rows="2" v-model="formData.address" readonly class="width"></el-input>
            </el-form-item>
            <el-form-item prop="date" label="活动日期">
                <el-date-picker
                    v-model="formData.date"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    readonly
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="startTime" label="时间安排">
                <el-time-picker
                    readonly
                    v-model="timeRange"
                    value-format="HH:mm:ss"
                    is-range
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    placeholder="选择时间范围"
                >
                </el-time-picker>
            </el-form-item>
            <el-form-item prop="specialtyId" label="参赛专业">
                <el-cascader
                    ref="artCascader"
                    class="width"
                    v-model="formData.specialtyId"
                    :props="optionProps"
                    :options="artTypes"
                    :show-all-levels="false"
                    placeholder="请选择专业"
                >
                </el-cascader>
            </el-form-item>
            <el-form-item prop="duration" label="节目时长">
                <el-input-number type="number" v-model="formData.duration" class="input1" disabled></el-input-number>
            </el-form-item>
            <el-form-item prop="quantity" label="节目数量">
                <el-input-number type="number" v-model="formData.quantity" class="input1" disabled></el-input-number>
            </el-form-item>
            <el-form-item prop="securityOwner" label="编导姓名">
                <el-input
                    v-model="formData.writerDirector"
                    suffix-icon="el-icon-user-solid"
                    class="input1"
                    placeholder="编导姓名"
                ></el-input>
            </el-form-item>
            <el-form-item prop="securityOwnerPhone" label="联系电话">
                <el-input
                    v-model="formData.phone"
                    suffix-icon="el-icon-phone"
                    class="input1"
                    placeholder="联系电话"
                ></el-input>
            </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>
</template>
<script>
import delChild from '@/mixins/delChild';
export default {
    name: 'ArrangeEdit',
    mixins: [delChild],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('arrange/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    this.timeRange = [res.startTime, res.endTime];
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/artType/allList')
            .then(res => {
                this.artTypes = this.delChild(res);
            })
            .catch(e => {
                console.log(e);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            timeRange: '',
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: true,
                emitPath: false,
                checkStrictly: true,
                expandTrigger: 'hover'
            },
            artTypes: []
        };
    },
    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('/arrange/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(`/arrange/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>
.width {
    width: 350px;
}
/deep/.el-input.is-disabled .el-input__inner {
    background-color: #ffffff;
    color: #606266;
}
</style>
