<template>
    <div>
        <search-layout :needBox="true">
            <template slot="searchItem1">
                <span class="label">账户名称</span>
                <el-input
                    v-model="searchParams.username"
                    size="small"
                    clearable
                    placeholder="请输入"
                    maxlength="60"
                >
                </el-input>
            </template>
            <template slot="searchItem2">
                <span class="label">登录时间</span>
                <!-- <el-date-picker
                    v-model="createDate"
                    type="daterange"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                >
                </el-date-picker> -->
                <el-date-picker
                    v-model="searchParams.createTimeBegin"
                    type="date"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="开始日期"
                    class="dateRangeInput"
                >
                </el-date-picker>
                <span class="dateSeparator">至</span>
                <el-date-picker
                    v-model="searchParams.createTimeEnd"
                    type="date"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="结束日期"
                    class="dateRangeInput"
                >
                </el-date-picker>
            </template>
            <template slot="searchBtn1">
                <el-button type="primary" @click="doSearch">查询</el-button>
            </template>
            <template slot="searchBtn2">
                <el-button type="primary" @click="doReset">重置</el-button>
            </template>
        </search-layout>

        <tableList
            ref="table"
            style="margin: 30px"
            :columns="columns"
            :getDataWay="getDataWay"
            :pagination="pagination"
            index
        >
        </tableList>
    </div>
</template>

<script>
import searchLayout from "@/components/grid/searchLayout";
import tableList from "@/components/grid/TableList";

export default {
    name: "loginLogManagement", // 登录日志
    components: { searchLayout, tableList },
    data() {
        return {
            searchParams: {
                //搜索参数
                username: "", // 操作账户
                createTimeBegin: "", // 上传时间起
                createTimeEnd: "", // 上传时间止
            },
            createDate: "",
            columns: [
                //表格列
                {
                    label: "账户名称",
                    prop: "username",
                },
                {
                    label: "IP",
                    prop: "ip",
                },
                {
                    label: "登录状态",
                    prop: "state",
                    formatter: (data) => {
                        return data.state == 1 ? "成功" : "失败";
                    },
                },
                {
                    label: "登录时间",
                    prop: "createTime",
                },
            ],
            pagination: {
                //分页使用
                currentPage: 1,
                pageSize: 10,
                position: "right",
                pageSizes: [10, 30, 50, 100, 200],
                layout: "sizes, total, prev, pager, next, jumper",
            },
            getDataWay: {
                //加载表格数据
                dataType: "url",
                type: "post",
                // firstRequest: false,
                data: this.$api.systemManagement.getLoginLogPageList,
                param: {},
            },
        };
    },

    methods: {
        doSearch() {
            // if (this.createDate) {
            //     this.searchParams.createTimeBegin = `${this.createDate[0]}`;
            //     this.searchParams.createTimeEnd = `${this.createDate[1]}`;
            // } else {
            //     this.searchParams.createTimeBegin = "";
            //     this.searchParams.createTimeEnd = "";
            // }

            let createTimeBegin = "";
            if (this.searchParams.createTimeBegin) {
                createTimeBegin = `${this.searchParams.createTimeBegin} 00:00:00`;
            }

            let createTimeEnd = "";
            if (this.searchParams.createTimeEnd) {
                createTimeEnd = `${this.searchParams.createTimeEnd} 23:59:59`;
            }

            if (
                createTimeBegin &&
                createTimeEnd &&
                createTimeBegin > createTimeEnd
            ) {
                this.$message.error("结束时间不能早于开始时间");
                return;
            }

            let pageMap = {
                username: this.searchParams.username,
                createTimeBegin,
                createTimeEnd,
            };
            this.refreshList(pageMap);
        },
        //刷新table
        refreshList(param) {
            param
                ? this.$refs["table"].loadData(param)
                : this.$refs["table"].loadData();
        },
        doReset() {
            this.searchParams = {
                username: "",
                createTimeBegin: "",
                createTimeEnd: "",
            };
            // this.createDate = "";
            this.doSearch();
        },
    },
    mounted() {},
};
</script>

<style scoped lang="less">
</style>