<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="父部门id" prop="parentId">
        <el-input v-model="queryParams.parentId" placeholder="请输入父部门id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="祖级列表" prop="ancestors">
        <el-input v-model="queryParams.ancestors" placeholder="请输入祖级列表" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="维保名称" prop="maintenanceName">
        <el-input v-model="queryParams.maintenanceName" placeholder="请输入维保名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" style="min-height:32px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['maintenance:maintenance:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="maintenanceList" row-key="maintenanceId"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
      <el-table-column label="维保名称" prop="maintenanceName" />
      <!-- <el-table-column label="维保编号" align="center" prop="maintenanceId" width="100" />
      <el-table-column label="父级编号" align="center" prop="parentId" width="100" />
      <el-table-column label="祖级列表" align="center" prop="ancestors" width="300" /> -->
      <el-table-column label="操作" align="center" fixed="right" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)"
            v-hasPermi="['maintenance:maintenance:add']">新增</el-button>
          <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['maintenance:maintenance:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['maintenance:maintenance:remove']"
            :disabled="scope.row.children != undefined || scope.row.parentId == 0"
            v-if="!(scope.row.children != undefined || scope.row.parentId == 0)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改维保对话框 -->
    <el-dialog  :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="maintenanceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父部门id" prop="parentId" v-if="form.parentId != 0">
          <el-tree-select v-model="form.parentId" :data="maintenanceOptions"
            :props="{ value: 'maintenanceId', label: 'maintenanceName', children: 'children' }"
            value-key="maintenanceId" placeholder="请选择父部门id" check-strictly />
        </el-form-item>
        <el-form-item label="维保名称" prop="maintenanceName">
          <el-input maxlength="100" v-model="form.maintenanceName" placeholder="请输入维保名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Maintenance">
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance, downloadMaintenance } from "@/api/maintenance/maintenance";

const { proxy } = getCurrentInstance();

const maintenanceList = ref([]);
const maintenanceOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    parentId: null,
    ancestors: null,
    maintenanceName: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询维保列表 */
function getList() {
  loading.value = true;
  listMaintenance(queryParams.value).then(response => {
    maintenanceList.value = proxy.handleTree(response.data, "maintenanceId", "parentId");
    loading.value = false;

    //// console.log(maintenanceList.value)
  });
}

/** 查询维保下拉树结构 */
function getTreeselect() {
  listMaintenance().then(response => {
    maintenanceOptions.value = [];
    // const data = { maintenanceId: 0, maintenanceName: '顶级节点', children: [] };
    // data.children = proxy.handleTree(response.data, "maintenanceId", "parentId");
    // maintenanceOptions.value.push(data);

    maintenanceOptions.value.push(...proxy.handleTree(response.data, "maintenanceId", "parentId"));
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    maintenanceId: null,
    parentId: null,
    ancestors: null,
    maintenanceName: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("maintenanceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.maintenanceId) {
    form.value.parentId = row.maintenanceId;
  } else {
    form.value.parentId = 123; // 船体
  }
  open.value = true;
  title.value = "添加维保";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.maintenanceId;
  }
  getMaintenance(row.maintenanceId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改维保";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["maintenanceRef"].validate(valid => {
    if (valid) {
      if (form.value.maintenanceId != null) {
        updateMaintenance(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMaintenance(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
 // console.log(row)
  if (row.children != undefined) {
    proxy.$modal.msgWarning("该数据项下含有子项，不得删除");
    return;
  }
  if (row.parentId == 0) {
    proxy.$modal.msgWarning(`${row.maintenanceName}不得删除`);
    return;
  }
  proxy.$modal.confirm('是否确认删除维保名称为"' + row.maintenanceName + '"的数据项？').then(function () {
    return delMaintenance(row.maintenanceId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

function initialData() {
  getList();
}

onActivated(() => {
  //// console.log("onActivated")
  initialData();
})
onMounted(() => {
  //// console.log("onMounted")
  initialData();
})
</script>