<script lang="ts" setup>
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridListeners, VxeGridProps } from '#/adapter/vxe-table';

import { Page, useVbenModal } from '@vben/common-ui';
import { MdiDetail, MdiEdit } from '@vben/icons';
import { parseQueryValues } from '@vben/utils';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getWorkerListApi } from '#/api/worker';
import { $t } from '#/locales';

import EditForm from './form.vue';

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: true,
  // 控制表单是否显示折叠按钮
  showCollapseButton: true,
  // 按下回车时是否提交表单
  submitOnEnter: true,
  schema: [
    {
      component: 'Input',
      fieldName: 'worker_xm',
      label: '姓名',
      componentProps: {
        placeholder: $t('ui.placeholder.input'),
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'worker_dh',
      label: '联系电话',
      componentProps: {
        placeholder: $t('ui.placeholder.input'),
        allowClear: true,
      },
    },
  ],
  wrapperClass: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-5',
};

const gridOptions: VxeGridProps<any> = {
  toolbarConfig: {
    custom: true,
    export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
  height: 'auto',
  exportConfig: {},
  pagerConfig: {},
  rowConfig: {
    isHover: true,
  },
  stripe: true,

  proxyConfig: {
    response: {
      result: 'Data',
      total: 'Total',
    },
    ajax: {
      query: async ({ page }, formValues) => {
        return await getWorkerListApi({
          pageindex: page.currentPage,
          rows: page.pageSize,
          ...parseQueryValues(formValues),
        });
      },
    },
  },

  columns: [
    // { title: '职员id', field: 'worker_id' },
    { title: '姓名', field: 'worker_xm', sortable: true },
    { title: '工号', field: 'worker_gh', sortable: true },
    { title: '类别', field: 'worker_lb', sortable: true },
    { title: '状态', field: 'worker_zt', sortable: true },
    // { title: '所属部门', field: 'worker_ssbm' },
    // { title: '二级部门', field: 'worker_ssbm2' },
    { title: '岗位', field: 'worker_gw', sortable: true },
    { title: '联系地址', field: 'worker_lxdz', sortable: true },
    { title: '联系电话', field: 'worker_dh', sortable: true },
    { title: '邮箱', field: 'worker_email', sortable: true },
    // { title: '紧急联系人', field: 'worker_jjlxr' },
    // { title: '紧急联系电话', field: 'worker_jjlxdh', width: 120 },
    { title: '创建时间', field: 'worker_createdate', sortable: true },
    { title: '创建者', field: 'worker_create', sortable: true },
    // { title: '是否绑定微信', field: 'worker_iswxbind', width: 120 },
    {
      title: '操作',
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      width: 140,
    },
  ],
};

const gridEvents: VxeGridListeners<any> = {
  cellDblclick: ({ row }) => {
    handleDetail(row);
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  gridEvents,
  gridOptions,
  formOptions,
});

const [Modal, modalApi] = useVbenModal({
  fullscreenButton: false,
  closeOnClickModal: false,
  closeOnPressEscape: false,
  connectedComponent: EditForm,
});

/* 创建 */
function handleCreate() {
  modalApi.setState({ showCancelButton: true });
  modalApi.setData({ formType: 'create' }).open();
}

/* 编辑 */
function handleEdit(row: any) {
  modalApi.setState({ showCancelButton: true });
  modalApi.setData({ formType: 'edit', row }).open();
}

/* 详情 */
function handleDetail(row: any) {
  modalApi.setState({ showCancelButton: false });
  modalApi.setData({ formType: 'detail', row }).open();
}

function handleFinish() {
  gridApi.reload();
}
</script>

<template>
  <Page auto-content-height>
    <Grid table-title="职员列表">
      <template #toolbar-tools>
        <el-button type="primary" @click="handleCreate"> 新增 </el-button>
      </template>
      <template #action="{ row }">
        <el-tooltip
          class="box-item"
          effect="dark"
          content="详情"
          placement="top"
        >
          <el-button
            round
            @click="() => handleDetail(row)"
            :icon="MdiDetail"
            class="!p-2"
          />
        </el-tooltip>
        <el-tooltip
          class="box-item"
          effect="dark"
          content="编辑"
          placement="top"
        >
          <el-button
            round
            @click="() => handleEdit(row)"
            :icon="MdiEdit"
            class="!p-2"
          />
        </el-tooltip>
      </template>
    </Grid>
    <Modal @finish="handleFinish" />
  </Page>
</template>
