<template>
  <view class="container">
    <!-- 建议放在外层 -->
    <u-navbar
      title="操作记录"
      @leftClick="devicemenuShow"
      :safeAreaInsetTop="false"
    >
      <view class="u-nav-slot" slot="left">
        <u-icon name="list" size="20"> </u-icon>
      </view>
    </u-navbar>

    <view v-show="menushow" class="menupage">
      <DeviceMenu @menuClick="menuClick"></DeviceMenu>
    </view>
    <view v-show="!menushow" class="main">
      <view class="u-page">
        <div class="flcard">
          <div class="btns">
            <u-button
              type="primary"
              shape="circle"
              text="起始时间"
              @click="dataShow = true"
            ></u-button>
            <u-button
              type="primary"
              shape="circle"
              text="结束时间"
              @click="dataShow1 = true"
            ></u-button>
            <u-button
              type="primary"
              shape="circle"
              text="查询"
              @click="checkHistoryData"
            ></u-button>
          </div>
          <u-datetime-picker
            :show="dataShow"
            mode="datetime"
            @cancel="dataShow = false"
            @confirm="selectStartTime"
            v-model="dataTime"
          ></u-datetime-picker>
          <u-datetime-picker
            :show="dataShow1"
            mode="datetime"
            @cancel="dataShow1 = false"
            @confirm="selectEndTime"
            v-model="dataTime"
          ></u-datetime-picker>
        </div>
        <u-list>
          <u-list-item
            class="itemback"
            v-for="(item, index) in historyData"
            :key="index"
          >
            <view class="datacard">
              <view class="content flcard">
                <view
                  class="demo-layout bg-purple-light"
                  style="margin-top: 10rpx; color: #3787fe"
                  >用户:{{ item.realname }}</view
                >
                <view
                  class="demo-layout bg-purple-light"
                  style="margin-top: 10rpx; color: #3787fe"
                  >操作设备:{{ item.devicename }}</view
                >
                <view
                  class="demo-layout bg-purple-light"
                  style="margin-top: 10rpx; color: #3787fe"
                  >操作记录:{{ item.strremark }}</view
                >
              </view>
            </view>
          </u-list-item>
        </u-list>
      </view>
    </view>
  </view>
</template>

<script>
import DeviceMenu from "./devicemenu/devicemenu.vue";
import api from "@/api/api";
import dayjs from "dayjs";
export default {
  data() {
    return {
      menushow: false,
      TabCur: "gate",
      dataShow: false,
      dataShow1: false,
      dataTime: dayjs().toDate(),
      deviceType: "", //设备类型
      StartTime: "",
      EndTime: "",
      historyData: [],
    };
  },
  components: {
    DeviceMenu,
  },
  props: ["showColum"],
  watch: {
    showColum(data) {
      this.colums = data;
      console.log(this.colums);
    },
  },
  created() {
    this.colums = this.showColum;
  },
  mounted() {},
  methods: {
    //选择起始时间
    selectStartTime(e) {
      const startTime = e.value;
      const formattedTime = dayjs(startTime).format("YYYY-MM-DD HH:mm:ss");
      this.StartTime = formattedTime;
      this.dataShow = false;
    },
    //选择起始时间
    selectEndTime(e) {
      const endTime = e.value;
      const formattedTime = dayjs(endTime).format("YYYY-MM-DD HH:mm:ss");
      this.EndTime = formattedTime;
      this.dataShow1 = false;
    },
    devicemenuShow(e) {
      this.menushow = true;
    },
    menuClick(id) {
      this.TabCur = id;
      this.deviceType = this.TabCur;
      this.menushow = false;
    },
    //查询历史数据
    checkHistoryData() {
      const params = {
        createTime_begin: this.StartTime,
        createTime_end: this.EndTime,
        pageNo: 1,
        pageSize: 10000,
        devicetype: this.deviceType + "*",
      };
      new Promise((resolve, reject) => {
        api
          .getOpreateHistory(params)
          .then((response) => {
            if (response.data.code == 200) {
              this.historyData = response.data.result.records;
            } else {
              resolve(response);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
  destroyed() {},
};
</script>

<style>
.main {
  margin-top: 100rpx;
  display: flex;
  flex-direction: column;
}
.menupage {
  position: absolute;
  z-index: 2;
  top: 40rpx;
  height: calc(100% - 40rpx);
  width: 100%;
}
.btns {
  display: flex;
}
.flcard {
  padding: 20rpx;
  background-color: #ffffff;
  margin-bottom: 5rpx;
}
</style>
