<template>
  <div>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button v-perms="['flow_type/add']" type="primary" @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
      <!-- <el-button
        v-perms="['flow_type/delete']"
        :disabled="!selectData.length"
        @click="handleDelete(selectData)"
      >
        删除
      </el-button> -->
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          {{ pager }}
          <el-table-column
            label="id"
            prop="id"
            show-overflow-tooltip
            width="60"
          />
          <el-table-column label="名称" prop="title" show-overflow-tooltip />
          <el-table-column
            label="应用部门"
            prop="department"
            show-overflow-tooltip
          />
          <el-table-column label="标识" prop="name" show-overflow-tooltip />
          <el-table-column label="图标" prop="icon" show-overflow-tooltip />
          <el-table-column
            label="所属分类"
            prop="type_name"
            show-overflow-tooltip
          />
          <!-- <el-table-column label="状态" prop="status" show-overflow-tooltip>
            <template #default="{ row }">
              <span v-if="row.status == 1" style="color: #67c23a">正常</span>
              <span v-else style="color: #fe0000">禁用</span>
            </template>
          </el-table-column> -->
          <el-table-column
            label="账号状态"
            min-width="100"
            v-perms="['auth.admin/edit']"
          >
            <template #default="{ row }">
              <el-switch
                v-model="row.status"
                :active-value="1"
                :inactive-value="0"
                @change="changeStatus(row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="auto"
            fixed="right"
          >
            <template #default="{ row }">
              <el-button
                v-perms="['flow/edit']"
                type="primary"
                link
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                v-perms="['flow/delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="flowTypeLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiCateLists, apiCateStatus } from "@/api/examined";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
// import { getRoutePath } from "router";
import EditPopup from "./editCate.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 查询条件
const queryParams = reactive({
  type: "",
  title: "",
  name: "",
  icon: "",
  department_ids: "",
  status: "",
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiCateLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiFlowTypeDelete({ id });
  getLists();
};
// 状态
const changeStatus = (row: any) => {
  apiCateStatus({ id: row.id, status: row.status });
};
getLists();
</script>
