<template>
  <div>
    <div class="titleBoxs">
      <div class="title">部门统计</div>
    </div>
    <div class="content-box-sun">
      <!-- 组织机构盒子 -->
      <div class="org-box">
        <div class="tree-box" :style="{ height: orgHeight + 'px' }">
          <MOSTY.DepartmentTree width="100%" placeholder="管理部门ID" clearable filterable v-model="listQuery.ssbmdm" />
        </div>
      </div>
      <div class="right-box-sun">
        <div class="chart-box">
          <div class="text-box">
            <div class="text-item" v-for="item in countAll" :key="item.name">
              <span class="title">{{item.name}}:</span>
              <span class="text-color">{{item.value}}</span>
              <span class="dw">{{item.dw}}</span>
            </div>
          </div>
          <div class="charesBox">
            <Pie :data="countAll" :key="keyCount"/>
          </div>
        </div>
        <!-- 表格盒子 -->
        <div class="table-box">
          <div class="searchBox" ref="searchBox">
            <el-form :model="listQuery" :inline="true">
              <el-form-item label="时间段">
                <el-date-picker
                  v-model="timeRange"
                  @change="handleTime"
                  unlink-panels
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="YYYY-M-D HH:mm:ss"
                  format="YYYY-M-D HH:mm:ss"
                />
              </el-form-item>
              <el-form-item>
                <el-button @click="handleFilter"> 查询 </el-button>
                <el-button @click="reset"> 重置 </el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="tabBox">
            <el-table
              :data="tableData"
              border
              row-key="id"
              style="width: 100%"
              :key="keyCount"
              :height="tableHeight"
              v-loading="loadingTable"
              element-loading-background="rgba(0,0,0,0.3)"
              element-loading-text="数据加载中。。"
            >
              <el-table-column label="序号" type="index" align="center" width="50"/>
              <el-table-column label="单位部门" prop="ssbm" align="center" show-overflow-tooltip />
              <el-table-column label="巡逻小组" prop="xfxz" align="center"/>
              <el-table-column label="巡逻人员" align="center" >
                <template #default="{ row }">
                  {{parseInt(row.fjsl+row.mjsl)}}
                </template>
              </el-table-column>

              <el-table-column label="巡逻车辆" prop="jcsl" align="center" />
              <el-table-column label="巡逻里程" prop="xflc" align="center" />
              <el-table-column label="巡逻时长" prop="xfsc" align="center" show-overflow-tooltip />

              <el-table-column label="操作" align="center" fixed="right" width="150px">
                <template #default="{ row }">
                  <el-button @click="detail(row)" size="small">详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="fenye" :style="{ top: tableHeight + 'px' }">
              <el-pagination
                class="pagination"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="listQuery.pageCurrent"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="listQuery.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="dialog" v-if="dialogFormVisible">
      <div class="head_box">
        <span class="title">详情</span>
        <div><el-button size="small" @click="close">关闭</el-button></div>
      </div>
      <el-form ref="elform" :model="form" :rules="rules" :inline="true" label-position="top">
        <el-form-item label="单位部门" prop="name">
          <el-input v-model="form.ssbm"></el-input>
        </el-form-item>
        <el-form-item label="巡逻小组" prop="bz1">
          <el-input v-model="form.xfxz"></el-input>
        </el-form-item>
        <el-form-item label="巡逻民警" prop="bz2">
          <el-input v-model="form.mjsl"></el-input>
        </el-form-item>
        <el-form-item label="巡逻辅警" prop="bz2">
          <el-input v-model="form.fjsl"></el-input>
        </el-form-item>
        <el-form-item label="巡逻车辆" prop="bz3">
          <el-input v-model="form.jcsl"></el-input>
        </el-form-item>
        <el-form-item label="巡逻里程" prop="bz4">
          <el-input v-model="form.xflc"></el-input>
        </el-form-item>
        <el-form-item label="巡逻时长（小时）" prop="bz5">
          <el-input v-model="form.xfsc"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { qcckPost, qcckGet } from "@/api/qcckApi.js";
import * as MOSTY from "@/components/MyComponents/index";
import Pie from "./components/pie.vue";
import { selectDeptPage } from "@/api/user-manage";
import { ref, getCurrentInstance, onMounted, reactive, watch } from "vue";
const { proxy } = getCurrentInstance();
const timeRange = ref([]) //
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 10,

}); //搜索表单
const tableData = ref([]); //表格数据
const total = ref(0);
const keyCount = ref(0); //tabel组件刷新值
const orgHeight = ref();// 表格高度计算
const dialogFormVisible = ref(false);
const form = ref({}); //新增表单
const elform = ref(null); //新增表单容器
//表单验证
const rules = ref({
  name: [{ required: true, message: "请输入名称", trigger: "change" }]
});
const searchBox = ref(null); //搜索盒子
const loadingTable = ref(true); //表格加载状态
const tableHeight = ref(); // 表格高度
const countAllDefault = ref([])
const countAll = ref([
  {name:'巡逻小组',type:'xfxz',value:0,dw:'个'},
  {name:'巡逻民警',type:'mjsl',value:0,dw:'人'},
  {name:'巡逻辅警',type:'fjsl',value:0,dw:'人'},
  {name:'巡逻车辆',type:'jcsl',value:0,dw:'辆'},
  {name:'巡逻里程',type:'xflc',value:0,dw:'KM'},
  {name:'巡逻时长',type:'xfsc',value:0,dw:'小时'},
])
watch(
  () => listQuery.value.ssbmdm,
  (val) => {
    if(val) {
      getListData();
      getDate() //获取全部
    }
  },
  {
    immediate: true,
    deep: true
  }
);

onMounted(() => {
  countAllDefault.value = JSON.parse(JSON.stringify(countAll.value))
  getListData();
  getDate() //获取全部
  tabHeightFn();
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});

// 获取全部数据
function getDate(){
    qcckPost({ssbmdm:listQuery.value.ssbmdm},'/mosty-qwzx/tbQwXfbb/bmtjAll').then(res=>{
      countAll.value = JSON.parse(JSON.stringify(countAllDefault.value))
      if(res){
        countAll.value.forEach(item=>{
          for(let key in res){
            if(key == item.type) item.value = parseInt(res[key])
            if(key == 'xflc') item.value = (item.value/1000).toFixed(2)
            if(key == 'xfsc') item.value = (item.value/3600).toFixed(2)
          }
        })
      }
    })
}

//处理时间
function handleTime(val){
  listQuery.value.kssj = val ? val[0] : '';
  listQuery.value.jssj = val ? val[1] : '';
}

//点击查询
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};
//点击重置
const reset = () => {
  listQuery.value = {pageCurrent: 1,pageSize: 10};
  timeRange.value = []
  getListData();
};

// 获取列表
function getListData() {
  loadingTable.value = true;
  qcckPost(listQuery.value,'/mosty-qwzx/tbQwXfbb/bmtj').then(res=>{
    tableData.value = res.records;
    tableData.value.forEach(item=>{
      item.xflc = item.xflc ? (parseInt(item.xflc)/1000).toFixed(2) : 0
      item.xfsc = item.xfsc ? (parseInt(item.xfsc)/3600).toFixed(2) : 0
    })
    total.value = res.total;
    loadingTable.value = false;
  }).catch(()=>{ loadingTable.value = false; })
}

// pageSize 改变触发
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

// 页码改变触发
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};

const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - 590;
  orgHeight.value = window.innerHeight - 192;
  window.onresize = function () { tabHeightFn(); };
};

//关闭弹窗
function close() {
  dialogFormVisible.value = false;
  form.value = {};
}

//修改
function detail(row) {
  form.value = JSON.parse(JSON.stringify(row));
  dialogFormVisible.value = true;
}


</script>
<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
.chart-box {
  height: 280px;
  display: flex;
  justify-content: center;
  align-items: center;
  .text-box {
    height: 160px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-right: 10px;
  }
  .charesBox{
    flex: 1;
    height: 100%;
  }
}
.text-item {
  width: 180px;
  display: flex;
  justify-content: space-between;
  .title{
    width: 75px;
  }
  .text-color {
    color: rgb(181, 1, 1);
    text-align: center;
  }
  .dw{
    width: 30px;
  }
}
</style>
