<template>
  <div>
    <div class="titleBox">
      <PageTitle title="任务中心" />
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch"></Search>
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable :tableData="pageData.tableData" :tableColumn="pageData.tableColumn" :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount" :tableConfiger="pageData.tableConfiger" :controlsWidth="pageData.controlsWidth">
        <!-- 操作 -->
        <template #controls="{ row }">
          <!-- <el-link type="primary" @click="addEdit('edit', row)">详情</el-link> -->
          <el-link type="primary" @click="openDetail( row)">详情</el-link>
          <!-- <el-link type="danger" @click="handleDelete([row.id])">删除</el-link> -->
        </template>
      </MyTable>
      <Pages @changeNo="changeNo" @changeSize="changeSize" :tableHeight="pageData.tableHeight" :pageConfiger="{
        ...pageData.pageConfiger,
        total: pageData.total
      }"></Pages>
    </div>
    <!-- 下发任务 -->
    <XfrwDialog ref="xfDiloag" @refresh="getList"></XfrwDialog>
  </div>
<AddForm ref="addForm"/>
</template>

<script setup>
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import XfrwDialog from "./components/xfrwDialog.vue";
import { qcckGet, qcckPost, qcckDelete } from "@/api/qcckApi.js";
import  AddForm from "./components/addForm.vue";
import { reactive, ref, onMounted, getCurrentInstance, nextTick } from "vue";
const { proxy } = getCurrentInstance();
const xfDiloag = ref();
const searchBox = ref(); //搜索框
const searchConfiger = ref([
  {
    label: "任务标题",
    prop: "rwbt",
    placeholder: "请输入任务标题",
    showType: "input"
  },
  {
    label: "任务时间",
    prop: "datetime",
    showType: "datetimerange"
  },
]);
const queryFrom = ref({});
const pageData = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 5,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "任务标题", prop: "rwbt" },
    { label: "任务开始时间", prop: "rwkssj" },
    { label: "任务结束时间", prop: "rwjssj" },
    { label: "巡逻线名称", prop: "xlmc" },
    { label: "负责人姓名", prop: "fzrxm" },
  ]
});




// 搜索
const onSearch = (val) => {
  queryFrom.value = { ...val };
  queryFrom.value.startTime = val.datetime ? val.datetime[0] : '';
  queryFrom.value.endTime = val.datetime ? val.datetime[1] : '';
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};

const changeNo = (val) => {
  pageData.pageConfiger.pageNum = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};

// 获取列表
const getList = () => {
  pageData.tableConfiger.loading = true;
  let data = { ...pageData.pageConfiger, ...queryFrom.value };
  delete data.datetime;
  qcckGet(data, "/mosty-jmxf/bxxRwxx/selectPage").then((res) => {
    pageData.tableData = res.records || [];
    pageData.total = res.total;
    pageData.tableConfiger.loading = false;
  }).catch(() => {
    pageData.tableConfiger.loading = false;
  });
};

// 详情
const addEdit = (type, row) => {
  nextTick(() => {
    xfDiloag.value.init(type, row);
  });
};

// 删除
const handleDelete = (ids) => {
  proxy.$confirm("确认删除该记录吗?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning"}).then(() => {
    qcckPost({ids}, "/mosty-jmxf/bxxRwxx/delete").then((res) => {
      proxy.$message({ type: "success", message: "删除成功!"  });
      getList();
    }).catch(() => {
      pageData.tableConfiger.loading = false;
    });
  });
};


// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight =
    window.innerHeight - searchBox.value.offsetHeight - 250;
  window.onresize = function () {
    tabHeightFn();
  };
};
// 查看详情
const addForm = ref()
// 打开详情
const openDetail = (row) => {
  addForm.value.init("detail", row);
};


onMounted(() => {
  getList();
  tabHeightFn();
});

</script>

<style>
.el-loading-mask {
  background: rgba(0, 0, 0, 0.5) !important;
}
</style>
