<template>
  <div class="page">
    <a-page-header :ghost="false" title="门店店员">
      <!--      <div class="ele-text-secondary"> 商户管理 </div>-->
    </a-page-header>
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="merchantClerkId"
          :columns="columns"
          :height="tableHeight"
          :datasource="datasource"
          v-model:selection="selection"
          :scroll="{ x: 800 }"
        >
          <template #toolbar>
            <search
              :selection="selection"
              @search="reload"
              @add="openEdit"
              @remove="removeBatch"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'logo'">
              <a-image :src="record.logo" :preview="false" :width="80" />
            </template>
            <template v-if="column.key === 'merchantClerkName'">
              <a-tooltip
                :title="`商户编号：${record.merchantClerkCode}`"
                placement="topLeft"
              >
                <p class="twoline-hide">
                  {{ record.merchantClerkName }}
                </p>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'user'">
              <a-tooltip :title="`用户ID：${record.userId}`">
                <a-avatar
                  :size="30"
                  :src="`${record.user.avatar}`"
                  style="margin-right: 4px"
                >
                  <template #icon>
                    <UserOutlined />
                  </template>
                </a-avatar>
                <a @click="openInfo(record)">{{ record.user.nickname }}</a>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'phone'">
              {{ record.user.phone }}
            </template>
            <template v-if="column.key === 'comments'">
              <a-tooltip :title="record.comments" placement="topLeft">
                {{ record.comments }}
              </a-tooltip>
            </template>
            <template v-if="column.key === 'createTime'">
              <a-tooltip :title="`${toDateString(record.createTime)}`">
                {{ record.createTime }}
              </a-tooltip>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">编辑</a>
                <a-divider type="vertical" />
                <a-popconfirm
                  title="确定要删除此记录吗？"
                  @confirm="remove(record)"
                >
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
      <!-- 编辑弹窗 -->
      <MerchantClerkEdit
        v-model:visible="showEdit"
        :data="current"
        @done="reload"
      />
    </div>
  </div>
</template>

<!--suppress TypeScriptValidateTypes -->
<script lang="ts" setup>
  import { createVNode, computed, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import MerchantClerkEdit from './components/merchant-clerk-edit.vue';
  import { Category } from '@/api/goods/category/model';
  import {
    pageMerchantClerks,
    removeMerchantClerk,
    removeMerchantClerks
  } from '@/api/merchant/clerk';
  import type {
    MerchantClerk,
    MerchantClerkParam
  } from '@/api/merchant/clerk/model';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 当前用户信息
  // const brand = getDictionaryOptions('serverBrand');

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '所属商户',
      dataIndex: 'merchantName',
      key: 'merchantName',
      ellipsis: true
    },
    {
      title: '用户信息',
      dataIndex: 'user',
      key: 'user'
    },
    {
      title: '真实姓名',
      dataIndex: 'realName',
      key: 'realName'
    },
    {
      title: '手机号码',
      dataIndex: 'phone',
      key: 'phone'
    },
    {
      title: '添加时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: true,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center',
      hideInSetting: true
    }
  ]);

  // 表格选中数据
  const selection = ref<MerchantClerk[]>([]);
  // 当前编辑数据
  const current = ref<MerchantClerk | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  const showUser = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    if (filters) {
      where.brand = filters.brand;
    }
    return pageMerchantClerks({ ...where, ...orders, page, limit });
  };

  /* 搜索 */
  const reload = (where?: MerchantClerkParam) => {
    selection.value = [];
    tableRef?.value?.reload({ where: where });
  };

  // 搜索是否展开
  const searchExpand = ref(false);

  // 表格固定高度
  const fixedHeight = ref(false);

  // 表格高度
  const tableHeight = computed(() => {
    return fixedHeight.value
      ? searchExpand.value
        ? 'calc(100vh - 618px)'
        : 'calc(100vh - 562px)'
      : void 0;
  });

  /* 打开编辑弹窗 */
  const openEdit = (row?: MerchantClerk) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 打开用户详情弹窗 */
  const openInfo = (row?: MerchantClerk) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 删除单个 */
  const remove = (row: MerchantClerk) => {
    const hide = message.loading('请求中..', 0);
    removeMerchantClerk(row.merchantClerkId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的记录吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        removeMerchantClerks(selection.value.map((d) => d.merchantClerkId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };
</script>

<script lang="ts">
  export default {
    name: 'MerchantClerkIndex'
  };
</script>

<style lang="less" scoped>
  // 文字超出隐藏(两行)
  // 需要设置文字容器的宽度
  .twoline-hide {
    width: 320px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    white-space: normal;
  }
</style>
