<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" :before-close="doClose" center>
    <data-form ref="dataform" :form="form" :form-model="formModel" :rules="rules" />
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doCreate">{{ $t('global.confirm') }}</el-button>
      <el-button @click="doClose">{{ $t('global.cancel') }}</el-button>
    </span>
  </el-dialog>
</template>

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

export default {
    name: 'ScriptDraft',
    props: {
        title: {
            type: String,
            default() {
                return '';
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            mapList: [],
            taskStatusList: [],
            formModel: {
                name: '',
                mapId: '',
                description:''
            }
        };
    },
    computed: {
        form() {
            const form = {
                labelWidth: '150px',
                items: [
                    { prop: 'name', label: this.$t('scriptRecord.scriptName'), type: 'text' },
                    { prop: 'description', label: this.$t('scriptRecord.scriptDescription'), type: 'textarea' }
                ]
            };
            return form;
        },
        rules() {
            const crules = {
                name: [
                    { validator: this.validateScriptName, trigger: 'blur' },
                    { validator: this.validateScriptName, trigger: 'change' }
                ],
                description: [
                    { validator: this.validateDescription, trigger: 'blur' },
                    { validator: this.validateDescription, trigger: 'change' }
                ]
            };
            return crules;
        }
    },
    methods: {
        validateScriptName(rule, value, callback) {
            if (value.trim().length === 0) {
                this.formModel.name = this.formModel.name.replace(/\s/g, '');
                return callback(new Error(this.$t('scriptRecord.inputScriptName')));
            } else {
                return callback();
            }
        },
        validateDescription(rule, value, callback) {
            if (value.trim().length === 0) {
                this.formModel.description = this.formModel.name.replace(/\s/g, '');
                return callback(new Error(this.$t('scriptRecord.inputScriptDescription')));
            } else {
                return callback();
            }
        },
        doShow(questid) {
            if (questid) {
                getQuestById(questid).then(resp=>{
                    const data = {'name':resp.data.name, 'description':resp.data.description, 'mapId':resp.data.mapId};
                    this.formModel = data;
                    this.formModel.id = questid;
                    this.dialogVisible = true;
                });
            } else {
                this.formModel.mapId = this.$route.params.mapId;
                this.dialogVisible = true;
            }

        },
        doCreate() {
            const self = this;
            this.$refs.dataform.validateForm(() => {
                self.$emit('create', Object.assign({}, this.formModel));
                self.doClose();
            });
        },
        doClose() {
            this.$refs.dataform.resetForm();
            this.dialogVisible = false;
        }
    }
};
</script>
<style lang="scss" scoped>
/deep/ .el-dialog--center .el-dialog__body{
    padding: 25px 65px 30px 10px;
}
</style>
