<template>
  <div class="custom-page">
    <avue-crud
      ref="crudRef"
      v-model="state.form"
      v-model:search="state.query"
      :table-loading="state.loading"
      :data="state.data"
      :option="state.options"
      @refresh-change="refreshChange"
      @search-reset="searchChange"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
    >
      <template #menu="scope">
        <custom-table-operate :actions="state.options.actions" :data="scope" />
      </template>
    </avue-crud>
  </div>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { useApp } from '@/hooks';
import { CRUD_OPTIONS } from '@/config';
import { isEmpty, getTree } from '@/utils';
import { GetEntityList, AddEntity, UpdateEntity, DeleteEntity } from '@/apis/system/dept';

const app = useApp();
const crudRef = ref(null);
const state = reactive({
  loading: false,
  query: {},
  form: {},
  selection: [],
  options: {
    ...CRUD_OPTIONS,
    rowKey: 'deptId',
    index: false,
    selection: false,
    expand: true,
    column: [
      {
        label: '部门名称',
        prop: 'deptName',
        search: true,
        rules: {
          required: true,
          message: '请输入',
          trigger: 'blur',
        },
      },
      {
        label: '状态',
        prop: 'status',
        type: 'select',
        search: true,
        dicData: [
          {
            label: '启用',
            value: '0',
          },
          {
            label: '禁用',
            value: '1',
          },
        ],
        value: '0',
        rules: {
          required: true,
          message: '请输入',
          trigger: 'blur',
        },
      },
      {
        label: '排序',
        prop: 'orderNum',
        type: 'number',
        rules: {
          required: true,
          message: '请输入',
          trigger: 'blur',
        },
      },
      {
        label: '负责人',
        prop: 'leader',
        hide: true,
      },
      {
        label: '联系电话',
        prop: 'phone',
        hide: true,
      },
      {
        label: '邮箱',
        prop: 'email',
        hide: true,
      },
    ],
    actions: [
      {
        name: '查看',
        icon: 'view',
        event: ({ row }) => rowView(row),
      },
      {
        name: '编辑',
        icon: 'edit',
        event: ({ row }) => rowEdit(row),
      },
      {
        type: 'danger',
        name: '删除',
        icon: 'delete',
        event: ({ row }) => rowDel(row),
      },
    ],
  },
  pageData: {
    total: 0,
    currentPage: 1,
    pageSize: 10,
  },
  data: [],
  minWeek: null,
  maxWeek: null,
  currentRow: {},
});

// 加载
const loadData = () => {
  state.loading = true;
  GetEntityList(state.query)
    .then((res) => {
      if (res.code === 200) {
        state.data = getTree(res.data, 'deptId');
        console.log(139, state.data);
      }
    })
    .catch((err) => {
      app.$message.error(err.msg);
      state.data = [];
    })
    .finally(() => {
      state.loading = false;
    });
};

loadData();

// 搜索
const searchChange = (params, done) => {
  if (done) done();
  state.query = params;
  loadData();
};

// 刷新
const refreshChange = () => {
  loadData();
  app.$message.success('刷新成功');
};

// 选择
const selectionChange = (rows) => {
  state.selection = rows;
};

// 查看
const rowView = (row) => {
  crudRef.value.rowView(row);
};

// 新增
const rowSave = (row, done, loading) => {
  AddEntity(row)
    .then((res) => {
      if (res.code === 200) {
        app.$message.success('添加成功！');
        done();
        loadData();
      }
    })
    .catch((err) => {
      app.$message.error(err.msg);
    })
    .finally(() => {
      loading();
    });
};

// 编辑
const rowEdit = (row) => {
  crudRef.value.rowEdit(row);
};
const rowUpdate = (row, index, done, loading) => {
  UpdateEntity(row)
    .then((res) => {
      if (res.code === 200) {
        app.$message.success('更新成功！');
        done();
        loadData();
      }
    })
    .catch((err) => {
      app.$message.error(err.msg);
    })
    .finally(() => {
      loading();
    });
};

// 删除
const rowDel = (row, index, done) => {
  if (isEmpty(row)) return;
  app
    .$confirm(`删除后信息将不可查看，确认要删除吗？`, '确定删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(() => {
      DeleteEntity({ id: row.id })
        .then((res) => {
          if (res.code === 200) {
            app.$message.success('删除成功！');
            loadData();
          }
        })
        .catch((err) => {
          app.$message.error(err.msg);
        });
    })
    .catch(() => {});
};
</script>
