<!--
 * @Description: nginx配置框
 * @Author: xiaofengzhou
 * @Email: 2946471396@qq.com
 * @Date: 2022-08-05 21:36:40
 * @LastEditTime: 2022-10-18 15:35:50
 * @LastEditors: xiaofengzhou
-->

<template>
    <div>
        <a-modal :maskClosable="false" width="800px" v-model:visible="visible" @cancel="onCancle" @ok="onOk" okText="保存" cancelText="取消">
            <div class="head">
                <span class="iconfont icon-screen-code-line head-icon"></span>
                &nbsp;&nbsp;
                <span class="head-text">Nginx配置</span>

            </div>
            <div class="change-content">
                <a-button @click="changeStandOrCluster(0)" class="change-btn" :class="!isActive ? 'active' : ''">单机配置
                </a-button>
                <a-button @click="changeStandOrCluster(1)" class="change-btn" :class="isActive ? 'active' : ''">集群配置
                </a-button>
            </div>
            <div v-show="standOrCluster">
                <a-form-model :style="{ width: '100%' }" layout="horizontal" v-bind="{
                    labelCol: { span: 6 },
                    wrapperCol: { span: 16 },
                }" :colon="true" labelAlign="right" :model="nginxConfig.property" :rules="nginxRules"
                    ref="nginxConfig">
                    <a-form-model-item label="nginx地址" prop="nginxAddress" v-bind="{}">
                        <a-input size="large" :disabled="action === 'view'" v-model="nginxConfig.property.nginxAddress"
                            placeholder="请输入nginx配置" />
                    </a-form-model-item>
                    <!-- <a-form-model-item label="请选择部署环境" prop="env" v-bind="{}">
                        <a-radio-group v-model="nginxConfig.env" :options="deployEnvOption">
                        </a-radio-group>
                    </a-form-model-item> -->

                </a-form-model>
                <a-form-model :style="{ width: '100%' }" layout="horizontal" v-bind="{
                    labelCol: { span: 6 },
                    wrapperCol: { span: 16 },
                }" :colon="true" labelAlign="right" :model="nginxConfig" :rules="nginxRules" ref="middlewareConfig">
                    <a-form-model-item label="是否需要安装中间件" prop="installedFlag" v-bind="{}">
                        <a-radio-group :disabled="action === 'view'" v-model="nginxConfig.installedFlag"
                            :options="installedFlagOption">
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="nginxConfig.installedFlag === '0'" label="请选择中间件脚本" prop="scriptId"
                        v-bind="{}" placeholder="请选择中间件脚本">
                        <a-select size="large" :options="allScriptsSel" v-model="nginxConfig.scriptId" :disabled="action === 'view'">

                        </a-select>
                    </a-form-model-item>
                </a-form-model>
            </div>
            <div v-show="!standOrCluster" style="height:500px;overflow:scroll">
                <!-- <div style="padding-left: 27px;margin-bottom: 10px;margin-top: 10px;">
                    是否配置集群:&nbsp;&nbsp;&nbsp;<a-radio-group :disabled="action === 'view'" v-model="isConfigClutser" :options="isInstallClusster">
                    </a-radio-group>
                </div> -->
                <div v-for="(item, index) in nginxClusterConfig">
                    <a-form-model :style="{ width: '100%' }" layout="horizontal" v-bind="{
                        labelCol: { span: 6 },
                        wrapperCol: { span: 16 },
                    }" :colon="true" labelAlign="right" :model="item.property" :rules="nginxRules"
                        ref="nginxClusterConfig">
                        <a-form-model-item label="注册中心地址" prop="nginxAddress" v-bind="{}">
                            <a-input size="large" :disabled="action === 'view'" v-model="item.property.nginxAddress" placeholder="请输入nginx配置" />
                        </a-form-model-item>
                    </a-form-model>
                     <a-form-model :style="{ width: '100%' }" layout="horizontal" v-bind="{
                    labelCol: { span: 6 },
                    wrapperCol: { span: 16 },
                }" :colon="true" labelAlign="right" :model="item" :rules="nginxRules" ref="middlewareClusterConfig">
                    <a-form-model-item label="是否需要安装中间件" prop="installedFlag" v-bind="{}">
                        <a-radio-group :disabled="action === 'view'" v-model="item.installedFlag"
                            :options="installedFlagOption">
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="item.installedFlag === '0'" label="请选择中间件脚本" prop="scriptId"
                        v-bind="{}" placeholder="请选择中间件脚本">
                        <a-select size="large" :options="allScriptsSel" v-model="item.scriptId" :disabled="action === 'view'">

                        </a-select>
                    </a-form-model-item>
                </a-form-model>
                </div>
                <div class="add-btn-container" v-show="action !== 'view'">
                    <button @click="expandForm" class="add-btn">
                        <span class="iconfont icon-roundadd add-btn-span"></span>
                    </button>
                </div>
            </div>

        </a-modal>
    </div>
</template>

<script>
const isInstallClusster = [{ label: '是', value: true }, { label: '否', value: false }]
export default {
    data() {
        return {
        
            allScriptsSel: [],
            action: 'view',
            isInstallClusster: [{ label: '是', value: true }, { label: '否', value: false }],
            isConfigClutser: false,
            isInstallClusster: [{ label: '是', value: true }, { label: '否', value: false }],
            isActive: false,

            deployEnvOption: [{ label: '测试环境', value: '0' }, { label: '生产环境', value: '1' }],
            isConfigClutser: false,
            isInstallClusster,
            installedFlagOption: [{ label: '安装', value: '0' },
            { label: '不安装', value: '1' }],
            visible: false,
            standOrCluster: true,
            defaultNginxConfig: {
                scriptId: '',
                middlewareType: '7',
                property: {
                    nginxAddress: '',
                
                },
                configType: '0',
                installedFlag: '0',
                env: '0'
            },
            nginxConfig: {
                scriptId: '',

                middlewareType: '7',
                property: {
                    nginxAddress: '',
              
                },
                configType: '0',
                installedFlag: '0',
                env: '0'

            },
            defaultNginxClusterConfig:
                [
                    {
                scriptId: '',

                        middlewareType: '7',
                        property: {
                            nginxAddress: '',
                        },
                        configType: '1',
                        installedFlag: '0'
                    }
                ]
            ,
            nginxClusterConfig:
                [
                    {
                scriptId: '',

                        middlewareType: '7',
                        property: {
                            nginxAddress: '',
                        },
                        configType: '1',
                        installedFlag: '0'
                    }
                ]
            ,
            nginxRules: {
                scriptId: [
                    {
                        required: true,
                        message: "中间件脚本不能为空",
                        trigger: "change",
                    }
                ],
                nginxAddress: [
                    {
                        required: true,
                        message: "nginx地址不能为空",
                        trigger: "blur",
                    },
                    {
                        max: 255,
                        message: "nginx地址长度不能大于255",
                        trigger: "blur",
                    },
                ],
            },
        }
    },
    methods: {
        initCluster(record){
             if (record.length === 0) {
                this.isConfigClutser = false
                this.nginxClusterConfig=JSON.parse(JSON.stringify(this.defaultNginxClusterConfig))


            } else {
                this.isConfigClutser=true

                          this.nginxClusterConfig=record



            }
        },
        // 初始化
        init(record) {
            this.nginxConfig = record
        },
        // 确认
        onOk() {
            
            if (this.action === 'view') {
                this.visible = false
            } else {
            if (!this.standOrCluster) {
                let p1 = new Promise((resolve, reject) => {

                        let flag = true
                        for (var x in this.$refs["middlewareClusterConfig"]) {
                            // 批量校验输入框
                            this.$refs["middlewareClusterConfig"][x].validate((valid) => {
                                if (!valid) {
                                    flag = false
                                }

                            })
                        }
                        if (flag) {
                            resolve()
                        }
                    })
                    // let p2 = new Promise((resolve, reject) => {
                    //     this.$refs["nginxConfig"].validate((valid) => {
                    //         if (valid) {
                    //             resolve()
                    //         }
                    //     })
                    // })

                    let p3 = new Promise((resolve, reject) => {
                        let flag = true
                        for (var x in this.$refs["nginxClusterConfig"]) {
                            // 批量校验输入框
                            this.$refs["nginxClusterConfig"][x].validate((valid) => {
                                if (!valid) {
                                    flag = false
                                }

                            })
                        }
                        if (flag) {
                            resolve()
                        }
                    })
                    Promise.all([p1,p3]).then(() => {
                        // this.$emit("nginxConfig", this.nginxConfig, 1);
                            this.nginxClusterConfig.map((item, index) => {
                                    item.properties = JSON.stringify(item.property)
                                })
								
                            this.$emit("nginxConfig", this.nginxClusterConfig, 2);
                            this.visible = false
                    })

            } else {
                let p1 = new Promise((resolve, reject) => {
                    this.$refs["middlewareConfig"].validate((valid) => {
                        if (valid) {
                            resolve()
                        }
                    })
                })
                let p2 = new Promise((resolve, reject) => {
                    this.$refs["nginxConfig"].validate((valid) => {
                        if (valid) {
                            resolve()
                        }
                    })
                })
                Promise.all([p1, p2]).then(() => {
                    this.$emit("nginxConfig", this.nginxConfig, 1);
                    this.visible = false
                })
             
            }


        }},
        // 取消
        onCancle() {
            if (this.action === 'view') {
                this.visible = false
            } else {
                this.nginxConfig = JSON.parse(JSON.stringify(this.defaultNginxConfig))
                this.nginxClusterConfig = JSON.parse(JSON.stringify(this.defaultNginxClusterConfig))

                this.$emit("nginxConfig", "cancel");
            }

        },
        // 重置
        reset() {
            // this.standOrCluster = true
            this.nginxClusterConfig=JSON.parse(JSON.stringify(this.defaultNginxClusterConfig))

            this.nginxConfig = JSON.parse(JSON.stringify(this.defaultNginxConfig))

        },
        // 切换集群单机
        changeStandOrCluster(val) {
            if (val === 1) {
                this.standOrCluster = false
                this.isActive = true
            } else {
                this.standOrCluster = true
                this.isActive = false
            }
        },
        // 集群扩展
        expandForm() {
            this.nginxClusterConfig.push(
                {
                scriptId: '',

                    middlewareType: '7',

                    property: {
                        nginxAddress: ''
                    },

                    configType: '1',
                    installedFlag: '0'

                }
            )
        }

    },
    //生命周期 - 创建完成（访问当前this实例）
    created() {

    },
    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {

    }
}
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */
/deep/ .ant-input{
    font-size: 16px !important;
}
/deep/ .ant-select{
    font-size: 16px !important;
}
/deep/ .ant-btn{
    font-size: 16px !important;
}

/deep/ .ant-form label{
    font-size: 16px !important;
}
.head {
    text-align: center;
    margin-bottom: 5px;

    .head-icon {
        font-size: 28px;
        vertical-align: middle;
        color: #284f84;
    }

    .head-text {
        font-size: 18px;
        vertical-align: middle;
        color: #284f84;
    }
}

.change-content {
    text-align: center;
    margin-bottom: 5px;

    .active {
        border-bottom: 2px solid #284f84;
    }

    .change-btn {
        /* border: none; */
        border-radius: 0%;
        border-top: none;
        border-left: none;
        border-right: none;

    }
}

.add-btn-container {
    text-align: center;

    .add-btn {
        border: none;
        background: none;
    }

    .add-btn-span {
        color: #284f84;
        font-size: 20px;
    }

}
</style>