<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Input
          v-model="searchForm.name"
          placeholder="请输入姓名、账号、设备号"
          search
          @on-search="searchList()"
          style="width: 240px; margin-right: 20px"
        />
        <Checkbox @on-change="showReadChange" v-model="searchForm.showRead"
          >只显示未读</Checkbox
        >
      </div>
    </div>
    <div class="table_wrap">
      <div class="log_list" v-for="item in searchForm.list" :key="item.doid">
        <div class="username" @click="headleView(item)">
          {{ item.classname }} 【{{ item.username }}】<span
            :class="[item.hasread === 1 ? 'active' : '']"
            >{{ item.hasread === 1 ? "已读" : "未读" }}</span
          >
        </div>
        <div class="comm">{{ item.comm }}</div>
        <div class="school">
          学校：<span>{{ item.schoolName }}</span> 设备号：<span>{{
            item.sn
          }}</span>
          时间：<span>{{ item.createtime }}</span>
        </div>
      </div>
      <div class="nodeta" v-if="searchForm.list.length === 0">暂无数据</div>
    </div>
    <div class="page_wrap" v-if="searchForm.list.length > 0">
      <Page
        :transfer="true"
        :total="searchForm.total"
        :current="searchForm.page"
        :page-size="searchForm.size"
        :page-size-opts="[10, 20, 40, 60]"
        @on-change="pageChange"
        @on-page-size-change="pageSizeChange"
        show-total
        show-sizer
      ></Page>
    </div>
    <!-- 查看 -->
    <Modal
      class="modal1"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div>
        <div class="username">
          {{ viewInfo.classname }} 【{{ viewInfo.username }}】
        </div>
        <div class="school">
          学校：<span style="margin-right: 20px">{{
            viewInfo.schoolName
          }}</span>
          时间：{{ viewInfo.createtime }}
        </div>
        <div class="comm">{{ viewInfo.comm }}</div>
        <div class="comm sn">设备号：{{ viewInfo.sn }}</div>
      </div>
      <div slot="footer" style="text-align: center">
        <div>
          <Button @click="viewInfo.show = false">关闭</Button>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
import { logdoperatemsg_edit_read, logdoperatemsg_list } from "@/api/log";

export default {
  data() {
    return {
      searchForm: {
        showRead: 0,
        name: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      viewInfo: {
        show: false
      },
      userInfo: {}
    };
  },
  created() {
    this.userInfo = JSON.parse(
      localStorage.getItem("xh_control_userInfo")
    ).content;
    this.searchList();
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  methods: {
    headleView(item) {
      this.viewInfo = {
        show: true,
        classname: item.classname,
        username: item.username,
        schoolName: item.schoolName,
        createtime: item.createtime,
        comm: item.comm,
        sn: item.sn
      };
      if (item.hasread != 1) {
        logdoperatemsg_edit_read({ ldomid: item.ldomid }).then((res) => {
          if (res.code === 0) {
            this.searchForm.list.forEach((v) => {
              if (item.ldomid === v.ldomid) {
                //改状态为已读
                v.hasread = 1;
              }
            });
            //修改首页未读条数
            this.$EventBus.$emit("nreadChange");
          } else {
            this.$Message.error(res.msg);
          }
        });
      }
    },
    showReadChange() {
      this.searchList();
    },
    // 搜索
    searchList() {
      this.searchForm.page = 1;
      this.getList();
    },
    // 页码改变
    pageChange(page) {
      this.searchForm.page = page;
      this.getList();
    },
    // 每页显示数量改变
    pageSizeChange(size) {
      this.searchForm.size = size;
      this.searchForm.page = 1;
      this.getList();
    },
    getList() {
      logdoperatemsg_list({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        name: this.searchForm.name,
        page: this.searchForm.page,
        size: this.searchForm.size,
        showRead: this.searchForm.showRead ? 1 : 0
      }).then((res) => {
        if (res.code === 0) {
          this.searchForm.list = res.obj.data;
          this.searchForm.total = res.obj.total;
        } else {
          this.$Message.error(res.msg);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.search_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 16px;
  .search_left {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }
}
.table_wrap {
  border: none;
  .log_list {
    border-radius: 8px;
    border: 1px solid #ced9f2;
    padding: 16px;
    margin-bottom: 10px;
    font-family: "SourceHanSansCN";
    .username {
      color: #253a70;
      font-size: 16px;
      font-weight: 700;
      line-height: 30px;
      margin-bottom: 10px;
      cursor: pointer;
      span {
        display: inline-block;
        border-radius: 11px;
        background: #95abdf1a;
        color: #95abdf;
        font-size: 14px;
        line-height: 14px;
        font-weight: 400;
        padding: 4px 10px;
      }
      .active {
        background: #ebf3ff;
        color: #339dff;
      }
    }
    .comm {
      color: #253a70;
      font-size: 16px;
      font-weight: 400;
      line-height: 30px;
    }
    .school {
      color: #798cb5;
      font-size: 16px;
      font-weight: 400;
      line-height: 30px;
      span {
        margin-right: 20px;
      }
    }
  }
}
.modal1 {
  font-family: "SourceHanSansCN";

  .username {
    color: #253a70;
    font-size: 18px;
    font-weight: 500;
    line-height: 30px;
    text-align: center;
  }
  .school {
    color: #afbcc7;
    font-size: 12px;
    font-weight: 400;
    text-align: center;
    line-height: 26px;
    margin: 10px 0;
  }
  .comm {
    color: #253a70;
    font-size: 16px;
    font-weight: 400;
    line-height: 30px;
  }
  .sn {
    user-select: text;
  }
}
.nodeta {
  min-height: 300px;
  line-height: 300px;
  text-align: center;
}
</style>
