<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="创建时间" style="width: 308px;">
                <el-date-picker v-model="pickerTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="设备类型" prop="type">
                    <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>
                <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="refreshList"></right-toolbar>
        </el-row>

        <!-- 
            {
            "id": 1,
            "data": "1",
            "content": "报警内容",
            "phoneNum": "18800001111",
            "ts": "2023-09-28 16:01:21"
            }
         -->
        <!-- 表格数据 -->
        <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="80" />
            <!-- <el-table-column label="部门id" prop="id" width="120" /> -->
            <el-table-column label="手机号" prop="phoneNum" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="发送时间" prop="ts" width="200" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="报警类型" prop="alarmType" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="报警值" prop="data" align="center" :show-overflow-tooltip="true" />
            <!-- <el-table-column label="报警内容" prop="content" align="center" :show-overflow-tooltip="true" /> -->
            <el-table-column label="接收时间" prop="receiveDate" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="发送状态" prop="status" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="失败原因" prop="reason" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="是否已处理" prop="ishandle" align="center" :show-overflow-tooltip="true" > 
                <template #default="{ row }">
                    <el-tag v-if="row.ishandle == 0" type="danger">未处理</el-tag>
                    <el-tag v-if="row.ishandle == 1" type="success">已处理</el-tag>
                </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" />


    </div>
</template>
  
<script setup>
import { ref, reactive, toRefs, getCurrentInstance, onMounted } from 'vue'
import { Picture as IconPicture } from '@element-plus/icons-vue'
import { getSmsRecord,getSmsRecordFromAliyun } from '@/api/guojian/index'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const { proxy } = getCurrentInstance();
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        startTime: '',
        endTime: '',
        userId: userStore.userId,
    },
});
const pickerTime = ref([]);
const { queryParams, form } = toRefs(data);

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

let deviceList = ref([])
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
/** 查询设备列表 */
function getList() {
    loading.value = true;
    if (Array.isArray(pickerTime.value) && pickerTime.value.length > 0) {
        queryParams.value.startTime = pickerTime.value[0];
        queryParams.value.endTime = pickerTime.value[1];
    } else {
        queryParams.value.startTime = '';
        queryParams.value.endTime = '';
    }
    getSmsRecord(queryParams.value).then(res => {
        loading.value = false;
        deviceList.value = res.rows;
        total.value = res.total;
    });

}

function refreshList() {
    getSmsRecordFromAliyun().then(res => {
        if (res.code == 200) {
            getList();
        }
    });
}

/** 搜索按钮操作 */
function handleQuery() {
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value.startTime = '';
    queryParams.value.endTime = '';
    queryParams.value.alarmType = '0';
    pickerTime.value = null;
    handleQuery();
}



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



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

</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>
  