<template>
  <a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
    <a-card :bordered="false">
      <div class="website-field">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="websiteId"
          :columns="columns"
          :datasource="datasource"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          class="sys-org-table"
        >
          <template #toolbar>
            <WebsiteFieldSearch @add="openEdit" />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'name'">
              <div class="ele-text-heading">{{ record.name }}</div>
              <span class="ele-text-placeholder">{{ record.comments }}</span>
            </template>
            <template v-if="column.key === 'value'">
              <a-image
                v-if="record.type === 1"
                :src="record.value"
                :width="120"
              />
              <span v-else>{{ record.value }}</span>
            </template>
            <template v-if="column.key === 'action'">
              <a @click="copyText('{{ config.' + record.name + ' }}')">调用</a>
              <a-divider type="vertical" />
              <a @click="openEdit(record)">编辑</a>
              <template v-if="record.deleted == 0">
                <a-divider type="vertical" />
                <a-popconfirm
                  title="确定要删除此记录吗？"
                  @confirm="remove(record)"
                >
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </template>
              <template v-if="record.deleted == 1">
                <a-divider type="vertical" />
                <a-popconfirm
                  title="确定要放回原处吗？"
                  @confirm="recovery(record)"
                >
                  <a class="ele-text-danger">恢复</a>
                </a-popconfirm>
              </template>
            </template>
          </template>
        </ele-pro-table>
        <!-- 编辑弹窗 -->
        <WebsiteFieldEdit
          v-model:visible="showEdit"
          :data="current"
          @done="reload"
        />
      </div>
    </a-card>
  </a-page-header>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type { DatasourceFunction } from 'ele-admin-pro/es/ele-pro-table/types';
  import WebsiteFieldSearch from './components/website-field-search.vue';
  import { Website } from '@/api/cms/website/model';
  import WebsiteFieldEdit from './components/website-field-edit.vue';
  import {
    WebsiteField,
    WebsiteFieldParam
  } from '@/api/cms/website/field/model';
  import {
    pageWebsiteField,
    removeWebsiteField,
    undeleteWebsiteField,
    updateWebsiteField
  } from '@/api/cms/website/field';
  import { copyText, getPageTitle } from '@/utils/common';

  const props = defineProps<{
    websiteId: any;
    data: Website;
  }>();

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
  const selection = ref<any[]>();
  // 当前编辑数据
  const current = ref<WebsiteField | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    // 搜索条件
    return pageWebsiteField({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<any[]>([
    {
      title: '键',
      dataIndex: 'name',
      key: 'name'
    },
    {
      title: '值',
      dataIndex: 'value',
      key: 'value'
    },
    {
      title: '排序',
      dataIndex: 'sortNumber',
      width: 120,
      align: 'center'
    },
    {
      title: '操作',
      key: 'action',
      width: 180,
      align: 'center',
      hideInSetting: true
    }
  ]);

  const moveUp = (row?: WebsiteField) => {
    updateWebsiteField({
      id: row?.id,
      sortNumber: Number(row?.sortNumber) + 1
    }).then((msg) => {
      message.success(msg);
      reload();
    });
  };

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

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

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

  // 从回收站放回原处
  const recovery = (row: WebsiteField) => {
    const hide = message.loading('请求中..', 0);
    undeleteWebsiteField(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 自定义行属性 */
  const customRow = (record: WebsiteField) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };

  watch(
    () => props.websiteId,
    (websiteId) => {
      if (websiteId) {
        reload();
      }
    },
    { immediate: true }
  );
</script>

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