<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="agency" label="选送机构">
                <el-input v-model="formData.agency"></el-input>
            </el-form-item> -->
            <el-form-item prop="composition" label="作品名称">
                <el-input v-model="formData.composition" :readonly="!organization"></el-input>
            </el-form-item>
            <el-form-item prop="artTypeId" label="专业">
                <el-cascader
                    ref="artCascader"
                    style="width: 100%"
                    v-model="formData.artTypeId"
                    :props="optionProps"
                    :options="artTypes"
                    :show-all-levels="false"
                    placeholder="请选择专业"
                    :disabled="!organization"
                >
                </el-cascader>
            </el-form-item>
            <el-form-item prop="teacher" label="指导老师">
                <el-input v-model="formData.teacher" :readonly="!organization"></el-input>
            </el-form-item>
            <el-form-item prop="contact" label="联系人">
                <el-input v-model="formData.contact" :readonly="!organization"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="联系电话">
                <el-input v-model="formData.phone" :readonly="!organization"></el-input>
            </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="showTime" label="表演时间">
                <el-date-picker
                    v-model="formData.showTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期时间"
                >
                </el-date-picker>
            </el-form-item> -->
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary" v-if="organization">保存</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 { mapState } from 'vuex';
import delChild from '@/mixins/delChild';
export default {
    name: 'PerformanceApplyEdit',
    mixins: [delChild],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('performanceApply/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .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);
            });
    },
    computed: {
        ...mapState(['organization'])
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                phone: {
                    pattern: /^1[3-9]\d{9}$/,
                    message: '请输入正确的手机号',
                    trigger: 'blur'
                }
            },
            statusOptions: [
                { label: '审核中', value: 'PENDING' },
                { label: '通过', value: 'PASS' },
                { label: '拒绝', value: 'DENY' }
            ],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: false,
                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 };
            data.performanceId = Number(this.$route.query.perforId);
            data.organizationId = this.organization.id;
            data.status = 'PENDING';
            this.saving = true;
            this.$http
                .post('/performanceApply/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(`/performanceApply/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 {
    background-color: #ffffff;
    color: #606266;
}
</style>
