<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" :before-close="doClose" center>
    <data-form v-if="isCreate" ref="dataform" :form="form" :form-model="formModel" :rules="rules" />
    <data-form v-else ref="dataform" :form="updateForm" :form-model="updateFormModel" :rules="updateRules" />
    <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 { getScriptByIdBasic} from '@/api/script';

export default {
    name: 'ScriptDraft',
    props: {
        title: {
            type: String,
            default() {
                return '';
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            isCreate:true,
            formModel: {
                name: '',
                mapId: '',
                description:''
            },
            updateFormModel:{
                name: '',
                mapId: '',
                description:'',
                fullMarksDuration:0,
                passingGradeDuration:0,
                zeroDuration:0
            }
            // updateForm
            //
        };
    },
    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;
        },
        updateForm() {
            const form = {
                labelWidth: '150px',
                items: [
                    { prop: 'name', label: this.$t('scriptRecord.scriptName'), type: 'text' },
                    { prop: 'description', label: this.$t('scriptRecord.scriptDescription'), type: 'textarea' },
                    { prop: 'fullMarksDuration', label: '满分时长', type: 'number', min:0, step:1, message:'秒' },
                    { prop: 'passingGradeDuration', label: '及格时长', type: 'number', min:0, step:1, message:'秒' },
                    { prop: 'zeroDuration', label: '零分时长', type: 'number', min:0, step:1, message:'秒' }
                ]
            };
            return form;
        },
        rules() {
            const crules = {
                name: [
                    { required: true, validator: this.validateScriptName, trigger: 'blur' }
                ],
                description: [
                    { required: true, validator: this.validateDescription, trigger: 'blur' }
                ]
            };
            return crules;
        },
        updateRules() {
            const crules = {
                name: [
                    { required: true, validator: this.validateScriptName, trigger: 'blur' }
                ],
                description: [
                    { required: true, validator: this.validateDescription, trigger: 'blur' }
                ],
                fullMarksDuration: [
                    { required: true, validator: this.validateDuration, trigger: 'blur' }
                ],
                passingGradeDuration: [
                    { required: true, validator: this.validateDuration, trigger: 'blur' }
                ],
                zeroDuration: [
                    { required: true, validator: this.validateDuration, trigger: 'blur' }
                ]
            };
            return crules;
        }
    },
    methods: {
        validateScriptName(rule, value, callback) {
            if (value.trim().length == 0) {
                this.formModel.name = this.formModel.name.replace(/\s/g, '');
                this.updateFormModel.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.description.replace(/\s/g, '');
                this.updateFormModel.description = this.formModel.name.replace(/\s/g, '');
                return callback(new Error(this.$t('scriptRecord.inputScriptDescription')));
            } else {
                return callback();
            }
        },
        validateDuration(rule, value, callback) {
            if (!value) {
                return callback(new Error('时长不能为空'));
            }
            switch (rule.field) {
            case 'fullMarksDuration': {
                this.updateFormModel.fullMarksDuration = parseInt(value);
                break;
            }
            case 'passingGradeDuration': {
                this.updateFormModel.passingGradeDuration = parseInt(value);
                break;
            }
            case 'zeroDuration': {
                this.updateFormModel.zeroDuration = parseInt(value);
                break;
            }
            }
            return callback();
        },
        doShow(questid) {
            if (questid) {
                getScriptByIdBasic(questid).then(resp=>{
                    this.isCreate = false;
                    const data = {
                        'name':resp.data.name,
                        'description':resp.data.description,
                        'mapId':resp.data.mapId,
                        'fullMarksDuration':resp.data.fullMarksDuration || 0,
                        'passingGradeDuration':resp.data.passingGradeDuration || 0,
                        'zeroDuration':resp.data.zeroDuration || 0
                    };
                    this.updateFormModel = data;
                    this.updateFormModel.id = questid;
                    this.dialogVisible = true;
                });
            } else {
                this.isCreate = true;
                this.formModel.mapId = this.$route.params.mapId;
                this.dialogVisible = true;
            }
        },
        doCreate() {
            const self = this;
            this.$refs.dataform.validateForm(() => {
                if (this.isCreate) {
                    self.$emit('create', Object.assign({}, this.formModel));
                } else {
                    self.$emit('create', Object.assign({}, this.updateFormModel));
                }
                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>
