<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" center>
        <data-form ref="dataform" :form="form" :formModel="formModel" :rules="rules"></data-form>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="doSave">确 定</el-button>
            <el-button @click="handleClose">取 消</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { create, checkDicDetailCodeExist, getData, update } from '@/api/management/dictionaryDetail';
    import { postOperateStepData, putOperateStepData, getPlaceholderList } from '@/api/management/operation'
    import { validateCharCode } from '@/utils/validate'
    export default {
        name: 'TrainingDetailEdit',
        props: {
            type: {
                type: String,
                required: true
            },
            dicId: {
                type: String,
                required: true
            }
        },
        data() {
            return {
                dialogVisible: false,
                formModel: {
                    operateCode: '',
                    orderNum: '',
                    deviceType: '',
                    val: '',
                    tip: ''
                },
                placeholderList: [],
                deviceList: [
                    { label: '菜单', value: 'bar' },
                    { label: '道岔', value: '02' },
                    { label: '区段', value: '03' },
                    { label: '信号机', value: '04' },
                    { label: '控制模式', value: '05' },
                    { label: '站台', value: '06' },
                    { label: '列车', value: '07' },
                    { label: '车站', value: '08' },
                    { label: '车次窗', value: '09' }
                ],
            }
        },
        computed: {
            form() {
                let isAdd = this.type === 'ADD'
                let form = {
                    labelWidth: '120px',
                    items: [
                        { prop: 'orderNum', label: '步骤序号', type: 'text', required: true, disabled: !isAdd },
                        { prop: 'operateCode', label: '步骤操作码', type: 'text', required: true, disabled: !isAdd },
                        { prop: 'deviceType', label: '设备类型', type: 'select', required: true, options: this.deviceList, disabled: !isAdd },
                        { prop: 'val', label: '步骤返回值', type: 'text', required: false, disabled: !isAdd },
                        { label: '', type: 'button', options: this.placeholderList, style: 'margin-bottom: 0; margin-top: -10px;', typeBtn: 'info', click: this.addTrainRemark },
                        { prop: 'tip', label: '步骤提示信息', type: 'textarea', required: true, tooltip: true, info: '选择占位符 ‘{}’ 不可删除，否则说明显示会有问题！' },
                    ]
                }
                return form
            },
            rules() {
                let crules = {
                    operateCode: [
                        { required: true, message: '请输入步骤操作码', trigger: 'blur' },
                    ],
                    orderNum: [
                        { required: true, message: '请输入步骤序号', trigger: 'blur' },
                    ],
                    tip: [
                        { required: true, max: 500, message: '请输入步骤提示信息', trigger: 'blur' }
                    ]
                }
                return crules;
            },
            title() {
                if (this.type === 'ADD') {
                    return '创建步骤信息'
                } else {
                    return '编辑步骤信息'
                }
            }
        },
        methods: {
            async show(data) {
                this.dialogVisible = true
                let res = await getPlaceholderList({ trainingType: this.$route.query.type, skinStyle: this.$route.query.skinStyle });
                this.placeholderList = res.data;
                if (data && data.operateCode) {
                    // 获取操作占位列表
                    this.formModel = {
                        operateCode: data.operateCode,
                        orderNum: data.orderNum,
                        deviceType: data.deviceType,
                        definitionId: data.definitionId,
                        val: data.val,
                        id: data.id,
                        tip: this.repliceName(data.tip, this.placeholderList)
                    }
                }
            },
            repliceName(fieldValue, enumList) {
                if (enumList && enumList.length > 0) {
                    for (let i = 0; enumList && i < enumList.length; i++) {
                        if (fieldValue.includes(`{${enumList[i].id}}`)) {
                            fieldValue = fieldValue.replace(`{${enumList[i].id}}`, `{${enumList[i].name}}`);
                        }
                    }
                    return fieldValue;
                }
            },
            addTrainRemark(val) {
                this.formModel.tip = `${this.formModel.tip}{${val.name}}`;
            },
            doSave() {
                let self = this
                this.$refs.dataform.validateForm(() => {
                    if (self.type === 'ADD') {
                        self.create()
                    } else {
                        self.update()
                    }
                })
            },
            create() {
                let self = this
                this.placeholderList.forEach(item => {
                    if (this.formModel.tip.includes(`{${item.name}}`)) {
                        let tip = this.formModel.tip.replace(`{${item.name}}`, `{${item.id}}`);
                        this.formModel.tip = tip;
                    }
                })
                this.formModel.definitionId = this.$route.query.id;
                this.formModel.orderNum = Number(this.formModel.orderNum);
                postOperateStepData(this.formModel).then(response => {
                    self.$message.success('创建操作步骤成功')
                    self.handleClose()
                    self.$emit('reloadTable')
                }).catch(error => {
                    self.$message.error('创建操作步骤失败：' + error.message)
                })
            },
            update() {
                let self = this
                this.placeholderList.forEach(item => {
                    if (this.formModel.tip.includes(`{${item.name}}`)) {
                        let tip = this.formModel.tip.replace(`{${item.name}}`, `{${item.id}}`);
                        this.formModel.tip = tip;
                    }
                })
                this.formModel.orderNum = Number(this.formModel.orderNum);
                putOperateStepData(this.formModel).then(response => {
                    self.$message.success('更新操作步骤成功')
                    self.handleClose()
                    self.$emit('reloadTable')
                }).catch(error => {
                    self.$message.error('更新操作步骤失败' + error.message)
                })
            },
            handleClose() {
                this.formModel = {
                    operateCode: '',
                    orderNum: '',
                    deviceType: '',
                    val: '',
                    tip: ''
                }
                this.$refs.dataform.resetForm()
                this.dialogVisible = false
            }
        }
    }
</script>