<template>
    <div>
        <el-form :model="modalData" ref="modalData" :rules="rules" label-width="80px" class="actionInfo" label-position="right">
            <el-form-item label="动作类型" class="conditionVO" prop="action.type">
                <el-select v-model="modalData.action.type " placeholder="请选择动作类型" @change="changeType" class="inputStyle">
                    <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">
                <el-input-number v-model="modalData.action.time " class="inputStyle" :min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="回复消息" class="conditionVO" prop="action.reply" v-if="isConversitionAdd">
                <el-input v-model="modalData.action.reply" type="textarea" class="textareaStyle" rows="3"></el-input>
            </el-form-item>
            <el-form-item label="设备指令" class="conditionVO" prop="action.deviceCommand" v-if="isCommandAdd">
                <el-select v-model="modalData.action.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>
            <!-- <command-Form :commandParam="form" :commandParamModal="formModel" :rules="commandRules"></command-Form> -->
            <!-- <div id="commandParam"></div> -->
            <el-form-item>
                <el-button type="primary" @click="addQuestAction('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 {postMemberBehaviorAction,getAvailableDeviceCommand,getDeviceCodeByDeviceType} from '@/api/simulation';
    export default {
        name: 'addAction',
        props: {
            group: {
                type: String,
                required: true
            },
            memberId:{
                type:String,
                required: true
            },
            behaviorId:{
                type:String,
                required: true
            },
            buttonName:{
                type:String,
                required: true
            },
            operateType:{
                type:String,
                required: true
            }
        },
        // components:{
        //     CommandForm,
        // },
        data() {
            return {
                modalData:{
                    action:{
                        reply:"",
                        time:0,
                        type:"Conversation",
                        deviceCommand:null,
                        commandParamList:[]
                    },
                    param:{
                        startStation:"",
                        endStation:"",
                    },
                },
                actionTypeList:DeviceTypeDic.ConstSelect.actionType,
                isConversitionAdd:true,
                isCommandAdd:false,
                deviceCommandList:[],
                stationList:[],
                rules:{
                    action:{
                        reply: [
                        { required: true, message: '请输入回复消息', trigger: 'blur' }
                        ],
                        type:[
                            { required: true, message: '请选择角色类型', trigger: 'change' }
                        ],
                        deviceCommand:[
                            { required: true, message: '请选择设备指令', trigger: 'change' }
                        ]
                    },
                    param:{
                        startStation:[
                            { required: true, message: '请选择起始站台', trigger: 'change' }
                        ],
                        endStation:[
                            { required: true, message: '请选择终点站台', trigger: 'change' }
                        ]
                    }
                },
                commandRules:{},
                formModel:{},
                form:{},
                isJinLu:false,
            }
        },
        mounted(){
            this.initData();
        },
        methods:{
            addQuestAction(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let group=this.$props.group;
                        let memberId=this.$props.memberId;
                        let behaviorId=this.$props.behaviorId;
                        if(this.modalData.action.deviceCommand==null){
                            delete this.modalData.action.deviceCommand;
                        }
                        if(this.modalData.action.deviceCommand=="Train_Manual_Route_Blocking_Drive")
                        {this.modalData.action.commandParamList=[this.modalData.param.startStation,this.modalData.param.endStation];}
                        let data=this.modalData.action;
                        let obj=this;
                        postMemberBehaviorAction(group,memberId,behaviorId,data).then(response=>{
                                delete this.modalData.action.id;
                                obj.modalData.action.type="Conversation";
                                obj.modalData.action.time=0;
                                obj.modalData.action.reply="";
                                obj.modalData.param.startStation="";
                                obj.isConversitionAdd=true;
                                obj.isCommandAdd=false;
                                obj.isJinLu=false;
                                obj.modalData.action.deviceCommand=null;
                                this.modalData.param.endStation="";
                                if(this.$props.operateType=="add")
                                {
                                    this.$message.success('添加动作成功');
                                }
                                else
                                {
                                    this.$emit('modifyButtonName');
                                    this.$message.success('修改动作成功');
                                }
                                this.$emit('create');
                            }).catch(error => {
                                if(this.$props.operateType=="add")
                                {
                                    this.$messageBox(`添加动作失败: ${error.message}`);
                                }
                                else
                                {
                                    this.$messageBox(`修改动作失败: ${error.message}`);
                                }
                            });
                    }
                    else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            initData(){
                getAvailableDeviceCommand().then(response=>{
                    this.deviceCommandList=response.data;
                });
                let params = {deviceType:"StationStand"};
                let group=this.$props.group;
                getDeviceCodeByDeviceType(group,params).then(response =>{
                    let resultData=response.data;
                    resultData=JSON.parse(JSON.stringify(response.data).replace(/groupNumber/g,"name"));
                    this.stationList=resultData;
                })
            },
            changeCommand(index){
                switch(index)
                {
                    case "Train_Manual_Route_Blocking_Drive":{
                        this.isJinLu=true;
                        this.initData();
                        // this.form={
                        //     labelWidth:'100px',
                        //     items: [
                        //         { prop: 'startStation', label: '起始车站', type: 'select', required: false, change: true },
                        //     ]
                        // };
                        // this.formModel={
                        //     startStation:"",
                        // };
                        // const cfConstructor = Vue.component(CommandForm);
                        // const instance = new cfConstructor({}).$mount('#commandParam');
                        // instance.$mount('#commandParam');
                        break;
                    }
                    default:{
                        this.isJinLu=false;
                        this.initData();
                        break;
                    }
                }
            },
            changeType(index){
                switch(index)
                {
                    case "Conversation":{
                        this.isConversitionAdd=true;
                        this.isCommandAdd=false;
                        break;
                    }
                    case "Command":{
                        this.isConversitionAdd=false;
                        this.isCommandAdd=true;
                        break;
                    }
                    default:{
                        break;
                    }
                }
            },
            doShow(data){
                if(data)
                {
                    this.initData();
                    this.modalData.action.id=data.id;
                    this.modalData.action.type=data.type;
                    this.modalData.action.time=data.time;
                    if(data.type=="Conversation")
                    {
                        this.isConversitionAdd=true;
                        this.isCommandAdd=false;
                        this.modalData.action.reply=data.reply;
                    }
                    else if(data.type=="Command")
                    {
                        this.isConversitionAdd=false;
                        this.isCommandAdd=true;
                        this.modalData.action.reply="";
                        this.modalData.action.deviceCommand=data.deviceCommand;
                        if(this.modalData.action.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>
