<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">消息通知</span>
    </div>
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>创建时间 : </span>
          <el-date-picker v-model="searchInput.createTime" type="date" placeholder="请选择时间" format="YYYY-MM-DD"
            value-format="YYYY-MM-DD" />
        </div>
        <div class="condition">
          <span>状态 : </span>
          <el-select style="width: 120px" v-model="searchInput.state" class="m-2" placeholder="请选择状态">
            <el-option label="全部" value="2" />
            <el-option label="已读" value="1" />
            <el-option label="未读" value="0" />
          </el-select>
        </div>

        <el-button style="margin-left: 20px;color:#fff" color="rgba(111, 182, 184, 1)" type="primary" class="search"
          @click="searchBtn"><el-icon>
            <Search />
          </el-icon> <span>搜索</span></el-button>
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button type="primary" style="color:#fff" color="rgba(111, 182, 184, 1)" @click="reset"><el-icon>
            <RefreshRight />
          </el-icon><span>刷新</span></el-button>
        <el-button type="danger" style="color: #fff;" @click="allRead" color="rgba(111, 182, 184, 1)">
          <img src="@/assets/read.png" style="width: 14px; height: 14px; margin-right: 4px" alt="" />
          <span>一键已读</span></el-button>
      </div>

      <!-- 编辑/新增摄像头IP -->
      <el-dialog v-model="centerDialogVisible" :title="dialongTitle" width="40%" align-center :before-close="editClose">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
          :size="formSize" status-icon>
          <el-form-item label="厂商" prop="count">
            <el-input v-model="ruleForm.count" placeholder="请输入厂商" />
          </el-form-item>
          <el-form-item label="IP地址" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入IP地址" />
          </el-form-item>
          <el-form-item label="状态" prop="region">
            <el-select v-model="ruleForm.region" placeholder="请选择状态">
              <el-option label="在线" value="1" />
              <el-option label="离线" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建人" prop="username" v-show="dialongTitle == '新增摄像头IP'">
            <el-input v-model="ruleForm.username" placeholder="请输入创建人姓名" />
          </el-form-item>
          <el-form-item label="摄像头密码" prop="pass">
            <el-input v-model="ruleForm.pass" placeholder="请输入摄像头密码" />
          </el-form-item>
          <el-form-item label="最后更新人" prop="username" v-show="dialongTitle == '修改摄像头IP'">
            <el-input v-model="ruleForm.username" placeholder="请输入更新人姓名" />
          </el-form-item>
          <el-form-item label="设备型号" prop="date1">
            <el-input v-model="ruleForm.date1" placeholder="请输入设备型号" />
          </el-form-item>
          <el-form-item label="协议" prop="types">
            <el-input v-model="ruleForm.types" placeholder="请输入协议"></el-input>
          </el-form-item>
          <el-form-item label="安装位置" prop="date2">
            <el-input v-model="ruleForm.date2" placeholder="请输入安装位置" />
          </el-form-item>

          <el-form-item label="摄像机编码" prop="resource">
            <el-input v-model="ruleForm.resource" placeholder="请输入摄像机编码">
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" placeholder="请输入备注" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确定
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <div class="footer">
      <el-table :row-class-name="tableRowClassName" :data="tableData.list" @selection-change="handleSelectionChange"
        style="width: 100%" :header-cell-style="{
          background: 'rgba(240, 243, 247, 1)',
          height: '50px',
          border: 0,
        }">
        <!-- <el-table-column align="center" type="selection" width="80" /> -->
        <el-table-column align="center" prop="updateTime" label="创建时间" width="250" />
        <el-table-column align="center" prop="recordNo" label="工单号" width="250" />
        <el-table-column align="center" prop="content" show-overflow-tooltip label="消息" />
        <el-table-column align="center" prop="isRead" label="状态" width="200">
          <template #default="{ row }">
            <div v-if="row.isRead == 1">已读</div>
            <div @click="look(row)" style="cursor: pointer;color: red" v-else-if="row.isRead == 0">未读</div>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="操作" width="220">
          <template #default="scope">
            <el-button link type="primary" @click="look(scope.row)">
              <div class="look">查看</div>
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
    </div>

    <!-- 分页组件 -->
    <el-pagination background :current-page="currentPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
      :total="total" @update:current-page="handleCurrentChange" />
  </div>
</template>

<script setup>
import { ref, watch, reactive, nextTick, onBeforeMount, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import vidiconsApi from "@/api/vidicons.js";
import { dayjs } from "element-plus";
import lodash from "lodash";
import axios from "axios";
import { useStore } from "vuex";
const store = useStore();
const api = ref("");
const router = useRouter();
// 表格数据
const tableData = reactive({ list: [] });
const activeIndex = ref(); // 默认跳转路由
const dialongTitle = ref(""); // 弹窗标题

const searchInput = reactive({
  state: "2",
  createTime: "",
}); // 搜索按钮数据
5;

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(10); // 当前总数
const selectData = reactive({ list: [] }); // 多选框选择的数据

const centerDialogVisible = ref(false); // 控制新增或修改弹窗开关
const videoDialogVisible = ref(false); // 控制播放弹窗开关

const urlLink = ref(); // 输出链接

// 表单数据
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  name: "",
  region: "",
  count: "",
  date1: "",
  date2: "",
  types: [],
  resource: "",
  desc: "",
  username: "",
  pass: "",
  id: "",
});
// 表单验证
const rules = reactive({
  region: [
    {
      required: true,
      message: "状态不能为空",
      trigger: "blur",
    },
  ],
  name: [
    { required: true, message: "IP地址不能为空", trigger: "blur" },
    // { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  count: [
    {
      required: true,
      message: "厂商不能为空",
      trigger: "blur",
    },
  ],
  date1: [
    {
      required: true,
      message: "设备型号不能为空",
      trigger: "blur",
    },
  ],
  date2: [
    {
      required: true,
      message: "安装位置不能为空",
      trigger: "blur",
    },
  ],
  types: [
    {
      required: true,
      message: "协议不能为空",
      trigger: "blur",
    },
  ],
  username: [
    {
      required: true,
      message: "更新人员姓名不能为空",
      trigger: "blur",
    },
  ],
  pass: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
  resource: [
    {
      required: true,
      message: "摄像机编码不能为空",
      trigger: "blur",
    },
  ],
  // desc: [{ required: true, message: "Please input activity form", trigger: "blur" }],
});
watch(
  () => searchInput.createTime,
  (newVal, oldVal) => {
    console.log("监听时间:", newVal);
    if (newVal == null) {
      searchBtn();
    }
  }
);
// 获取消息列表
const getList = async (msg) => {
  let data = {
    currentPage: currentPage.value,
    pageCount: pageSize.value,
    statu: searchInput.state
  }
  if (searchInput.createTime) {
    data.createTime = searchInput.createTime
  }
  let res = await axios({
    method: "get",
    url: api.value + "/repairSystemMessages/queryManageMessagePage",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, "消息中心列表");
  if (res.data.code == 200) {
    tableData.list = res.data.data.list;
    total.value = res.data.data.totalCount
    if (msg) {
      ElMessage({
        type: "success",
        showClose: true,
        message: msg,
        center: true,
      });
    }
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.code == 570) {
      sessionStorage.removeItem("token")
      router.push({
        path: `/login`,
      });
    }
  }
};
// 刷新
const reset = async () => {
  getList("刷新成功")
};
// 路由
const handleSelect = (key, keyPath) => { };
// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 查看按钮
const look =lodash.throttle(async (row) => {
  let data = { id: row.id }
  let res = await axios({
    method: "get",
    url: api.value + "/repairSystemMessages/readSystemMessage",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, "已读");
  if (res.data.code == 200) {
    store.dispatch("newsAsync");
    getList()
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });

  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
},1000);

// 获取未读数量
const newsNum = async () => {
  let data = {}
  let res = await axios({
    method: "get",
    url: api.value + "/repairSystemMessages/queryPCMessageUnreadCount",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, "获取未读数量");
  if (res.data.code == 200) {
    let num = res.data.data.count
    // store.dispatch("newsAsync");
    sessionStorage.setItem("newsNum", num)
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 一键已读
const allRead = lodash.throttle(async () => {
  let data = {}
  let res = await axios({
    method: "get",
    url: api.value + "/repairSystemMessages/readAllManageMessage",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, "一键已读");
  if (res.data.code == 200) {
    store.dispatch("newsAsync");
    getList()
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });

  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
},1000);

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 分页
const handleCurrentChange = (value) => {
  currentPage.value = value;
  getList();
};

onBeforeMount(() => {
  api.value = store.state.user.api;
  getList();
  store.dispatch("newsAsync");
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  width: 97.5%;
  height: 89%;
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;

    .camera {
      margin-right: 15px;
      color: #4392f7;
    }
  }

  .middle {
    width: 96%;
    margin: 0 auto;

    color: #000;

    // border-bottom: 1px solid rgb(231, 231, 231);
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin: 10px 0 0 0;

      .condition {
        display: flex;
        align-items: center;
        margin: 10px 30px 10px 0;

        span {
          margin: 0 10px 0 0;
        }
      }
    }

    .gongneng {
      margin: 10px 0;
    }

    :deep(.cont) {
      width: 60%;
      margin: 20px auto;
    }

    :deep(.download) {
      display: flex;
      align-items: center;
      margin: 10px;
    }

    :deep(.download span) {
      font-size: 16px;
      margin-left: 20px;
    }

    :deep(.cont .el-button) {
      margin-left: 60px;
      margin-bottom: 30px;
    }

    :deep(.cont .accomplish) {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    :deep(.cont .accomplish .el-button) {
      width: 50%;
      margin: 0;
    }
  }

  .footer {
    width: 96%;
    height: 550px;
    margin: 10px auto 30px;

    .el-table--fit {
      height: 100%;

      :deep(.el-table__header-wrapper) {
        background-color: #000;
      }

      :deep(.el-table__row) {
        height: 50px;
      }

      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      .el-button--primary {
        margin-left: 5px;
      }

      :deep(.el-table__body .even) {
        background-color: #fff;
      }

      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }

      :deep(.look) {
        padding: 5px 14px;
        border-radius: 45px;
        border: 0.74px solid rgba(30, 125, 251, 1);
      }
    }
  }

  // .el-pagination {
  //   // width: 1600px;
  //   width: 96%;
  //   margin: 0 auto 18px;
  //   justify-content: flex-end;

  //   :deep(.el-pagination__total) {
  //     color: #000;
  //   }

  //   :deep(.el-pagination__goto) {
  //     color: #000;
  //   }

  //   :deep(.el-pagination__classifier) {
  //     color: #000;
  //   }

  //   :deep(.el-input__wrapper) {
  //     border: 1px solid rgba(0, 0, 0, 1);
  //     border-radius: 5px;
  //     box-shadow: none;
  //   }

  //   :deep(.el-pager li) {
  //     margin: 0 5px;
  //     border: 1px solid rgba(0, 0, 0, 1);
  //     border-radius: 5px;
  //     background-color: transparent;
  //   }

  //   :deep(.el-pager li.is-active) {
  //     background-color: rgba(30, 125, 251, 1);
  //   }

  //   :deep(.btn-prev) {
  //     margin-right: 5px;
  //     border: 1px solid rgba(0, 0, 0, 1);
  //     border-radius: 5px;
  //     background-color: transparent;
  //   }

  //   :deep(.btn-next) {
  //     margin-left: 5px;
  //     border: 1px solid rgba(0, 0, 0, 1);
  //     border-radius: 5px;
  //     background-color: transparent;
  //   }
  // }

  .el-pagination {
    width: 96%;
    margin: 0 auto 18px;
    justify-content: flex-end;

    :deep(.el-pagination__total) {
      color: #000;
    }

    :deep(.el-pagination__goto) {
      color: #000;
    }

    :deep(.el-pagination__classifier) {
      color: #000;
    }

    :deep(.el-input__wrapper) {
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      box-shadow: none;
    }

    :deep(.el-pager li) {
      margin: 0 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.el-pager li.is-active) {
      background-color: rgba(111, 182, 184, 1);
    }

    :deep(.btn-prev) {
      margin-right: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.btn-next) {
      margin-left: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
  }
}

.el-input {
  width: 192px;
}
</style>
