<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleClose"
    center
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
      <el-form-item label="添加仿真成员:" prop="type">
        <el-select v-model="formModel.type" placeholder="请选择" size="small">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仿真成员名称:" prop="name">
        <el-input v-model="formModel.name" placeholder="请输入成员名称" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" :lodaing="lodaing" @click="doSave">{{ $t('global.confirm') }}</el-button>
      <el-button @click="handleClose">{{ $t('global.cancel') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSimulationMember } from '@/api/jointSimulation';
export default {
    name: 'AddMember',
    data() {
        return {
            dialogVisible: false,
            lodaing:false,
            formModel: {
                type: '',
                name:''
            },
            rules: {
                type: [
                    { required: true, message: '请选择成员类型', trigger: 'change' }
                ],
                name:[
                    { required: true, message: '请输入成员名称', trigger: 'blur' }
                ]
            },
            typeList: [{label: '行调', value: 'DISPATCHER'}, {label: '通号', value: 'MAINTAINER'}]
        };
    },
    computed: {
        title() {
            return '添加仿真角色成员';
        }
    },
    methods: {
        initData(row) {

        },
        doShow(row) {
            this.initData(row);
            this.dialogVisible = true;
        },
        doSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.lodaing = true;
                    addSimulationMember(this.formModel, this.$route.query.group).then((res) => {
                        this.lodaing = false;
                        this.$message.success('添加仿真角色成员成功！');
                        this.$emit('addScriptMember', res.data);
                        this.handleClose();
                    }).catch(() => {
                        this.lodaing = false;
                        this.$message.error('添加仿真角色成员失败！');
                    });
                }
            });
        },
        handleClose() {
            this.dialogVisible = false;
            this.formModel = {
                type: '',
                name:''
            };
            this.$refs.form.resetFields();
        }
    }
};
</script>
