<template>
    <el-dialog
        v-loading="saving"
        @close="closeDialog"
        title="备案管理信息"
        center
        :visible.sync="dialogVisible"
        width="600px"
    >
        <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="writerDirector" 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="phone" 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="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-dialog>
</template>
<script>
export default {
    props: ['dialogVisible'],
    data() {
        return {
            saving: false,
            showMore: false,
            formData: {},
            rules: {
                phone: [
                    {
                        required: true,
                        pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
                        message: '请输入联系电话',
                        trigger: 'blur'
                    }
                ],
                writerDirector: [
                    {
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    }
                ]
            },
            arrid: '',
            timeRange: '',
            artTypes: []
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        closeDialog() {
            this.$emit('close');
        },
        init(id) {
            this.arrid = id;
            if (id) {
                this.$http
                    .get('arrange/get/' + 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);
            //     });
        },
        submit() {
            let data = { ...this.formData };
            data.id = this.arrid;
            this.saving = true;
            this.$http
                .post('/arrange/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$parent.getData();
                    this.$forceUpdate();
                    // this.dialogVisible = false;
                    this.closeDialog();
                })
                .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>
