<template>
    <!-- MQTT管理 -->
    <div class="flex-col">
        <s-header :name="$t('mqtt.topicManagement')" :noback="false"></s-header>
        <div class="outer flex-col justify-center ">
            <div class="l-flex-RC">
                <div class="outer1 flex-col"></div>
                <div class="TextGroup flex-col">
                    <span class="txt">{{ $t("mqtt.addTopic") }}</span>
                </div>
            </div>
        </div>
        <van-form @submit="addTopic">
            <van-cell-group inset>
                <van-field v-model="params.topic" :label="$t('mqtt.topic')" :placeholder="$t('mqtt.topicPlaceholder')"
                    :rules="[{ required: true, message: $t('mqtt.topicPlaceholder') }]" />
                <van-field name="radio" :label="$t('mqtt.qos')">
                    <template #input>
                        <van-radio-group v-model="params.qos" direction="horizontal">
                            <van-radio :name="0">0</van-radio>
                            <van-radio :name="1">1</van-radio>
                            <van-radio :name="2">2</van-radio>
                        </van-radio-group>
                    </template>
                </van-field>
                <van-field v-model="params.topicDesc" rows="5" autosize :label="$t('mqtt.topicDescription')" type="textarea" :placeholder="$t('mqtt.topicDescriptionPlaceholder')"
                    :rules="[{ required: true, message: $t('mqtt.topicDescriptionPlaceholder') }]" />
                <br>
            </van-cell-group>
            <div style="margin: 10px 100px;">
                <van-button round block type="primary" native-type="submit">
                    {{ $t('mqtt.sumbit') }}
                </van-button>
            </div>
        </van-form>
        <div class="taskMessageBox flex-col">
            <div class="searchRow flex-row justify-between">
                <div class="flex-col">
                     <div class="flex-row justify-between bd3">
                        <div class="flex-col outer4"></div>
                        <span class="flex-col txt2">{{ $t("mqtt.topicList") }}（左滑选择删除）</span>
                    </div>
                </div>
            </div>
            <div class="listBox">
                <div v-for="(item, index) in alarmHistoryList" :key="index" class="listItem">
                    <van-swipe-cell right-width="70">
                        <div class="itemBox">
                        <div class="itemRow">
                            <span class="itemTitle">{{ $t('mqtt.topic') }}:&nbsp;</span>
                            {{ item.topic }}
                        </div>
                        <div class="itemRow">
                            <span class="itemTitle">{{ $t('mqtt.createTime') }}:&nbsp;</span>
                            {{ showDateTime(item.createDate) }}
                        </div>
                        <div class="itemRow">
                            <span class="itemTitle">{{ $t('mqtt.qos') }}:&nbsp;</span>
                            {{ item.qos }}
                        </div>
                        <div class="itemRow">
                            <span class="itemTitle">{{ $t('mqtt.topicDescription') }}:&nbsp;</span>
                            {{ item.topicDesc }}
                        </div>
                    </div>
                        <template #right>
                            <van-button square text="删除" color="#df5e4c" class="delete-button" @click="deleteTopic(item.id)"/>
                        </template>
                    </van-swipe-cell>
                    
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { onMounted, reactive, ref } from 'vue';
import sHeader from "../../../components/SimpleHeader";
import { addMqttTopic, getSubscribeList, delSubscribe } from '@/service/mqtt';
import { showConfirmDialog, showFailToast, showSuccessToast } from "vant";
import dateUtil from "@/utils/dateUtil";
import { useI18n } from "vue-i18n";


export default {
    components: { sHeader },
    setup() {
        const { t } = useI18n();
        // 添加主题参数
        const params = reactive({
            topic: '',
            qos: 0,
            topicDesc: '',
        });
        // 添加主题
        const addTopic = async () => {
            const { data } = await addMqttTopic(params);
            if (data.code === "00000") {
                showSuccessToast(t("mqtt.addSuccessfully"));
                setTimeout(() => {
                    getList();
                }, 1000);
            } else {
                showFailToast(t("mqtt.addFailed"));
            }
        };
        // 查询消息参数
        const alarmHistoryList = ref([]); // 列表集合
        const alarmHistoryTotal = ref(0); // 列表总数
        // 获取消息列表
        const getList = async () => {
            const { data } = await getSubscribeList();
            if (data.code === "00000") {
                alarmHistoryList.value = data.data;
                alarmHistoryTotal.value = alarmHistoryList.value.length;
            } else {
                showFailToast(data.message);
            }
        };
        // 格式化时间
        const showDateTime = (date) => {
            return date
                ? dateUtil.formateDate(new Date(date), "yyyy/MM/dd hh:mm:ss")
                : "";
        };
        // 删除主题
        const deleteTopic =  (id) => {
            const params = {
                id: id
            };
            showConfirmDialog({
                title: "提示",
                message: "确定删除吗？",
            }).then(async() => {
                const { data } = await delSubscribe(params)
                if (data.code === "00000") {
                    showSuccessToast("删除成功");
                    setTimeout(() => {
                        getList();
                    }, 1000);
                } else {
                    showFailToast(data.message);
                }
            }).catch(() => {
            })
        };

        // 初始化页面获取列表
        onMounted(async () => {
            getList();
        });
        return {
            params,
            addTopic,
            alarmHistoryList,
            alarmHistoryTotal,
            showDateTime,
            deleteTopic,
        };
    }
};
</script>

<style lang="less" scoped>
@import "../../../common/style/common.less";

.outer {
    background: rgba(255, 255, 255, 1) url("../../../assets/home/line.png") bottom center no-repeat;
    background-size: 100% auto;
    padding: 20px 20px;

    .outer1 {
        position: relative;
        width: 15px;
        height: 15px;
        margin-right: 5px;
        background: url("../../../assets/home/machineSales.png") center no-repeat;
        background-size: 100%;
    }

    .TextGroup {

        .txt {
            overflow-wrap: break-word;
            color: rgba(64, 77, 116, 1);
            font-size: 0.4rem;
            font-family: PingFangSC-Semibold;
            text-align: left;
            white-space: nowrap;
            line-height: 0.4rem;
            display: block;
        }

    }
}

.searchRow {
    padding: 0.5rem 0.5rem;
    background: rgba(255, 255, 255, 1) url("../../../assets/home/line.png") bottom center no-repeat;
    background-size: 100%;
    align-items: center;

    .bd3 {


        .outer4 {
            width: 19px;
            height: 19px;
            margin-right: 5px;
            background: url("../../../assets/taskMessage/retailIcon.png") top center no-repeat;
            background-size: 100%;
        }

        .txt2 {
            overflow-wrap: break-word;
            color: rgba(64, 77, 116, 1);
            font-size: 14px;
            font-family: PingFangSC-Semibold;
            text-align: left;
            white-space: nowrap;
            // line-height: 0.4rem;
            display: block;

            .discountNumber {
                font-size: 18px;
                color: #df5e4c;
            }
        }
    }

    .label3 {
        color: #4d6add;
    }

    .main5 {
        width: auto;

        .label2 {
            width: 0.5rem;
            height: 0.5rem;
            margin-right: 5px;
            cursor: pointer;
        }
    }
}

.listBox {
    .listItem {
        width: 100%;
        background: url("../../../assets/home/line.png") bottom center no-repeat;
        background-size: 100%;
        position: relative;
        display: inline-block;

        .itemBox {
            margin: 10px 20px;
            line-height: 20px;

            .itemRow {
                width: 100%;
            }

            .itemTitle {
                color: #8787a6;
                font-size: 13px;
            }

            .discount {
                padding-left: 2em;
            }
        }
    }
}


.delete-button {
    height: 100%;
    padding: 10px 10px;
}
</style>