<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="25%" :before-close="doClose" center>
        <data-form ref="dataform" :form="form" :formModel="formModel" :rules="rules"></data-form>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="doCreate">确 定</el-button>
            <el-button @click="doClose">取 消</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { listPublishMap } from '@/api/jmap/map'
    
    import { getQuestById} from '@/api/quest';

    export default {
        name: 'ScriptDraft',
        data() {
            return {
                dialogVisible: false,
                mapList: [],
                taskStatusList: [],
                disabled:null,
                formModel: {
                    name: '',
                    mapId: '',
                    description:''
                },
                isShow: false,
            }
        },
        props: {
            title: String,
        },
        computed: {
            form() {
                let isAdd = this.type === 'ADD'
                let form = {
                    labelWidth: '100px',
                    items: [
                        { prop: 'name', label: '剧本名称', type: 'text', required: true},
                        { prop: 'mapId', label: '地图类型', type: 'select', required: true, options: this.mapList,disabled:this.disabled},
                        { prop: 'description', label: '剧本描述', type: 'textarea', required: true},
                    ]
                }
                return form
            },
            rules() {
                let crules = {
                    name: [
                        { required: true, message: '请输入剧本', trigger: 'blur' },
                    ],
                    mapId: [
                        { required: true, message: '请选择地图类型', trigger: 'change' },
                    ],
                    description:[
                        { required: true, message: '请输入剧本描述', trigger: 'blur' },
                    ]
                }
                return crules
            },
            // title() {
            //     return '创建剧本'
            // }
        },
        mounted() {
            this.loadInitData();
        },
        methods: {
            loadInitData() {
                this.mapList = [];
                listPublishMap().then(response => {
                    this.mapList = response.data.map(elem => { return { value: elem.id, label: elem.name } });
                })
            },
            doShow(questid) {
                if(questid)
                {
                    getQuestById(questid).then(resp=>{
                        let data={'name':resp.data.name,'description':resp.data.description,'mapId':resp.data.mapId};
                        this.formModel=data;
                        this.formModel.id=questid;
                        this.disabled="disabled";
                        this.dialogVisible = true
                    });
                }
                else
                {
                    this.disabled=null;
                    this.dialogVisible = true
                }
                
            },
            doCreate() {
                let self = this
                this.$refs.dataform.validateForm(() => {
                    self.$emit('create', Object.assign({}, this.formModel));
                    self.doClose()
                })
            },
            doClose() {
                this.$refs.dataform.resetForm();
                this.isShow = false;
                this.dialogVisible = false
            }
        }
    }
</script>