<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <bg-filter-group
        @search="changeSearch"
        class="page-filter-group"
        v-model="filter.search"
        placeholder="请输入关键字">
        <template v-slot:left_action>
          <div class="apaas_button">
            <el-button type="default" @click="router.go(-1)"> 返回 </el-button>
          </div>
          <span class="filter-group-item">执行ping命令</span>
        </template>
      </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:exec_start_time="{ row }">
              <span>{{ dateStringTransform(row.exec_start_time) }}</span>
            </template>
            <template v-slot:exec_desc="{ row }">
              <span
                class="can_click_text"
                @click="gotoPage(`/auto-maintenance/task-history/list/record?id=${row.id}&task_id=${route.query.id}`)">
                {{ row.exec_desc }}
              </span>
            </template>
            <template v-slot:state="{ row }">
              <span>
                <bg-icon
                  :style="{
                    color: ['#2b4695', '#429e8a', '#d75138'][row.state],
                  }"
                  :icon="
                    ['#bg-ic-s-circle-restart', '#bg-ic-s-circle-check', '#bg-ic-s-circle-close'][row.state]
                  "></bg-icon>
                {{ ["执行中", "成功", "失败"][row.state] }}
              </span>
            </template>
          </bg-table>
        </div>
        <bg-pagination
          :page="filter.page"
          :size="filter.page_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 { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";
import { Search } from "@element-plus/icons-vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import { dateStringTransform, downloadFile } from "@/services/helper.js";

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

const dataTable = ref(null);
const headers = [
  {
    label: "状态",
    prop: "state",
  },
  {
    label: "执行说明",
    prop: "exec_desc",
  },
  {
    label: "执行开始时间",
    prop: "exec_start_time",
  },
  {
    label: "执行耗时(s)",
    prop: "exec_time",
  },
  {
    label: "操作人",
    prop: "create_user",
  },
];

const state = reactive({
  tableRows: [
    {
      state: "执行中",
      desc: "asda",
    },
  ], // 表格数据
  tableTotal: 0, // 表格数据条数
  filter: {
    search: "",
    page: 1,
    page_size: 10,
  }, // 表格筛选项
  actionRow: null, // 当前操作的数据
});

const gotoPage = (url) => {
  router.push(url);
};

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

const getTableRows = () => {
  let params = {
    ...state.filter,
    task_id: route.query.id,
  };
  axios
    .get(`/v1/api/automated_mainten/task_history/task_info_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.page_size = size;
  changePage(1);
}; // 改变每页条数

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

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

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  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%;
    }
  }

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

.page-filter-group {
  position: relative;

  .filter-group-item {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 14px;
    color: #202531;
  }
}
</style>
