<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="查询名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入查询名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询状态" prop="status">
        <DictSelect
          v-model="queryParams.status"
          placeholder="请选择查询状态"
          dict-type="COMMON_STATUS"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['infra:query-manage:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openForm(undefined)"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['infra:query-manage:export']"
          >导出全部</el-button
        >
      </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
          >展开/折叠</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="list"
      row-key="id"
      border
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="查询编码" align="center" prop="code" />
      <el-table-column label="查询名称" align="center" prop="name">
        <!--  -->
        <template #default="{ row }">
          <span v-if="row.parentId == 0 || row.status == 1">{{
            row.name
          }}</span>
          <el-link v-else type="primary" @click="handleToQuery(row)">{{
            row.name
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column label="查询状态" align="center" prop="status">
        <template #default="{ row }">
          <dict-tag type="common_status" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <el-button
            v-if="scope.row.parentId == 0"
            type="text"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(scope.row)"
            >新增</el-button
          >
          <el-button
            v-hasPermi="['infra:query-manage:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openForm(scope.row.id)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['infra:query-manage:delete']"
            class="text-danger"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框(添加 / 修改) -->
    <QueryManageForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
import * as QueryManageApi from "@/api/mes/queryManage/index";
import QueryManageForm from "./QueryManageForm.vue";
export default {
  name: "QueryManage",
  components: {
    QueryManageForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询管理列表
      list: [],
      // 选中行
      currentRow: {},
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        code: null,
        name: null,
        parentId: null,
        sort: null,
        status: null,
        dataSourceConfigId: null,
        sqlStatement: null,
        createTime: [],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    handleToQuery(row) {
      this.$router.push({
        path: `/querymanage/queryFormNew?id=${row.id}&name=${encodeURIComponent(
          row.name
        )}`,
      });
    },
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await QueryManageApi.getQueryManagePage(this.queryParams);
        // this.list = res.data.list;
        this.list = this.handleTree(res.data.list, "id");
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    handleAdd({ name, id }) {
      this.$refs["formRef"].formData.parentId = id;
      this.$refs["formRef"].formData.parentName = name;
      this.$refs["formRef"].open();
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm(
        '是否确认删除查询管理编号为"' + id + '"的数据项?'
      );
      try {
        await QueryManageApi.deleteQueryManage(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm("是否确认导出所有查询管理数据项?");
      try {
        this.exportLoading = true;
        const res = await QueryManageApi.exportQueryManageExcel(
          this.queryParams
        );
        this.$download.excel(res, "查询管理.xls");
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  },
};
</script>
