<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="dialogVisible"
    append-to-body
    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 v-if="stationRoleList.includes(formModel.type)" label="值班车站:" prop="deviceCode">
        <el-select v-model="formModel.deviceCode" placeholder="请选择" size="small">
          <el-option
            v-for="item in filterStationList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </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',
    props: {
        stationList: {
            type: Array,
            required: true
        }
    },
    data() {
        return {
            dialogVisible: false,
            lodaing:false,
            formModel: {
                type: '',
                name:'',
                deviceCode: ''
            },
            stationRoleList:[
                'STATION_SUPERVISOR',
                'STATION_ASSISTANT',
                'STATION_MASTER',
                'STATION_SIGNALER',
                'STATION_PASSENGER',
                'STATION_SWITCH_MAN',
                'STATION_FACILITATOR',
                'STATION_WORKER',
                'DEVICE_MANAGER',
                'TRAIN_MASTER'
            ],
            filterStationList: [],
            rules: {
                type: [
                    { required: true, message: '请选择成员类型', trigger: 'change' }
                ],
                deviceCode: [
                    { required: true, message: '请选择值班车站', trigger: 'change' }
                ]
            },
            typeList: [
                {label: '行调', value: 'DISPATCHER'},
                {label: '通号', value: 'MAINTAINER'},
                {label: '车站值班员', value: 'STATION_SUPERVISOR'},
                {label: '车站助理', value: 'STATION_ASSISTANT'},
                {label: '车站站长', value: 'STATION_MASTER'},
                {label: '车站信号员', value: 'STATION_SIGNALER'},
                {label: '车站客运员', value: 'STATION_PASSENGER'},
                {label: '车站扳道员', value: 'STATION_SWITCH_MAN'},
                {label: '车站引导员', value: 'STATION_FACILITATOR'},
                {label: '车站工务工', value: 'STATION_WORKER'},
                {label: '设备管理员', value: 'DEVICE_MANAGER'},
                {label: '车务段段长', value: 'TRAIN_MASTER'}
            ]
        };
    },
    computed: {
        title() {
            return '添加仿真角色成员';
        }
    },
    methods: {
        doShow(row) {
            this.filterStationList = [];
            this.stationList.forEach(item => {
                if (!item.depot) {
                    this.filterStationList.push(item);
                }
            });
            this.dialogVisible = true;
        },
        doSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.lodaing = true;
                    // this.formModel.type !== 'STATION_SUPERVISOR'
                    if (!this.stationRoleList.includes(this.formModel.type)) { delete this.formModel.deviceCode; }
                    addSimulationMember(this.formModel, this.$route.query.group).then((res) => {
                        this.$message.success('添加仿真角色成员成功！');
                        this.$emit('addScriptMember', res.data);
                        this.lodaing = false;
                        this.handleClose();
                    }).catch(() => {
                        this.lodaing = false;
                        this.$message.error('添加仿真角色成员失败！');
                    });
                }
            });
        },
        handleClose() {
            this.dialogVisible = false;
            this.formModel = {
                type: '',
                name:'',
                deviceCode: ''
            };
            this.$refs.form.resetFields();
        }
    }
};
</script>
