<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="rgba(61, 81, 232, 1)"
          type="primary"
          class="search"
          @click="searchBtn"
          ><el-icon><Search /></el-icon> <span>搜索</span></el-button
        >
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button type="primary" color="rgba(61, 81, 232, 1)" @click="reset"
          ><el-icon><RefreshRight /></el-icon><span>刷新</span></el-button
        >
        <el-button type="danger" @click="allLook" color="rgba(61, 81, 232, 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="create_time"
          label="创建时间"
          width="250"
        />
        <el-table-column align="center" prop="message" label="消息" />
        <el-table-column align="center" prop="state" label="状态" width="200">
          <template #default="scope">
            <div v-if="scope.row.state == 1">已读</div>
            <div v-else-if="scope.row.state == 0" style="color: red">未读</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, 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: "全部",
  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" }],
});
// 获取消息列表
const getList = async () => {
  let data = new FormData();
  // console.log(searchInput.state === 0);

  data.set("create_time", searchInput.createTime);
  if (searchInput.state == 2 || searchInput.state == "全部") {
  } else {
    data.set("state", searchInput.state);
  }
  data.set("page", currentPage.value);
  data.set("rows", pageSize.value); //前面的key记得对应！
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/userqueryMes.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.status == 200) {
    tableData.list = res.data.rows;
    if (res.data.currentPage) {
      currentPage.value = res.data.currentPage;
    } else {
      currentPage.value = 1;
    }
    // if (tableData.list) {
    //   let newsArr = tableData.list.filter((item) => {
    //     return item.state == 0;
    //   });
    //   console.log(newsArr.length);
    //   sessionStorage.setItem("newsNum", newsArr.length);
    // } else {
    //   sessionStorage.setItem("newsNum", 0);
    // }
    total.value = res.data.total;
  } else {
    tableData.list = res.data.rows;
    currentPage.value = 1;
    total.value = res.data.total;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.message == "token错误") {
      router.push({
        path: `/login`,
      });
    }
  }
};
// 刷新
const reset = async () => {
  searchInput.state = "全部";
  searchInput.createTime = "";
  let data = new FormData();
  console.log(searchInput.state === 0);
  data.set("page", currentPage.value);
  data.set("rows", pageSize.value); //前面的key记得对应！
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/userqueryMes.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.status == 200) {
    tableData.list = res.data.rows;
    if (res.data.currentPage) {
      currentPage.value = res.data.currentPage;
    } else {
      currentPage.value = 1;
    }
    if (tableData.list) {
      let newsArr = tableData.list.filter((item) => {
        return item.state == 0;
      });
      console.log(newsArr.length);
      sessionStorage.setItem("newsNum", newsArr.length);
    } else {
      sessionStorage.setItem("newsNum", 0);
    }
    total.value = res.data.total;
  } else {
    tableData.list = res.data.rows;
    currentPage.value = 1;
    total.value = res.data.total;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
    if (res.data.message == "token错误") {
      router.push({
        path: `/login`,
      });
    }
  }
};
// 路由
const handleSelect = (key, keyPath) => {};
// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 查看按钮
const look = async (row) => {
  let data = new FormData();
  data.set("id", row.id);
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/userupdateMesState.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.status == 200) {
    store.dispatch("newsAsync");
    router.push({
      path: `/schoolBus/statement`,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 查看按钮
const allLook = async (row) => {
  let data = new FormData();
  let res = await axios({
    method: "post",
    url: api.value + "/carBook/userupdateAllMesState.action",
    headers: {
      token: sessionStorage.getItem("token"),
    },
    data: data,
  });
  console.log(res);
  if (res.status == 200) {
    getList();
    store.dispatch("newsAsync");
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 多选框功能
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();
});
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-input {
  width: 192px;
}
</style>
