<!--
 * @FilePath: \code\Goats-Cloud-ui\src\views\big\device\index.vue
 * @Author: 王路平
 * @文件版本: V1.0.0
 * @Date: 2023-06-17 08:22:57
 * @Description: 
 * 
 * 版权信息         : 2023 by ${再登软件}, All Rights Reserved.
-->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="设备名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="机架号" prop="label">
                <el-input v-model="queryParams.label" placeholder="请输入机架号" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备类型" prop="type">
                <!-- <el-input v-model="queryParams.type" placeholder="请输入设备类型" clearable style="width: 200px"
                    @keyup.enter="handleQuery" /> -->
                    <el-select v-model="queryParams.type" value-key="label" style="width: 200px" filterable placeholder="选择设备类型" @keyup.enter="handleQuery">
                        <el-option value="" label="请选择"></el-option>
                        <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.label" />
                    </el-select>
            </el-form-item>
            <el-form-item label="部门名称" prop="dept">
                <el-input v-model="queryParams.dept" placeholder="请输入部门名称" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['big:device:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" v-hasPermi="['big:device:update']"
                    @click="handleUpdate(ids[0])">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" v-hasPermi="['big:device:del']"
                    @click="handleDelete">删除</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" width="120" />
            <!-- <el-table-column label="部门id" prop="id" width="120" /> -->
            <el-table-column label="设备名称" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="机架号" prop="label" :show-overflow-tooltip="true" />
            <el-table-column label="类型" prop="type" :show-overflow-tooltip="true" />
            <el-table-column label="型号" prop="model" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="所属部门" prop="deptName" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" :show-overflow-tooltip="true">
                <template #default="scope">
                    <el-tag v-if="scope.row.status == 'true'" type="success">在线</el-tag>
                    <el-tag v-else type="warning">离线</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
                <template #default="scope">
                    <el-tooltip content="修改" placement="top" v-if="scope.row.roleId !== 1">
                        <el-button link type="primary" v-hasPermi="['big:device:update']" icon="Edit"
                            @click="handleUpdate(scope.row.id)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top" v-if="scope.row.userId !== 1">
                        <el-button link type="primary" icon="Delete" v-hasPermi="['big:device:del']"
                            @click="handleDelete(scope.row)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <add_edit @closeDialog="open = false" :type="type" :title="title" :form="form" :visible="open"
            @refreshTable="getList" />
        <!-- <assemblyVue @closeDialog="assemblyOpen = false" :form="assemblyForm" :title="assemblyTitle" :visible="assemblyOpen"></assemblyVue>
        <el-dialog v-model="imgDialogVisible">
            <img w-full :src="dialogImageUrl" alt="Preview Image" class="img-preview" />
        </el-dialog>
        <devInfo ref="devInfoRight" @closeDialog="devinfoopen = false" :visible="devinfoopen" :form="devinfoForm" :title="devinfoTitle"></devInfo> -->
    </div>
</template>
  
<script setup>
import { ref, reactive, toRefs, getCurrentInstance, onMounted } from 'vue'
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { getDeviceList, delDevice } from '@/api/big/device'
import { getDeptList } from '@/api/big/dept'
import { getDeviceTypeList } from '@/api/big/deviceType'
import useDeviceStore from '@/store/modules/device'
import add_edit from './components/add_edit.vue'
import useUserStore from '@/store/modules/user'
// import assemblyVue from './components/assembly'
// import devInfo from './components/devInfo.vue'
const store = useDeviceStore()
const userStore = useUserStore()
const { proxy } = getCurrentInstance();
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        dept: '',
        type: '',
        label: '',
        userId: 1,
    },
});

const { queryParams, form } = toRefs(data);

// testScreen({}).then(res => {
//  alert(JSON.stringify(res))
// })

let deviceList = ref([])

const open = ref(false);
const loading = ref(false);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const file_list = ref([])
const type = ref("add");
const typeList = ref([])
/** 查询设备列表 */
function getList() {
    // loading.value = true;
    // console.log(JSON.stringify(queryParams.value));
    // getDeviceList(queryParams.value).then(res => {
    //     loading.value = false;
    //     deviceList.value = res.rows;
    //     total.value = res.total;
    // });

}

/** 搜索按钮操作 */
function handleQuery() {
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 添加部门 */
function handleAdd() {
    title.value = "添加发送计划";
    form.value = {
            email: undefined,
            typeId: undefined,
            ids: undefined,
            email_title: undefined,
            email_content: undefined,
            remark: undefined,
        };
    file_list.value = []
    type.value = "add";
    open.value = true;
}
/** 修改部门 */
function handleUpdate(id) {
    title.value = "修改设备类型";
    // deviceList.value.find(item => {
    //     if (item.id == id) {
    //         form.value = item;
    //     }
    // })
    type.value = "update";

    // console.log(form.value, 'form.value');
    open.value = true;
}




/** 删除按钮操作 */
function handleDelete(row) {
    let rows = deviceList.value.filter(item => ids.value.includes(item.id));
    const names = row.name || rows.map(item => item.name).join(',');
    const deviceTypeIds = row.id || ids.value.join(',');
    proxy.$modal.confirm('是否确认删除名称为"' + names + '"的数据项？').then(function () {
        return delDevice(deviceTypeIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });

}

function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}





//获取设备类型列表
function remoteMethodTypeId() {
    getDeviceTypeList({ pageSize: 100 }).then(res => {
        if (res.code === 200) {
            typeList.value = res.rows.map(item => {
                return {
                    label: item.desc,
                    value: item.id
                }
            })
            store.setTypeList(typeList.value)
        }
    })
}

//获取部门列表
function remoteMethodDeptId() {
    getDeptList({ pageSize: 100 }).then(res => {
        if (res.code === 200) {
            let deptList = res.rows.map(item => {
                return {
                    label: item.deptName,
                    value: String(item.deptId)
                }
            })
            store.setDeptList(deptList)
        }
    })
}
//获取网关列表
function remoteMethodGetwayId() {
    getDeviceList({ pageNum: 1,pageSize: 100, type: '网关',userId:1 }).then(res => {
        if (res.code === 200) {
            let getwayList = res.rows.map(item => {
                return {
                    label: item.name,
                    value: item.id
                }
            })
            store.setGetwayList(getwayList)
        }
    })
}

// 获取表单选项
function getAddFormOptions() {
    remoteMethodTypeId()
    remoteMethodDeptId()
    remoteMethodGetwayId()
}
onMounted(() => {
    getList();
    getAddFormOptions()
})

</script>
  
<style lang="scss" scoped>
:deep(.el-table .el-table__cell) {
    z-index: auto;
}

.img-preview {
    max-width: 100%;
    max-height: 100%;
    display: block;
    margin: 0 auto;
}
</style>
  