<template>
    <div>
        <el-form :model="modalData" ref="modalData" :rules="rules" label-width="100px" class="actionInfo" label-position="right">
            <el-form-item label="主体角色" class="conditionVO" prop="actionVO.memberId">
                <el-select v-model="modalData.actionVO.memberId" placeholder="请选择主体角色">
                    <el-option v-for="member in memberList" :key="member.id" :label="member.name" :value="member.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="完成时间" class="conditionVO">
                <el-input-number v-model="modalData.actionVO.time " class="inputStyle" :min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="动作类型" class="conditionVO" prop="actionVO.type">
                <el-select v-model="modalData.actionVO.type " placeholder="请选择动作类型" @change="changeType">
                    <el-option v-for="actionType in actionTypeList" :key="actionType.label" :label="actionType.label" :value="actionType.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="目标角色" class="conditionVO" prop="actionVO.targetId" v-if="isConversitionAdd">
                <el-select v-model="modalData.actionVO.targetId" placeholder="请选择目标角色">
                    <el-option v-for="member in memberList" :key="member.id" :label="member.name" :value="member.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="回复消息" class="conditionVO" prop="actionVO.reply" v-if="isConversitionAdd">
                <el-input v-model="modalData.actionVO.reply" type="textarea" class="textareaStyle" rows="3"></el-input>
            </el-form-item>
            <el-form-item label="设备指令" class="conditionVO" prop="actionVO.type" v-if="isCommandAdd">
                <el-select v-model="modalData.actionVO.deviceCommand " placeholder="请选择设备指令" @change="changeCommand" class="inputStyle">
                    <el-option v-for="deviceCommand in deviceCommandList" :key="deviceCommand.deviceCommand" :label="deviceCommand.label" :value="deviceCommand.deviceCommand"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="起始站台" class="conditionVO" v-if="isJinLu" prop="param.startStation">
                <el-select v-model="modalData.param.startStation " placeholder="请选择起始站台" class="inputStyle">
                    <el-option v-for="station in stationList" :key="station.code" :label="station.name" :value="station.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="终点站台" class="conditionVO" v-if="isJinLu" prop="param.endStation">
                <el-select v-model="modalData.param.endStation " placeholder="请选择终点站台" class="inputStyle">
                    <el-option v-for="station in stationList" :key="station.code" :label="station.name" :value="station.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addScriptActionInfo('modalData')">{{buttonName}}</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import Vue from 'vue';
    import DeviceTypeDic from '@/scripts/DeviceTypeDic';
    // import CommandForm from "./commandForm";
    import {addScriptAction,modifyScriptAction,getAvailableDeviceCommand,getDeviceCodeByDeviceType,getScriptMemberData} from '@/api/simulation';
    export default {
        name: 'addAction',
        props: {
            group: {
                type: String,
                required: true
            },
            buttonName:{
                type:String,
                required: true
            },
            operateType:{
                type:String,
                required: true
            }
        },
        // components:{
        //     CommandForm,
        // },
        data() {
            return {
                modalData:{
                    actionVO:{
                        memberId:"",
                        targetId:"",
                        time:0,
                        reply:"",
                        type:"Conversation",
                        // deviceCommand:"",
                        commandParamList:[]
                    },
                    param:{
                        startStation:"",
                        endStation:"",
                    }
                },
                isConversitionAdd:true,
                isCommandAdd:false,
                actionTypeList:DeviceTypeDic.ConstSelect.actionType,
                isJinLu:false,
                stationList:[],
                memberList:[],
                deviceCommandList:[],
                rules:{
                    actionVO:{
                        memberId:[
                            { required: true, message: '请选择主体角色', trigger: 'change' }
                        ],
                        reply:[
                            { required: true, message: '请输入回复消息', trigger: 'blur' }
                        ],
                        targetId:[
                            { required: true, message: '请选择目标角色', trigger: 'change' }
                        ]
                    },
                    param:{
                        startStation:[
                            { required: true, message: '请选择起始站台', trigger: 'change' }
                        ],
                        endStation:[
                            { required: true, message: '请选择终点站台', trigger: 'change' }
                        ]
                    }
                }
            }
        },
        mounted(){
            this.initData();
        },
        methods:{
            initData(){
                getScriptMemberData(this.group).then(resp => {
                    this.memberList=resp.data;
                    getAvailableDeviceCommand().then(response=>{
                        this.deviceCommandList=response.data;
                        this.getDeviceCode();
                    });
                }).catch(error => {})
            },
            getDeviceCode(){
                let params = {deviceType:"StationStand"};
                let group=this.group;
                getDeviceCodeByDeviceType(group,params).then(response =>{
                    // debugger;
                    let resultData=response.data;
                    resultData=JSON.parse(JSON.stringify(response.data).replace(/groupNumber/g,"name"));
                    this.stationList=resultData;
                })
            },
            addScriptActionInfo(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let group=this.group;
                        if(this.modalData.actionVO.deviceCommand==""){
                            delete this.modalData.actionVO.deviceCommand;
                        }
                        if(this.modalData.actionVO.deviceCommand=="Train_Manual_Route_Blocking_Drive")
                        {this.modalData.actionVO.commandParamList=[this.modalData.param.startStation,this.modalData.param.endStation];}
                        let data=this.modalData.actionVO;
                        let obj=this;

                        if(this.$props.operateType=="add")
                        {
                            addScriptAction(group,data).then(response=>{
                                this.initActionData();
                                this.$message.success('添加动作成功');
                                this.$emit('create');
                            }).catch(error => {
                                this.$messageBox(`添加动作失败: ${error.message}`);
                            });
                        }
                        else
                        {
                            let actionId=this.modalData.actionVO.id;
                            modifyScriptAction(group,actionId,data).then(response=>{
                                this.initActionData();
                                this.$emit('modifyButtonName');
                                this.$message.success('修改动作成功');
                                this.$emit('create');
                            }).catch(error => {
                                this.$messageBox(`修改动作失败: ${error.message}`);
                            });
                        }
                    }
                    else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            initActionData(){
                this.modalData.actionVO.memberId="";
                this.modalData.actionVO.targetId="";
                this.modalData.actionVO.type="Conversation";
                this.modalData.actionVO.time=0;
                this.modalData.actionVO.reply="";
                this.modalData.param.startStation="";
                this.isConversitionAdd=true;
                this.isCommandAdd=false;
                this.isJinLu=false;
                this.modalData.actionVO.deviceCommand=null;
                this.modalData.param.endStation="";
            },
            changeType(index){
                switch(index)
                {
                    case "Conversation":{
                        this.isConversitionAdd=true;
                        this.isCommandAdd=false;
                        this.isJinLu=false;
                        break;
                    }
                    case "Command":{
                        this.isConversitionAdd=false;
                        this.isCommandAdd=true;
                        // 
                        if(this.modalData.actionVO.deviceCommand=="Train_Manual_Route_Blocking_Drive")
                        {
                            this.isJinLu=true;
                        }
                        else
                        {
                            this.isJinLu=false;
                        }
                        break;
                    }
                    default:{
                        break;
                    }
                }
            },
            changeCommand(index){
                switch(index)
                {
                    case "Train_Manual_Route_Blocking_Drive":{
                        this.isJinLu=true;
                        this.getDeviceCode();
                        break;
                    }
                    default:{
                        this.isJinLu=false;
                        break;
                    }
                }
            },
            doShow(data){
                if(data)
                {
                        // debugger;
                        this.initData();
                        this.modalData.actionVO.id=data.id;
                        this.modalData.actionVO.memberId=data.memberId;
                        this.modalData.actionVO.type=data.type;
                        this.modalData.actionVO.time=data.time;
                        if(data.type=="Conversation")
                        {
                            this.modalData.actionVO.targetId=data.targetId;
                            this.isConversitionAdd=true;
                            this.isCommandAdd=false;
                            this.modalData.actionVO.reply=data.reply;
                        }
                        else if(data.type=="Command")
                        {
                            this.isConversitionAdd=false;
                            this.isCommandAdd=true;
                            this.modalData.actionVO.reply="";
                            this.modalData.actionVO.deviceCommand=data.deviceCommand;
                            if(this.modalData.actionVO.deviceCommand=="Train_Manual_Route_Blocking_Drive")
                            {
                                this.isJinLu=true;
                                this.modalData.param.startStation=data.commandParamList[0];
                                this.modalData.param.endStation=data.commandParamList[1];
                            }
                        }
                }
            }
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";
    .addAction{
        margin-top: 20px;
        margin-left: 5px;
        font-size: 15px;
    }
    .actionInfo{
        margin-top:20px;
        margin-left: 5px;
        font-size: 15px;
        width:98%;
    }
    .inputStyle{
        width:300px;
        height:30px;
    }
    .textareaStyle{
        width:300px;
    }
</style>
