<template>
    <div class="app-container Input">
        <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="密钥配置" name="secret">
                <el-form ref="form" :model="form" :rules="rules" label-width="230px">
                    <!-- 上 -->
                    <el-col :span="24">
                        <el-form-item label="对接平台名称" prop="platformName">
                            <el-input v-model="form.platformName" placeholder="请输入对接平台名称" style="width: 90%"/>
                        </el-form-item>
                        <el-form-item label="接口地址前缀" prop="urlPrefix">
                            <el-input v-model="form.urlPrefix" placeholder="请输入接口地址前缀" style="width: 90%"/>
                        </el-form-item>
                        <el-form-item label="对接平台标识(OperatorId)" prop="theirOperatorId">
                            <el-input v-model="form.theirOperatorId" placeholder="请输入对接平台标识(OperatorId)"
                                      style="width: 90%"/>
                        </el-form-item>
                    </el-col>
                    <!-- 左侧 -->
                    <el-col :span="12">
                        <el-card class="box-card" style="margin-right: 50px;">
                            <div slot="header" class="clearfix">对接平台密钥信息</div>
                            <el-form-item label="唯一识别密钥(OperatorSecret)" prop="theirOperatorSecret"
                                          style="margin-top: 20px;">
                                <el-input v-model="form.theirOperatorSecret"
                                          placeholder="请输入对接平台生成的唯一识别密钥(OperatorSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="消息密钥(DataSecret)" prop="theirDataSecret">
                                <el-input v-model="form.theirDataSecret"
                                          placeholder="请输入对接平台生成的消息密钥(DataSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="初始化向量(DataSecretIv)" prop="theirDataSecretIv">
                                <el-input v-model="form.theirDataSecretIv"
                                          placeholder="请输入对接平台生成的初始化向量(DataSecretIv)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="签名密钥(SigSecret)" prop="theirSigSecret">
                                <el-input v-model="form.theirSigSecret"
                                          placeholder="请输入对接平台生成的签名密钥(SigSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                        </el-card>
                    </el-col>
                    <!-- 右侧 -->
                    <el-col :span="12">
                        <el-card class="box-card">
                            <div slot="header" class="clearfix">
                                <span>我方生成密钥信息</span>
                                <el-button style="float: right; padding: 3px 0" type="text"
                                           @click="clickGenerateSecret">生成密钥
                                </el-button>
                            </div>
                            <el-form-item label="唯一识别密钥(OperatorSecret)" prop="ourOperatorSecret"
                                          style="margin-top: 20px;">
                                <el-input v-model="form.ourOperatorSecret"
                                          placeholder="请输入我方生成的唯一识别密钥(OperatorSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="消息密钥(DataSecret)" prop="ourDataSecret">
                                <el-input v-model="form.ourDataSecret"
                                          placeholder="请输入我方生成的消息密钥(DataSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="初始化向量(DataSecretIv)" prop="ourDataSecretIv">
                                <el-input v-model="form.ourDataSecretIv"
                                          placeholder="请输入我方生成的初始化向量(DataSecretIv)"
                                          style="width: 90%"/>
                            </el-form-item>
                            <el-form-item label="签名密钥(SigSecret)" prop="ourSigSecret">
                                <el-input v-model="form.ourSigSecret" placeholder="请输入我方生成的签名密钥(SigSecret)"
                                          style="width: 90%"/>
                            </el-form-item>
                        </el-card>
                    </el-col>
                </el-form>
                <div class="save">
                    <el-button type="primary" @click="submitForm" style="margin-top: 20px;">保存</el-button>
                </div>
            </el-tab-pane>

            <el-tab-pane label="同步数据" name="notification">
                <template v-if="stationList.length > 0">
                    <el-row :gutter="20">
                        <div v-for="s in stationList" :key="s" class="text item">
                            <el-col :span="8">
                                <el-card class="box-card" style="margin-right: 50px;" shadow="hover">
                                    <el-descriptions title="站点">
                                        <el-descriptions-item label="站点id">{{ s.stationId }}</el-descriptions-item>
                                        <el-descriptions-item label="站点名称">{{ s.stationName }}</el-descriptions-item>
                                        <el-descriptions-item>
                                            <el-button @click="clickDrawer(s.stationId, s.stationName)" type="primary" style="margin-left: 16px;">
                                                打开通知菜单
                                            </el-button>
                                        </el-descriptions-item>
                                    </el-descriptions>
                                </el-card>
                            </el-col>
                        </div>
                    </el-row>
                </template>
                <el-empty description="暂无数据" v-else></el-empty>

                <el-drawer
                    :title="drawerTitle"
                    :visible.sync="drawer"
                    :direction="direction">
                    <el-row>
                        <el-col :span="24">
                            <div class="grid-content bg-purple-dark">
                                <span>推送充电站信息变化</span>
                                <el-form ref="form" :model="form" label-width="20%">
                                    <el-form-item label="活动名称">
                                        <el-input v-model="form.name"></el-input>
                                    </el-form-item>

                                    <el-form-item>
                                        <el-button type="primary" @click="onSubmit">推送</el-button>
                                    </el-form-item>
                                </el-form>
                            </div>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="24">
                            <div class="grid-content bg-purple-dark">
                                <span>推送设备状态</span>
                            </div>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="24">
                            <div class="grid-content bg-purple-dark">
                                <span>推送设备充电中状态</span>
                            </div>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="24">
                            <div class="grid-content bg-purple-dark">
                                <span>推送充电订单信息</span>
                            </div>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="24">
                            <div class="grid-content bg-purple-dark">
                                <span>推送站点费率</span>
                            </div>
                        </el-col>
                    </el-row>
                </el-drawer>
            </el-tab-pane>

        </el-tabs>
    </div>
</template>

<script>


import {addSecret, getSecret, selectStationList, updateSecret} from "@/api/thirdParty/secret";
import {generateRandomID, isEmptyString} from "@/utils/common";
import Template from "@/views/billing/template";

export default {
    dicts: ["third_party_type"],
    components: {Template},
    data() {
        return {
            activeName: "secret",
            id: this.$route.params.id,
            platformName: this.$route.params.platformName,
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            stationList: [],
            platformType: null,
            drawer: false,
            direction: 'rtl',
            drawerTitle: null,
        };
    },
    created() {
        this.updateTitle();
    },
    methods: {
        // 提交
        onSubmit() {

        },
        clickDrawer(stationId, stationName) {
            console.log("站点id：", stationId);
            this.drawerTitle = "同步【" + stationName + "】站点的数据到【" + this.platformName + "】";
            this.drawer = true;
        },
        // Drawer  :before-close="handleClose"
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {});
        },
        // 查询站点列表
        queryStationList() {
            const param = {
                platformType: this.platformType
            };
            selectStationList(param).then(response => {
                this.stationList = response.data;
                console.log(this.stationList);
            });
        },
        // 生成密钥
        clickGenerateSecret() {
            // 是否有值，默认false
            let hasValue = false;
            if (!isEmptyString(this.form.ourOperatorSecret) || !isEmptyString(this.form.ourDataSecret)
                || !isEmptyString(this.form.ourDataSecretIv) || !isEmptyString(this.form.ourSigSecret)) {
                // 任一字段不为空，hasValue为true
                hasValue = true;
            }
            if (hasValue) {
                // 有值时弹出提示框
                this.$confirm('当前已经有值, 此操作将覆盖原值, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 点击确定生成密钥
                    this.generateSecret();
                });
            } else {
                // 没有值，直接生成密钥
                this.generateSecret();
            }
        },
        // 生成密钥方法
        generateSecret() {
            this.form.ourOperatorSecret = generateRandomID(16);
            this.form.ourDataSecret = generateRandomID(16);
            this.form.ourDataSecretIv = generateRandomID(16);
            this.form.ourSigSecret = generateRandomID(16);
        },
        handleClick(tab, event) {
            // console.log("handleClick", tab, event);
            this.initializeData(tab.name);
        },
        initializeData(name) {
            if (name === "secret") {
                this.querySecretDetail();
            } else if (name === "notification") {
                this.queryStationList();
            }
        },
        querySecretDetail() {
            getSecret(this.id).then(response => {
                this.form = response.data;
                this.platformType = response.data.platformType;
            });
        },
        // 更新页面title显示
        updateTitle() {
            const platformName = this.platformName;
            const title = "【" + platformName + "】平台配置";
            const route = Object.assign({}, this.$route, {title: `${title}`});
            this.$store.dispatch("tagsView/updateVisitedView", route);
        },
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateSecret(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSecret(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        }
    },
    mounted() {
        this.initializeData(this.activeName);
    },
};
</script>

<style scoped lang="scss">
.save {
    text-align: right;
}
</style>
