<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">黑名单管理</span>
    </div>
    <div class="changeItems">
      <div
        class="changeItem"
        :class="changeIndex == 1 ? 'changeItem_active' : ''"
        @click="changeItem(1)"
      >
        汇总
      </div>
      <div
        class="changeItem"
        :class="changeIndex == 2 ? 'changeItem_active' : ''"
        @click="changeItem(2)"
      >
        明细列表
      </div>
    </div>
    <div class="middles" v-if="changeIndex == 1">
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>预订人 : </span>
              <el-input
                :clearable="true"
                @change="searchBtn"
                v-model.trim="collect.searchInput.reserveName"
                class="w-50 m-2"
                placeholder="请输入预订人"
                style="width: 200px"
              />
          </div>
          <div class="condition">
            <span>手机号码 : </span>
              <el-input
                :clearable="true"
                @change="searchBtn"
                v-model.trim="collect.searchInput.phone"
                class="w-50 m-2"
                placeholder="请输入手机号码"
                style="width: 200px"
              />
          </div>
          <div class="condition">
            <span>微校卡号 : </span>
              <el-input
                :clearable="true"
                @change="searchBtn"
                v-model.trim="collect.searchInput.cardNumber"
                class="w-50 m-2"
                placeholder="请输入微校卡号"
                style="width: 200px"
              />
          </div>
          <div class="condition">
            <span>创建时间 : </span>
            <el-date-picker
              v-model="collect.searchInput.createTime"
              type="daterange"
              range-separator="-"
              start-placeholder="起始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :prefix-icon="Calendar"
              placeholder="请选择日期"
              style="width: 280px"
              @change="searchBtn"
            />
          </div>

          <!-- <el-button
            style="margin-left: 20px"
            color="rgba(41, 109, 227, 1)"
            type="primary"
            class="search"
            @click="searchBtn"
            ><el-icon>
              <Search />
            </el-icon>
            <span>查询</span></el-button
          > -->
          <el-button
            v-if="flagBtn.yichu == 1"
            style="margin-left: 20px"
            color="rgba(41, 109, 227, 1)"
            type="primary"
            class="search"
            @click="collectFun.detailBatchOut"
            ><span>批量移出</span></el-button
          >
          <el-button
            style="margin-left: 20px"
            color="rgba(41, 109, 227, 1)"
            type="primary"
            class="search"
            @click="collectFun.collectResetClick"
            ><span>重置</span></el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="collect.tableData.list"
          @selection-change="collectFun.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
            width="100"
            align="center "
            type="index"
            label="序号"
          />
          <el-table-column align="center" prop="reserveName" label="预订人" />
          <el-table-column align="center" prop="phone" label="手机号码" />
          <el-table-column align="center" prop="cardNumber" label="微校卡号" />
          <el-table-column
            align="center"
            prop="createTime"
            label="创建时间"
            width="180"
          />
          <el-table-column align="center" prop="count" label="次数" />
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="140"
          >
            <template #default="scope">
              <div class="edit">
                <!-- 取消订单 -->
                <el-popconfirm
                  v-if="flagBtn.yichu == 1"
                  width="220"
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  :icon="InfoFilled"
                  icon-color="#f89626"
                  title="是否移出此人员?"
                  @confirm="collectFun.collectOrderOut(scope.row)"
                >
                  <template #reference>
                    <div class="look">移出</div>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pageSize">
          <span></span>
          <el-pagination
            background
            :current-page="collect.currentPage"
            :page-size="collect.pageSize"
            layout="total, prev, pager, next, jumper, slot"
            :total="collect.total"
            @update:current-page="collectFun.handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <div class="middles" v-if="changeIndex == 2">
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>预订人 : </span>
            <el-input
              :clearable="true"
              @change="searchBtn"
              v-model.trim="detail.searchInput.reserveName"
              class="w-50 m-2"
              placeholder="请输入预订人"
              style="width: 200px"
            />
          </div>
          <div class="condition">
            <span>手机号码 : </span>
            <el-input
              :clearable="true"
              @change="searchBtn"
              v-model.trim="detail.searchInput.phone"
              class="w-50 m-2"
              placeholder="请输入手机号码"
              style="width: 200px"
            />
          </div>
          <div class="condition">
            <span>微校卡号 : </span>
            <el-input
              :clearable="true"
              @change="searchBtn"
              v-model.trim="detail.searchInput.cardNumber"
              class="w-50 m-2"
              placeholder="请输入微校卡号"
              style="width: 200px"
            />
          </div>
          <div class="condition">
            <span>创建时间 : </span>
            <el-date-picker
              v-model="detail.searchInput.createTime"
              type="daterange"
              range-separator="-"
              start-placeholder="起始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :prefix-icon="Calendar"
              placeholder="请选择日期"
              style="width: 280px"
              @change="searchBtn"
            />
          </div>
          <el-button
            v-if="flagBtn.yichu == 1"
            style="margin-left: 20px"
            color="rgba(41, 109, 227, 1)"
            type="primary"
            class="search"
            @click="detailFun.detailBatchOut"
            ><span>批量移出</span></el-button
          >
          <el-button
            style="margin-left: 20px"
            color="rgba(41, 109, 227, 1)"
            type="primary"
            class="search"
            @click="detailFun.detailResetClick"
            ><span>重置</span></el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="detail.tableData.list"
          @selection-change="detailFun.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
            width="100"
            align="center "
            type="index"
            label="序号"
          />
          <el-table-column align="center" prop="reserveName" label="预订人" />
          <el-table-column
            align="center"
            prop="reservePhone"
            label="手机号码"
            width="180"
          />
          <el-table-column
            align="center"
            prop="cardNumber"
            label="微校卡号"
            width="160"
          />
          <el-table-column
            align="center"
            prop="reserveLiveTime"
            label="预入住时间"
            width="190"
          />
          <el-table-column
            align="center"
            prop="reserveLeaveTime"
            label="预离店时间"
            width="190"
          />
          <el-table-column
            align="center"
            prop="createTime"
            label="创建时间"
            width="190"
          />
          <el-table-column align="center" label="状态">
            <template #default="{ row }">
              <div style="color: #f07218" v-if="row.orderStatus == 1">
                待支付
              </div>
              <div style="color: #09ae22" v-if="row.orderStatus == 2">
                已支付
              </div>
              <div style="color: #9bab0a" v-if="row.orderStatus == 3">
                待入住
              </div>
              <div style="color: #0b89d7" v-if="row.orderStatus == 4">
                已入住
              </div>
              <div style="color: #4d17b1" v-if="row.orderStatus == 5">
                待结账
              </div>
              <div style="color: #eb3f0a" v-if="row.orderStatus == 6">
                退款中
              </div>
              <div style="color: #84a712" v-if="row.orderStatus == 7">
                已退款
              </div>
              <div style="color: #1b14e5" v-if="row.orderStatus == 8">
                已退房
              </div>
              <div style="color: #818282" v-if="row.orderStatus == 9">
                已取消，超时
              </div>
              <div style="color: #eebe77" v-if="row.orderStatus == 10">
                已支付，未入住
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="100"
          >
            <template #default="scope">
              <div class="edit">
                <!-- 取消订单 -->
                <el-popconfirm
                  v-if="flagBtn.yichu == 1"
                  width="220"
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  :icon="InfoFilled"
                  icon-color="#f89626"
                  title="是否移出此人员?"
                  @confirm="detailFun.orderOut(scope.row)"
                >
                  <template #reference>
                    <div class="look">移出</div>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div class="pageSize">
          <span></span>
          <el-pagination
            background
            :current-page="detail.currentPage"
            :page-size="detail.pageSize"
            layout="total, prev, pager, next, jumper, slot"
            :total="detail.total"
            @update:current-page="detailFun.handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  nextTick,
  watch,
  onBeforeMount,
  onUnmounted,
} from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { Calendar } from "@element-plus/icons-vue";
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 flag = ref(true);

const changeIndex = ref(1); // 1汇总   2明细列表  切换

const flagBtn = ref(); // 按钮权限
const nowDay = ref(dayjs().format("YYYY-MM-DD HH:mm:ss"));

// 表格数据
const loading = ref(false);

// 汇总页面 （==================================================）
const collect = reactive({
  tableData: {
    list: [],
  },
  searchInput: {
    reserveName: "",
    phone: "",
    cardNumber: "",
    createTime:''
  }, // 搜索按钮数据
  currentPage: 1, // 当前页
  pageSize: 10,
  total: "", // 当前总数
  selectData: {
    list: [],
  },
});

// 明细页面  订单(===========================================)
const detail = reactive({
  tableData: {
    list: [],
  },
  searchInput: {
    reserveName: "",
    phone: "",
    cardNumber: "",
    createTime:""
  }, // 搜索按钮数据
  currentPage: 1, // 当前页
  pageSize: 10,
  total: "", // 当前总数
  selectData: {
    list: [],
  },
});

// 查看员工列表
const getList = async () => {
  loading.value = true;
  // 汇总
  if (changeIndex.value == 1) {
    let data = {
      adminId: sessionStorage.getItem("permissionSettingId"),
      page: collect.currentPage, // 当前页
      size: collect.pageSize, // 一页数据条数
      reserveName: collect.searchInput.reserveName,
      phone: collect.searchInput.phone,
      cardNumber: collect.searchInput.cardNumber,
    };
    if (collect.searchInput.createTime) {
      data.startTime = collect.searchInput.createTime[0];
      data.endTime = collect.searchInput.createTime[1];
    }

    let res = await axios({
      method: "get",
      url: api.value + "/blacklist/list",
      headers: {
        token: sessionStorage.getItem("token"),
        user_head: sessionStorage.getItem("user_head"),
      },
      params: data,
    });
    console.log(res, "黑名单汇总");
    if (res.data.code == 200) {
      collect.tableData.list = res.data.data.records;
      collect.total = res.data.data.total;
      loading.value = false;
    } else {
      loading.value = false;
      ElMessage({
        type: "error",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    }
  }
  // 明细列表  订单
  else if (changeIndex.value == 2) {
    let data = {
      page: detail.currentPage, // 当前页
      size: detail.pageSize, // 一页数据条数
      reserveName: detail.searchInput.reserveName,
      phone: detail.searchInput.phone,
      cardNumber: detail.searchInput.cardNumber,
    };
    if (detail.searchInput.createTime) {
      data.startTime = detail.searchInput.createTime[0];
      data.endTime = detail.searchInput.createTime[1];
    }

    let res = await axios({
      method: "get",
      url: api.value + "/blacklist-order/list",
      headers: {
        token: sessionStorage.getItem("token"),
        user_head: sessionStorage.getItem("user_head"),
      },
      params: data,
    });
    console.log(res, "黑名单明细列表  订单");
    if (res.data.code == 200) {
      detail.tableData.list = res.data.data.records;
      detail.total = res.data.data.total;
      loading.value = false;
    } else {
      loading.value = false;
      ElMessage({
        type: "error",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    }
  }
};

// 切换 汇总  明细列表
const changeItem = (ind) => {
  changeIndex.value = ind;
  getList();
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 汇总 （========================================================）
const collectFun = reactive({
  // 重置
  collectResetClick:()=>{
    collect.searchInput.reserveName="";
    collect.searchInput.phone="";
    collect.searchInput.cardNumber="";
    collect.searchInput.createTime="";
    searchBtn();
  },
  // 多选框功能
  handleSelectionChange: (val) => {
    console.log(val);
    collect.selectData.list = val;
  },
  // 批量移出
  detailBatchOut: async () => {
    console.log(collect.selectData.list);
    let arr = [];
    if (collect.selectData.list.length) {
      collect.selectData.list.forEach((i) => {
        arr.push(i.id);
      });
      let data = {
        ids: arr,
      };
      let res = await axios({
        method: "post",
        url: api.value + "/blacklist/delete",
        headers: {
          token: sessionStorage.getItem("token"),
          user_head: sessionStorage.getItem("user_head"),
        },
        data: data,
      });
      console.log(res, "移出");
      if (res.data.code == 200) {
        getList()
        ElMessage({
          type: "success",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
    }else{
      ElMessage({
        type: "warning",
        showClose: true,
        message: '至少选择一名人员移出',
        center: true,
      });
    }
  },
  // 移出
  collectOrderOut: async (row) => {
    console.log(row, "移出");
    let data = {
      ids: [row.id],
    };
    let res = await axios({
      method: "post",
      url: api.value + "/blacklist/delete",
      headers: {
        token: sessionStorage.getItem("token"),
        user_head: sessionStorage.getItem("user_head"),
      },
      data: data,
    });
    console.log(res, "移出");
    if (res.data.code == 200) {
      getList()
      ElMessage({
        type: "success",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    } else {
      ElMessage({
        type: "error",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    }
  },
  // 分页
  handleCurrentChange: (value) => {
    // console.log(value);
    collect.currentPage = value;
    getList();
  },
});

// 明细列表（=========================================================）
const detailFun = reactive({
  // 重置
  detailResetClick:()=>{
    detail.searchInput.reserveName="";
    detail.searchInput.phone="";
    detail.searchInput.cardNumber="";
    detail.searchInput.createTime="";
    searchBtn();
  },
  // 多选框功能
  handleSelectionChange: (val) => {
    console.log(val);
    detail.selectData.list = val;
  },
  // 批量移出
  detailBatchOut: async () => {
    console.log(detail.selectData.list);
    let arr = [];
    if (detail.selectData.list.length) {
      detail.selectData.list.forEach((i) => {
        arr.push(i.id);
      });
      let data = {
        ids: arr,
      };
      let res = await axios({
        method: "post",
        url: api.value + "/blacklist-order/delete",
        headers: {
          token: sessionStorage.getItem("token"),
          user_head: sessionStorage.getItem("user_head"),
        },
        data: data,
      });
      console.log(res, "移出");
      if (res.data.code == 200) {
        getList()
        ElMessage({
          type: "success",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
    }else{
      ElMessage({
        type: "warning",
        showClose: true,
        message: '至少选择一名人员移出',
        center: true,
      });
    }
  },
  // 移出
  orderOut: async (row) => {
    console.log(row, "移出");
    let data = {
      ids: [row.id],
    };
    let res = await axios({
      method: "post",
      url: api.value + "/blacklist-order/delete",
      headers: {
        token: sessionStorage.getItem("token"),
        user_head: sessionStorage.getItem("user_head"),
      },
      data: data,
    });
    console.log(res, "移出");
    if (res.data.code == 200) {
      getList()
      ElMessage({
        type: "success",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    } else {
      ElMessage({
        type: "error",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    }
  },
  // 分页
  handleCurrentChange: (value) => {
    // console.log(value);
    detail.currentPage = value;
    getList();
  },
});

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

onBeforeMount(async () => {
  api.value = store.state.user.api;
  const btnflag = JSON.parse(sessionStorage.getItem("btnflag"));
  flagBtn.value = btnflag.blackList;
  console.log(flagBtn.value, "按钮权限");

  flag.value = sessionStorage.getItem("isLogin");
  getList();
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  width: calc(100% - 40px);
  height: calc(100% - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;
  box-shadow: 0px 3px 10px rgba(0, 97, 255, 0.2);
  color: #84a712;
  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: rgb(0, 0, 0);
    font-size: 18px;
    font-weight: 600;

    span {
      display: inline-block;
      height: 60px;
      line-height: 60px;
      margin-right: 20px;
      cursor: pointer;
    }

    .is_active {
      color: rgba(111, 182, 184, 1);
    }
  }

  .top {
    width: 96%;
    height: 150px;
    margin: 20px auto;

    ul {
      padding: 0;
      margin: 0;
      height: 100%;
      list-style: none;
      display: flex;

      li {
        background-color: rgba(242, 242, 242, 1);
        border-radius: 10px;
        flex: 1;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        p {
          font-size: 40px;
          font-weight: 700;
          color: #000;
          margin: 0;
        }

        span {
          color: #000;
          font-size: 18px;
          font-weight: 400;
        }
      }

      li:not(:last-child) {
        margin-right: 36px;
      }
    }
  }

  .middles {
    height: calc(100% - 101px);
    display: flex;
    flex-direction: column;
  }
  //  切换 汇总 明细列表
  .changeItems {
    display: flex;
    margin: 10px 30px 0 30px;
    .changeItem {
      //   width: 80px;
      padding: 0 10px;
      height: 30px;
      background-color: rgba(230, 230, 230, 1);
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 3px;
      color: rgba(166, 166, 166, 1);
      cursor: pointer;
    }
    .changeItem_active {
      background-color: rgba(222, 234, 252, 1);
      color: rgba(0, 97, 255, 1);
    }
  }

  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;
    color: #000;

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

      .search {
        color: #fff;
      }

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

        :deep(.el-input .el-input__inner) {
          font-size: 14px;
        }

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

    .gongneng {
      margin: 10px 0 20px 0;

      span {
        color: #fff;
      }
    }
  }

  .footer {
    flex: 1;
    width: calc(100% - 60px);
    margin: 10px auto 0;
    overflow: auto;

    .el-table--fit {
      width: 100%;
      height: calc(100% - 60px);
      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 15px;

        tr {
          color: #000;
        }
      }

      :deep(.el-table__row) {
        height: 50px;
        font-size: 15px;
        color: #000;
      }

      :deep(.el-table__row):nth-child(2n) {
        .el-table-fixed-column--right {
          background-color: rgba(240, 243, 247, 1);
        }
      }

      :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(.edit) {
        display: flex;
        align-items: center;
        justify-content: center;
        color: rgba(111, 182, 184, 1);
      }

      :deep(.look) {
        padding: 0 10px;
        cursor: pointer;
        border: 1px solid transparent;
      }
    }
    .pageSize {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 13px 0;

      span {
        color: #000;
      }

      .el-pagination {
        // width: 1600px;
        :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);
          border: 1px solid rgba(0, 97, 255, 1);
          color: rgba(0, 97, 255, 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;
}

.firstLogin {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: calc(100vh);
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 1000000;
  display: flex;
  justify-content: center;
  align-items: center;

  .goInfo {
    width: 655px;
    height: 408px;
    background-color: #fff;
    border-radius: 9px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #000;

    h2 {
      margin: 0;
      font-size: 28px;
    }

    p {
      margin: 60px 0;
      padding: 0;
      font-size: 18px;
    }
  }
}
</style>
