<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="regulation">
      <div class="middle">
        <div class="filter">
          <div class="condition">
            <span>关键字&nbsp;&nbsp;</span>
            <el-input
              :clearable="true"
              @clear="searchBtn"
              v-model.trim="searchInput.keyWord"
              class="w-50 m-2"
              placeholder="请输入关键字查询"
              style="width: 180px"
            />
          </div>
          <div class="condition">
            <span>创建时间&nbsp;&nbsp;</span>
            <el-date-picker
              v-model="searchInput.createTime"
              type="daterange"
              unlink-panels
              range-separator="-"
              start-placeholder="起始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              :prefix-icon="Calendar"
              placeholder="请选择日期"
              @change="searchBtn"
            />
          </div>
          <div class="condition">
            <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
              style="margin-left: 20px"
              color="rgba(41, 109, 227, 1)"
              type="primary"
              plain
              @click="importExcel"
              v-if="flagBtn.daochu"
            >
              导出</el-button
            >
          </div>
        </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" type="selection" width="80" /> -->
          <el-table-column
            width="150"
            align="center "
            type="index"
            label="序号"
          />
          <el-table-column
            align="center"
            prop="orderNumber"
            width="180"
            label="订单编号"
          >
            <template #default="{ row }">
              {{ row.orderNumber }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="reserveName" label="入住人" />
          <el-table-column align="center" prop="houseName" label="房型">
            <template #default="{ row }">
             <span>{{ row.roomName }}</span> <span style="color:rgba(30, 125, 251,1)">( {{ row.roomType==1?'全':'钟' }} )</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="roomNumber" label="房号" />
          <el-table-column
            align="center"
            prop="payPrice"
            v-if="changeIndex == 1"
            label="订单金额(元)"
          />
          <el-table-column
            align="center"
            prop="payPrice"
            v-if="changeIndex == 2"
            label="待结账(元)"
          />
          <el-table-column
            align="center"
            prop="payTime"
            v-if="changeIndex == 1"
            label="支付时间"
            width="180"
          />
          <el-table-column
            align="center"
            prop="orderStatus"
            label="订单状态"
            width="160"
          >
            <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"
            prop="createTime"
            label="创建时间"
            width="180"
          />
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="150"
            v-if="flagBtn.xiangqing==1"
          >
            <template #default="scope">
              <div class="look"  @click="clickOrderInfo(scope.row)">详情</div>
            </template>
          </el-table-column>
        </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>

    <!-- 详情弹窗区域 -->
    <el-dialog
      class="orderInfo"
      title="订单详情"
      v-model="orderInfoVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      align-center
      width="850"
      :before-close="orderInfoClose"
    >
      <div class="userinfo">用户信息</div>
      <div class="info">
        <div class="user">
          <div class="name">姓名</div>
          <el-input v-model="order.userName" placeholder=""></el-input>
        </div>
        <div class="user">
          <div class="name">联系方式</div>
          <el-input v-model="order.userPhone"></el-input>
        </div>
      </div>
      <div class="info">
        <div class="user">
          <div class="name">订单号</div>
          <el-input v-model="order.orderNo"></el-input>
          <!-- <el-tooltip placement="top" :content="order.orderNo">
                <el-input v-model="order.orderNo"></el-input>
              </el-tooltip> -->
        </div>
        <div class="user">
          <div class="name">支付金额（元）</div>
          <el-input v-model="order.allOfReceiveAmount"></el-input>
        </div>
      </div>
      <div class="info">
        <div class="user">
          <div class="name">入住时间</div>
          <el-input v-model="order.realStartTime"></el-input>
        </div>
        <div class="user">
          <div class="name">离住时间</div>
          <el-input v-model="order.realEndTime"></el-input>
        </div>
      </div>
      <div class="info">
        <div class="user">
          <div class="name">入住天数</div>
          <el-input v-model="order.housDay"></el-input>
        </div>
        <div class="user">
          <div class="name">支付时间</div>
          <el-input v-model="order.payTime"></el-input>
        </div>
      </div>

      <!-- 水费区域 -->
      <div class="rate">水费明细</div>
      <div class="water">
        表计：{{ order.watermeter }} &nbsp;&nbsp;&nbsp;楼层：{{
          order.floor
        }}
        &nbsp;&nbsp;&nbsp;抄表时间：{{ order.realStartTime }}
      </div>
      <el-table
        :data="waterTable.list"
        max-height="93"
        style="width: 832px"
        stripe
        :cell-style="rowbg"
        :header-cell-style="{
          color: ' rgba(0, 0, 0, 1)',
          background: 'rgba(240, 243, 247, 1)',
        }"
      >
        <el-table-column
          prop="waterVolume"
          align="center"
          label="用水量（吨）"
        ></el-table-column>
        <el-table-column
          prop="priceOfWater"
          align="center"
          label="水价（元）"
        ></el-table-column>
        <el-table-column
          prop="allowance"
          align="center"
          label="补助量（吨）"
        ></el-table-column>
        <el-table-column
          prop="cost"
          align="center"
          label="产生水费（元）"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>

      <!-- 电费区域 -->
      <div class="rate">电费明细</div>
      <div class="water">
        表计：{{ order.elemeter }} &nbsp;&nbsp;&nbsp;楼层：{{
          order.floor
        }}
        &nbsp;&nbsp;&nbsp;抄表时间：{{ order.realStartTime }}
      </div>
      <el-table
        :data="electricTable.list"
        max-height="93"
        style="width: 832px"
        stripe
        :cell-style="rowbg"
        :header-cell-style="{
          color: ' rgba(0, 0, 0, 1)',
          background: 'rgba(240, 243, 247, 1)',
        }"
      >
        <el-table-column
          prop="eleVolume"
          align="center"
          label="用电量（度）"
        ></el-table-column>
        <el-table-column
          prop="priceOfEle"
          align="center"
          label="电价（元）"
        ></el-table-column>
        <el-table-column
          prop="allowance"
          align="center"
          label="补助量（度）"
        ></el-table-column>
        <el-table-column
          prop="cost"
          align="center"
          label="产生电费（元）"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
    </el-dialog>
  </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 changeIndex = ref(1); // 1支付成功订单  2待结账 切换

const flagBtn = ref(); // 按钮权限

const searchInput = reactive({
  keyWord: "", // 关键字
  createTime: "",
}); // 搜索按钮数据

// 待结账 (---------------------------------------------------------------------)
const accounts = reactive({
  keyWord: "", // 关键字
  createTime: "", // 创建时间
});

const nowDay = ref(dayjs().format("YYYY-MM-DD HH:mm:ss"));
// 表格数据
const loading = ref(false);
const tableData = reactive({
  list: [],
});

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(10); // 当前总数

// 详情
const orderInfoVisible = ref(false); // 详情弹窗
const order = reactive({
  userName: "", // 姓名
  userPhone: "", // 联系方式
  orderNo: "", // 订单号
  allOfReceiveAmount: "", // 支付金额（元）
  realStartTime: "", // 入住时间
  realEndTime: "", // 离住时间
  housDay: "", // 入住天数
  payTime: "", // 支付时间
  floor: "", // 楼层
  watermeter: "", // 水表计
  elemeter: "", // 电表计
});
const waterTable = reactive({
  list: [
    {
      waterVolume: "",
      priceOfWater: "",
      allowance: "",
      cost: "",
    },
  ],
}); // 水费明细
const electricTable = reactive({
  list: [
    {
      eleVolume: "",
      priceOfEle: "",
      allowance: "",
      cost: "",
    },
  ],
}); // 电费明细

// 切换 支付成功订单 待结账
const changeItem = (ind) => {
  changeIndex.value = ind;
  getList();
};

// 查看员工列表
const getList = async () => {
  loading.value = true;
  let data = {
    page: currentPage.value, // 当前页
    size: pageSize.value, // 一页数据条数
    permissionSettingId: sessionStorage.getItem("permissionSettingId"),
    keyWord: searchInput.keyWord,
    state: changeIndex.value,
  };
  if (searchInput.createTime) {
    data.startTime = searchInput.createTime[0];
    data.endTime = searchInput.createTime[1];
  }
  let res = await axios({
    method: "get",
    url: api.value + "/house-order/reportStatistics",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("user_head"),
    },
    params: data,
  });
  console.log(res, "用户列表");
  if (res.data.code == 200) {
    tableData.list = res.data.data.list;
    total.value = res.data.data.totalCount;
    loading.value = false;
  } else {
    loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

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

// 详情弹窗 (----------------------------------------------)
const clickOrderInfo = async (row) => {
  orderInfoVisible.value = true;
  order.floor = row.roomName + " " + row.roomNumber;
  console.log(row);
  let data = {
    orderNumber: row.orderNumber,
    adminId: sessionStorage.getItem("permissionSettingId"),
  };
  let res = await axios({
    method: "get",
    url: api.value + "/house-order/particulars",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("user_head"),
    },
    params: data,
  });
  console.log(res, "详情信息");
  if (res.data.code == 200) {
    let data = res.data.data.houseOrder;
    order.userName = data.reserveName;
    order.userPhone = data.reservePhone;
    order.orderNo = data.orderNumber;
    order.allOfReceiveAmount = data.payPrice;
    order.realStartTime = data.reserveLiveTime;
    order.realEndTime = data.reserveLeaveTime;
    order.housDay = data.liveDay;
    order.payTime = data.payTime;
    order.watermeter = res.data.data.waterEquipmentMeterName;
    order.elemeter = res.data.data.electricEquipmentMeterName;
    waterTable.list = [
      {
        waterVolume: data.waterConsume, // 用水量（吨）
        priceOfWater: 2.1, // 水价（元）
        allowance: res.data.data.freeQuotaOfWater, // 补助量（吨）
        cost: data.waterCost, // 产生水费（元）
      },
    ];
    electricTable.list = [
      {
        eleVolume: data.electricConsume, // 用电量（吨）
        priceOfEle: 0.6, // 电价（元）
        allowance: res.data.data.freeQuotaOfElectric, // 补助量（吨）
        cost: data.electricCost, // 产生电费（元）
      },
    ];
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
// 关闭详情弹窗
const orderInfoClose = () => {
  orderInfoVisible.value = false;
};

// 表格斑马纹颜色修改
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();
};
//导出功能
const importExcel = lodash.debounce(async () => {
  let data = {
    permissionSettingId: sessionStorage.getItem("permissionSettingId"),
    keyWord: searchInput.keyWord,
    state: changeIndex.value,
  };
  if (searchInput.createTime) {
    data.startTime = searchInput.createTime[0];
    data.endTime = searchInput.createTime[1];
  }
  let res = await axios({
    method: "get",
    url: api.value + "/house-order/reportStatisticsExport",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
    responseType: "blob",
  });
  console.log(res, "统计报表导出");
  if (res.status == 200) {
    let name = `统计报表`;
    var content = res.data;
    var datas = new Blob([content]);
    var downloadUrl = window.URL.createObjectURL(datas);
    var anchor = document.createElement("a");
    anchor.href = downloadUrl;
    anchor.download = name + ".xlsx";
    anchor.click();
    window.URL.revokeObjectURL(datas);
    ElMessage({
      type: "success",
      showClose: true,
      message: "导出成功",
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
}, 1000);

onBeforeMount(async () => {
  api.value = store.state.user.api;
  const btnflag = JSON.parse(sessionStorage.getItem("btnflag"));
  flagBtn.value = btnflag.statement;
  console.log(flagBtn.value, "按钮权限");
  changeItem(changeIndex.value);
});
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);

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

  //   切换 申请记录 规则设置
  .changeItems {
    display: flex;
    margin: 15px 0 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;
      margin: 10px 0 0 0;

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

    .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;
    }
  }
  //   待结账页面样式
  .regulation {
    height: calc(100% - 106px);
    overflow: auto;
    .condition {
      color: #000;
      margin: 20px 30px;
      font-size: 14px;
    }
    .hint {
      color: rgba(212, 48, 48, 1);
    }
  }
  .footer {
    width: calc(100% - 60px);
    height: calc(100% - 132px);
    margin: 10px auto 0;

    .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;

        .orderNum {
          color: rgba(111, 182, 184, 1);
          cursor: pointer;
        }
      }

      :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(.look) {
        color: rgba(0, 97, 255, 1);
        cursor: pointer;
      }
    }
  }
  //   订单详情
  :deep(.orderInfo) {
    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: 0 38px;
      height: 750px;
      overflow: auto;

      .userinfo {
        font-size: 18px;
        font-weight: 500;
        color: rgba(0, 0, 0, 1);
        text-align: left;
        margin-top: 15px;
      }

      .info {
        display: flex;

        .user {
          margin-right: 120px;
          .name {
            text-align: left;
            padding: 12px 0;
          }

          .el-input {
            width: 283px;
            // height: 30px;
            opacity: 1;
            // margin-right: 81px;
            // .el-input__inner {
            //   color: #000;
            // }
          }
        }
      }

      .rate {
        font-size: 18px;
        font-weight: 500;
        color: rgba(0, 0, 0, 1);
        text-align: left;
        margin: 15px 0;
      }

      .water {
        text-align: left;
        font-size: 16px;
        font-weight: 400;
        color: rgba(0, 0, 0, 1);
      }

      .el-table {
        margin: 10px 0;

        tr {
          height: 48px;
        }

        .el-table__body-wrapper {
          overflow: visible;
        }
      }
    }
  }

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

    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;
      }
    }
  }
}
</style>
