<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">台账管理</span>
    </div>
    <div>
      <div class="top">
        <ul>
          <li>
            <p>{{ pendingOrderSum }}</p>
            <span>订单总额（元）</span>
          </li>
          <li>
            <p>{{ orderSum }}</p>
            <span>支付总额（元）</span>
          </li>
          <li>
            <p>{{ consumerOrderSum }}</p>
            <span>退款总额（元）</span>
          </li>
          <li>
            <p>{{ sumAccount }}</p>
            <span>入账总额（元）</span>
          </li>
        </ul>
      </div>
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>关键字 : </span>
            <el-input
              :clearable="true"
              @clear="searchBtn"
              v-model="searchInput.keyWord"
              class="w-50 m-2"
              placeholder="请输入订单、预定人、手机号"
              style="width: 220px"
            />
          </div>
          <div class="condition">
            <span>支付时间 : </span>
            <el-date-picker
              v-model="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: 230px"
            />
          </div>
          <div class="condition">
            <span>退房时间 : </span>
            <el-date-picker
              v-model="searchInput.checkOutTime"
              type="daterange"
              range-separator="-"
              start-placeholder="起始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :prefix-icon="Calendar"
              placeholder="请选择日期"
              style="width: 230px"
            />
          </div>
          <div class="condition">
            <span>清账状态 : </span>
            <el-select
              :clearable="false"
              v-model="searchInput.status"
              class="m-2"
              placeholder="请选择清账状态"
              style="width: 200px"
            >
              <el-option label="全部" :value="2" />
              <el-option label="未清账" :value="0" />
              <el-option label="已清账" :value="1" />
            </el-select>
          </div>

          <el-button
            style="margin-left: 20px"
            color="rgba(9, 101, 98, 1)"
            type="primary"
            class="search"
            @click="searchBtn"
            ><el-icon>
              <Search />
            </el-icon>
            <span>查询</span></el-button
          >
          <el-button
            style="margin-left: 20px"
            color="rgba(9, 101, 98, 1)"
            type="primary"
            class="search"
            @click="importExcel"
            ><span>导出订单</span></el-button
          >
        </div>
      </div>
      <div class="footer" v-loading="loading">
        <!-- <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <el-table-column
            align="center"
            prop="orderNum"
            label="已消费订单号"
          />
          <el-table-column align="center" prop="userName" label="预订人" />
          <el-table-column align="center" prop="userPhone" label="手机号码" />
          <el-table-column
            align="center"
            prop="houseOrderNumber"
            label="房间数(间)"
          />
          <el-table-column
            align="center"
            prop="payAccount"
            label="订单金额(元)"
          />
          <el-table-column align="center" prop="payTime" label="支付时间" />
        </el-table> -->
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <el-table-column align="center" prop="createTime" label="交易时间" />
          <el-table-column
            align="center"
            width="180"
            prop="orderNum"
            label="订单号"
          />
          <el-table-column
            align="center"
            width="60"
            prop="userName"
            label="预订人"
          />
          <el-table-column
            align="center"
            width="120"
            prop="userPhone"
            label="手机号码"
          />
          <el-table-column
            align="center"
            width="100"
            prop="orderAccount"
            label="订单金额(元)"
          />
          <el-table-column
            align="center"
            width="100"
            prop="payAccount"
            label="支付金额(元)"
          />
          <el-table-column
            align="center"
            width="100"
            prop="refundAmount"
            label="退款金额(元)"
          />
          <el-table-column
            align="center"
            width="100"
            prop="inAccount"
            label="入账金额(元)"
          />
          <el-table-column align="center" prop="payTime" label="支付时间" />
          <el-table-column
            align="center"
            prop="checkOutTime"
            label="退房时间"
          />
          <el-table-column
            align="center"
            width="100"
            prop="saccountState"
            label="清账状态"
          >
            <template #default="{ row }">
              <div
                class="saccountState"
                v-if="row.saccountState == 1"
                style="color: #000"
              >
                已清账
              </div>
              <div
                class="saccountState"
                v-if="row.saccountState == 0"
                style="color: rgba(212, 48, 48, 1)"
              >
                未清账
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="saccountTime"
            label="清账时间"
          />
        </el-table>
      </div>

      <!-- 分页组件 -->
      <div class="pageSize">
        <span></span>
        <el-pagination
          background
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper, slot"
          :total="total"
          @update:current-page="handleCurrentChange"
        />
      </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 loading = ref(false);
const tableData = reactive({
  list: [
    // {
    //   userName: "111233342",
    //   userName2: "张三",
    //   userPhone: "1121311",
    //   teamName: 12,
    //   schoolName: 1200,
    //   workTypeName: "2023-08-01 12:09:08",
    // },
    // {
    //   userName: "111233342",
    //   userName2: "张三",
    //   userPhone: "1121311",
    //   teamName: 12,
    //   schoolName: 1200,
    //   workTypeName: "2023-08-01 12:09:08",
    // },
    // {
    //   userName: "111233342",
    //   userName2: "张三",
    //   userPhone: "1121311",
    //   teamName: 12,
    //   schoolName: 1200,
    //   workTypeName: "2023-08-01 12:09:08",
    // },
    // {
    //   userName: "111233342",
    //   userName2: "张三",
    //   userPhone: "1121311",
    //   teamName: 12,
    //   schoolName: 1200,
    //   workTypeName: "2023-08-01 12:09:08",
    // },
    // {
    //   userName: "111233342",
    //   userName2: "张三",
    //   userPhone: "1121311",
    //   teamName: 12,
    //   schoolName: 1200,
    //   workTypeName: "2023-08-01 12:09:08",
    // },
  ],
});

const searchInput = reactive({
  keyWord: "",
  teamId: "",
  schoolId: "",
  createTime: "",
  status: 2,
  checkOutTime: "",
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(7);
const total = ref(); // 当前总数
const selectData = reactive({ list: [] }); // 多选框选择的数据
const pendingOrderSum = ref(); // 订单总额（元）
const orderSum = ref(); // 支付总额（元）
const consumerOrderSum = ref(); // 退款总额（元）
const sumAccount = ref(); // 入账总额（元）
// 表单验证
const rules = reactive({
  // serial: [{ required: true, message: "序列号不能为空", trigger: "blur" }],
  userName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
  cardNumber: [
    { required: true, message: "微校卡号不能为空", trigger: "blur" },
  ],
  userzzId: [{ required: true, message: "身份不能为空", trigger: "blur" }],
  userPhone: [{ required: true, message: "手机号码不能为空", trigger: "blur" }],
  schoolId: [{ required: true, message: "校区不能为空", trigger: "blur" }],
  workType: [{ required: true, message: "工种不能为空", trigger: "blur" }],
  teamId: [{ required: true, message: "维修班不能为空", trigger: "blur" }],
  articleIds: [
    { required: true, message: "关联报修类型不能为空", trigger: "blur" },
  ],
  buildIds: [{ required: true, message: "关联楼栋不能为空", trigger: "blur" }],
  acceptanceTime: [
    { required: true, message: "接单考核时间不能为空", trigger: "blur" },
  ],
  maintenanceTime: [
    { required: true, message: "维修考核时间不能为空", trigger: "blur" },
  ],
});

watch(
  () => searchInput.createTime,
  (newVal, oldVal) => {
    console.log("支付时间:", newVal);
    if (newVal == null) {
      getList();
    }
  }
);
watch(
  () => searchInput.checkOutTime,
  (newVal, oldVal) => {
    console.log("退房时间:", newVal);
    if (newVal == null) {
      getList();
    }
  }
);
// 头部数据
const headerData = async () => {
  let data = new FormData();
  data.set("ledgerParam", searchInput.keyWord);
  data.set("managerId", sessionStorage.getItem("token"));
  data.set("saccountState", searchInput.status);
  if (searchInput.status == 2) {
    data.set("saccountState", "");
  }
  if (searchInput.createTime) {
    data.set("payStartTime", searchInput.createTime[0]);
    data.set("payEndTime", searchInput.createTime[1]);
  }
  if (searchInput.checkOutTime) {
    data.set("leaveStartTime", searchInput.checkOutTime[0]);
    data.set("leaveEndTime", searchInput.checkOutTime[1]);
  }
  let res = await axios({
    method: "post",
    url: api.value + "/mhotel/bookqueryLedgerTj.action",
    headers: {},
    data: data,
  });
  console.log(res, "头部数据");
  if (res.data.code == 200) {
    pendingOrderSum.value = res.data.orderT.tj;
    orderSum.value = res.data.payT.tj;
    consumerOrderSum.value = res.data.refundT.tj;
    sumAccount.value = res.data.inT.tj;
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 查看员工列表
const getList = async () => {
  loading.value = true;
  let data = {
    page: currentPage.value,
    rows: pageSize.value,
    ledgerParam: searchInput.keyWord, // 关键字查询
    managerId: sessionStorage.getItem("token"),
    saccountState: searchInput.status,
  };
  if (searchInput.status == 2) {
    data.saccountState = "";
  }
  if (searchInput.createTime) {
    data.payStartTime = searchInput.createTime[0];
    data.payEndTime = searchInput.createTime[1];
  }
  if (searchInput.checkOutTime) {
    data.leaveStartTime = searchInput.checkOutTime[0];
    data.leaveEndTime = searchInput.checkOutTime[1];
  }
  headerData(); // 调用头部数据
  let res = await axios({
    method: "post",
    url: api.value + "/mhotel/bookquearyLedgerPage.action",
    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.bookIPage.pageList;
    total.value = res.data.data.bookIPage.total;

    // pendingOrderSum.value = res.data.data.pendingOrderSum;
    // orderSum.value = res.data.data.orderSum;
    // consumerOrderSum.value = res.data.data.consumerOrderSum;
    // sumAccount.value = res.data.data.sumAccount;
    loading.value = false;
    // ElMessage({
    //   type: "success",
    //   showClose: true,
    //   message: res.data.message,
    //   center: true,
    // });
  } else {
    loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

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

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};
//导出功能
const importExcel = lodash.debounce(async () => {
  let data = {
    ledgerParam: searchInput.keyWord, // 关键字查询
    managerId: sessionStorage.getItem("token"),
    saccountState: searchInput.status,
  };
  if (searchInput.status == 2) {
    data.saccountState = "";
  }
  if (searchInput.createTime) {
    data.payStartTime = searchInput.createTime[0];
    data.payEndTime = searchInput.createTime[1];
  }
  if (searchInput.checkOutTime) {
    data.leaveStartTime = searchInput.checkOutTime[0];
    data.leaveEndTime = searchInput.checkOutTime[1];
  }
  let res = await axios({
    method: "post",
    url: api.value + "/mhotel/booktoLedgerExcel.action",
    headers: {
      // token: sessionStorage.getItem("token"),
    },
    params: data,
  });
  console.log(res, "导出台账管理");
  if (res.data.code == 200) {
    var downloadPath = res.data.downurl;
    // window.open(downloadPath);
    window.location.href = downloadPath;
    var downloadLink = document.createElement("a");
    downloadLink.style.display = "none"; // 使其隐藏
    downloadLink.href = downloadPath;
    downloadLink.download = "";
    downloadLink.click();
    document.body.appendChild(downloadLink);
    document.body.removeChild(downloadLink);
    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 tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 分页
const handleCurrentChange = (value) => {
  // console.log(value);
  currentPage.value = value;
  getList();
};

onBeforeMount(async () => {
  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;
  box-shadow: 0px 3px 10px rgba(0, 97, 255, 0.2);

  .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 {
      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;
      }
    }
  }
  .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: 15px 0 15px 0;

      .search {
        color: #fff;
      }

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

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

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

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

      span {
        color: #fff;
      }
    }

    :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: 400px;
    margin: 10px auto 30px;

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

      :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;
      }

      :deep(.look):hover {
        color: red;
      }
    }

    // 添加员工弹窗样式
    :deep(.addStaff) {
      //   height: 420px;
      border-radius: 11px;

      .el-dialog__header {
        border-radius: 11px 11px 0 0;
        background: rgba(237, 241, 245, 1);
        font-weight: 600;
        margin: 0;

        .el-dialog__headerbtn {
          outline: none;
        }
      }

      .el-dialog__body {
        padding: 30px 20px 10px 20px;

        .el-input {
          width: 200px;
        }

        // 关联报修类型样式（-----------------）
        // .checkbox {
        //   .el-form-item__content {
        //     display: flex;
        //     flex-direction: column;
        //     align-items: flex-start;
        //     .el-checkbox-group {
        //       display: flex;
        //       flex-direction: column;
        //     }
        //   }
        // }

        .options {
          .el-form-item__content {
            .queding {
              margin-left: 20px;
              color: #fff;
            }

            display: flex;
            flex-direction: row-reverse;
          }
        }
      }
    }
  }

  .pageSize {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 30px;

    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);
      }

      :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>
