<template>
    <transition appear enter-active-class="animated fadeIn">
        <q-card class="shadow-24">
            <!-- 搜索条件 -->
            <q-card-section>
                <div class="row q-col-gutter-md">
                    <!-- 刷新 查询 -->
                    <div class="col-xl-2 col-lg-2 col-sm-12">
                        <q-btn-group push glossy>
                            <q-btn push label="新增" icon="add" @click="addOrEditForm()">
                                <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                    content-style="font-size: 12px">{{
                                        '新增一条数据' }}</q-tooltip>
                            </q-btn>
                            <q-btn push label="刷新" icon="refresh" @click="reFresh()">
                                <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                    content-style="font-size: 12px">清空所有条件并，刷新数据</q-tooltip>
                            </q-btn>
                            <q-btn push label="查询" icon="search" @click="getPage()">
                                <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                    content-style="font-size: 12px">按条件查询数据</q-tooltip>
                            </q-btn>
                        </q-btn-group>
                    </div>
                    <!-- 搜索条件 -->
                    <div class="col-xl-2 col-lg-2 col-sm-12">
                        <q-input dense label="控制器编号" outlined v-model="pagination.data.deviceCode" />
                    </div>
                    <div class="col-xl-2 col-lg-2 col-sm-12">
                        <q-input dense label="控制器名称" outlined v-model="pagination.data.deviceName" />
                    </div>
                    <div class="col-xl-2 col-lg-2 col-sm-12">
                        <q-select dense outlined v-model="pagination.data.activated" :options="activatedList" emit-value
                            map-options label="控制器状态" />
                    </div>
                </div>
            </q-card-section>
            <q-card-section style="padding:0 10px 15px 10px;">
                <div class="row q-col-gutter-md">
                    <div class="col-xl-12 col-lg-12 col-sm-12">
                        <q-table class="my-sticky-header-column-table" :rows="list" row-key="id" separator="cell"
                            :loading="loading" :columns="columns" @request="onRequest" v-model:pagination="pagination"
                            :table-style="{ height: Screen.height - 190 + 'px' }" flat bordered
                            :rows-per-page-options="[20, 30, 40, 50, 100]">
                            <template v-slot:body="props">
                                <q-tr :props="props" class="tr_hover">
                                    <q-td style="font-size: 16px;" key="index" :props="props">{{ props.rowIndex + 1
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="deviceCode" :props="props">{{
                                        props.row.deviceCode
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="deviceName" :props="props">{{
                                        props.row.deviceName
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="protocol" :props="props">{{
                                        props.row.protocol
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="host" :props="props">{{
                                        props.row.host
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="port" :props="props">{{
                                        props.row.port
                                    }}</q-td>
                                    <q-td style="font-size: 16px;" key="isConnected" :props="props">
                                        <q-badge size="" style="font-size: 18px;height:30px;"
                                            :color="props.row.isConnected == true ? 'green' : 'red'"
                                            :label="props.row.isConnected == true ? '连接' : '断开'" />
                                    </q-td>
                                    <q-td style="font-size: 16px;" key="activated" :props="props">
                                        <q-badge size="" style="font-size: 18px;height:30px;"
                                            :color="props.row.activated == true ? 'green' : 'red'"
                                            :label="props.row.activated == true ? '启用' : '停用'" />
                                    </q-td>
                                    <q-td style="font-size: 16px;" key="remark" :props="props">{{ props.row.remark }}</q-td>
                                    <q-td key="action" :props="props" style="width: 100px">
                                        <q-btn round flat push color="accent" icon="menu" @click="toDetail(props.row)"
                                            class="text-weight-bold click-hover">
                                            <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                                content-style="font-size: 12px">查看PLC点位</q-tooltip>
                                        </q-btn>
                                        <q-btn round flat push color="info" icon="edit" @click="addOrEditForm(props.row)">
                                            <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                                content-style="font-size: 12px">编辑这条数据</q-tooltip>
                                        </q-btn>
                                        <q-btn round flat push color="negative" icon="delete"
                                            @click="openDelDialog(props.row)">
                                            <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                                                content-style="font-size: 12px">删除这条数据</q-tooltip>
                                        </q-btn>
                                    </q-td>
                                </q-tr>
                            </template>
                        </q-table>
                    </div>
                </div>
            </q-card-section>
        </q-card>
    </transition>
    <!-- PLC点位 -->
    <devicePointDialog ref="devicePointDialogRef" />
    <!-- 系统参数设置 -->
    <addOrEditDialog ref="addOrEdit" @refresh="getPage" />
    <!-- 删除弹窗 -->
    <customDialog ref="deleteDialogRef" @func="delFunc" />
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { Notify, Screen } from 'quasar';
import { deviceApi } from 'src/api/device/deviceApi';
import { DeivceSearchEntity } from '../../api/device/model/device';
import { Pagination, cconvert } from 'src/api/class';
import customDialog from 'src/components/customDialog.vue';
import addOrEditDialog from 'src/pages/device/components/addOrEditDialog.vue'
import devicePointDialog from './components/devicePointDialog.vue'
import helper from 'src/Utils/helper';

//公共
const activatedList = ref([
    { label: '启用', value: true },
    { label: '停用', value: false }
])
const pagination = ref(new Pagination(new DeivceSearchEntity()));
const loading = ref(false);

const columns = ref([
    {
        name: 'index',
        label: '序号',
        field: 'index',
        align: 'center',
        headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;',
    },
    {
        name: 'deviceCode',
        align: 'center',
        label: '控制器编号',
        field: 'deviceCode',
        headerClasses: 'text-h4 text-weight-bold',
        headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;',
    },
    { name: 'deviceName', align: 'center', label: '控制器名称', field: 'deviceName', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'protocol', align: 'center', label: '控制器系列', field: 'protocol', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'host', align: 'center', label: '控制器主机地址', field: 'host', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'port', align: 'center', label: '控制器主机端口', field: 'port', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'isConnected', align: 'center', label: 'PLC设备连接状态', field: 'isConnected', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'activated', align: 'center', label: '控制器状态', field: 'activated', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'remark', align: 'center', label: '备注', field: 'remark', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' },
    { name: 'action', align: 'center', label: '操作', field: 'action', headerStyle: 'background-color: #5186ec;color: white;font-weight: bold;font-size:20px;' }
])

const list = ref([]);

onMounted(() => {
    getPage();
})

const reFresh = () => {
    pagination.value.data = new DeivceSearchEntity();
    getPage();
};

const onRequest = (props: any) => {
    const { page, rowsPerPage, rowsNumber } = props.pagination;
    pagination.value.page = page;
    pagination.value.rowsPerPage = rowsPerPage != 0 ? rowsPerPage : rowsNumber;
    getPage();
};

const getPage = () => {
    deviceApi.getDevicePageList(pagination.value).then((res: any) => {
        list.value = res.data;
        cconvert(pagination.value, res);
    });
}

const addOrEdit = ref();
const addOrEditForm = (row?: any) => {
    addOrEdit.value.clickForm(row);
};

const deleteDialogRef = ref();
const openDelDialog = (row: any) => {
    deleteDialogRef.value.open(row);
}

const delFunc = (e: any) => {
    deviceApi.deleteDevice(e.id).then(() => {
        getPage();
    });
};

const devicePointDialogRef = ref();
const toDetail = (row: any) => {
    devicePointDialogRef.value.clickForm(row);
}

</script>
<style scoped>
/* .tr_hover {
    background: #aaaaaa;
  } */

.tr_hover:hover {
    background: #e8e8e8;
}

.click-hover {
    cursor: pointer;
    color: #1976d2;
}

.click-hover:hover {
    color: #2196f3;
    /* background-color: yellow; */
}
</style>