<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group @search="changeSearch" v-model="filter.search" placeholder="请输入关键字"> </bg-filter-group>
      <div class="table_container">
        <div class="table bg-scroll">
          <bg-table ref="dataTable" :headers="headers" :rows="tableRows" :stripe="true">
            <template v-slot:name="{ row }">
              <span class="can_click_text" @click="getChildren(row)">
                {{ row.name }}
              </span>
            </template>
            <template v-slot:state="{ row }">
              <span> 执行中 </span>
            </template>
          </bg-table>
        </div>
        <bg-pagination
          :page="filter.page"
          :size="filter.size"
          :total="tableTotal"
          @change-page="changePage"
          @change-size="changeSize">
        </bg-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onBeforeMount, toRefs, computed, watch, nextTick, watchEffect } from "vue";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";
import { useRouter, useRoute } from "vue-router";
import { Search } from "@element-plus/icons-vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";

const router = useRouter();
const route = useRoute();

const bgForm = ref(null);
const dataTable = ref(null);
const headers = [
  {
    label: "状态",
    prop: "state",
  },
  {
    label: "任务名称",
    prop: "name",
  },
  {
    label: "执行耗时",
    prop: "workTime",
  },
  {
    label: "操作人",
    prop: "person",
  },
];

const state = reactive({
  bgForm,
  typeList: [], // 分类数据
  typeKeyword: "", // 分类删选关键词
  nodeClassifyId: null, // 当前选中分类的uuid  用于新增字典
  nodeId: null, // 当前选中分类的id  用于请求列表
  timer: null, // 定时器
  tableRows: [], // 表格数据
  selected: [], //选择数据
  tableTotal: 0, // 表格数据条数
  filter: {
    time: "",
    search: "",
    page: 1,
    limit: 10,
  }, // 表格筛选项
  actionRow: null, // 当前操作的数据
  dialogDelete: false, // 删除弹窗
  addType: 0, //
  addDialog: false,
  formData: {
    name: "",
    describe: "",
    state: 1,
    p_dict_id: "",
  },
  rules: {
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    describe: [
      { required: true, message: "请输入描述", trigger: "blur" },
      { max: 200, message: "描述最大为200字", trigger: "blur" },
    ],
    state: [{ required: true, message: "请选择是否启用", trigger: "change" }],
  },
  fatherRow: null,
});

const selectRows = (data) => {
  state.selected = data.selection;
};

const clearSelected = () => {
  dataTable.value.clearTable();
};

const deleteAllTips = () => {};

const getChildren = (row) => {
  router.push(`/auto-maintenance/task-history/list`);
};

const getTypeList = () => {
  let params = {
    name: state.typeKeyword,
  };
  axios
    .get(`/apaas/system/v5/dictionary/classify/list`, { params })
    .then((res) => {
      if (res.data.code == 200) {
        state.typeList = res.data.data || [];
        state.nodeClassifyId = state.typeList[0].classify_id || null;
        state.nodeId = state.typeList[0].id || null;
        if (state.nodeId) {
          getTableRows();
        }
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
}; // 获取字典分类

const changeSearch = (val) => {
  state.filter.search = val;
  changePage(1);
}; // 表格关键字筛选

const filterAction = () => {
  changePage(1);
}; // 查询按钮

const filterClear = () => {
  state.filter = {
    time: "",
    search: "",
    limit: 10,
    page: 1,
  };
  changePage(1);
}; // 重置筛选项

const getTableRows = () => {
  let params = { ...state.filter };
  params.id = state.nodeId;
  axios
    .get(`/apaas/system/v5/dictionary/list`, {
      params,
    })
    .then((res) => {
      if (res.data.code == 200) {
        let data = res.data.data || [];
        state.tableRows = data;
        state.tableTotal = res.data.total;
      } else {
        ElMessage.error(res.data.data);
      }
    });
}; // 获取表格数据

const changePage = (page) => {
  state.filter.page = page;
  getTableRows();
}; // 改变页码

const changeSize = (size) => {
  state.filter.limit = size;
  changePage(1);
}; // 改变每页条数

const register = () => {
  state.formData = {
    name: "",
    describe: "",
    state: 1,
    p_dict_id: state.fatherRow ? state.fatherRow.dict_id : "",
  };
  if (state.bgForm) {
    nextTick().then(() => {
      state.bgForm.validate((valid) => {
        if (!valid) {
          state.bgForm.clearValidate();
        }
      });
    });
  }
  state.addType = 1;
  state.addDialog = true;
}; // 新增字典按钮

onBeforeMount(() => {
  getTypeList();
});

const { tableRows, tableTotal, filter } = toRefs(state);
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  height: calc(100vh - 56px);
  padding: 0 24px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  .main_container {
    height: 100%;
    .filter-group {
      .left-filter {
        flex: 1;
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
      }
      .right-action {
        width: 144px;
        padding-bottom: 16px;
        .el-button {
          width: 64px;
        }
      }
    }

    .table_container {
      height: calc(100% - 70px);
      width: 100%;
      padding: 0 16px;
      .table {
        max-height: calc(100% - 64px);
      }
    }
  }

  .bg_form {
    width: 100%;
    box-sizing: border-box;
    .el-form-item {
      margin-bottom: 24px;
      :deep().el-form-item__label {
        line-height: 36px;
        height: 36px;
      }
      .el-form-item__content {
        width: 100%;
        .el-textarea {
          :deep().el-input__count {
            bottom: -16px;
            right: 4px;
            font-family: Roboto-Regular;
            color: #a9b1c7;
          }
        }
        .bg-switch-ele {
          width: 52px;
          height: 24px;
          :deep().el-switch__core {
            width: 100%;
            height: 24px;
            .el-switch__inner,
            .el-switch__action {
              top: 3px;
            }
          }
        }
      }
    }
  }
}
</style>
