<template>
  <page-container>
    <div class="basic-list-standard-list">
      <h-card :bordered="false">
        <h-row>
          <h-col :sm="8" :xs="24">
            <info title="我的待办" value="8个任务" :bordered="true" />
          </h-col>
          <h-col :sm="8" :xs="24">
            <info title="本周任务平均处理时间" value="32分钟" :bordered="true" />
          </h-col>
          <h-col :sm="8" :xs="24">
            <info title="本周完成任务数" value="24个任务" />
          </h-col>
        </h-row>
      </h-card>
      <h-card
        title="基本列表"
        class="basic-list-card"
        style="margin-top: 24px"
        :bordered="false"
        :body-style="{ padding: '0 32px 40px 32px' }"
      >
        <template #extra>
          <div class="extra-content">
            <h-radio-group default-value="all">
              <h-radio-button value="all">全部</h-radio-button>
              <h-radio-button value="progress">进行中</h-radio-button>
              <h-radio-button value="waiting">等待中</h-radio-button>
            </h-radio-group>
            <h-input-search
              class="extra-content-search"
              placeholder="请输入"
              @search="() => ({})"
            />
          </div>
        </template>
        <h-button type="dashed" style="width: 100%; margin-bottom: 8px">
          <plus-outlined />
          添加
        </h-button>
        <h-list
          size="large"
          row-key="id"
          :loading="context.loading"
          :pagination="options"
          :data-source="context.dataSource"
        >
          <template #renderItem="{ item }">
            <h-list-item :key="item.id">
              <template #actions>
                <a
                  key="edit"
                  @click="
                    e => {
                      e.preventDefault();
                      showEditModal(item);
                    }
                  "
                >
                  编辑
                </a>
                <h-dropdown>
                  <template #overlay>
                    <h-menu @click="({ key }) => editAndDelete(key, item)">
                      <h-menu-item key="edit">编辑</h-menu-item>
                      <h-menu-item key="delete">删除</h-menu-item>
                    </h-menu>
                  </template>
                  <a>更多</a>
                </h-dropdown>
              </template>
              <h-list-item-meta :description="item.subDescription">
                <template #avatar>
                  <h-avatar :src="item.logo" shape="square" size="large" />
                </template>
                <template #title>
                  <a :href="item.href">{{ item.title }}</a>
                </template>
              </h-list-item-meta>
              <div class="list-content">
                <div class="list-content-item">
                  <span>Owner</span>
                  <p>{{ item.owner }}</p>
                </div>
                <div class="list-content-item">
                  <span>开始时间</span>
                  <p>{{ dayjs(item.createdAt).format('YYYY-MM-DD HH:mm') }}</p>
                </div>
                <div class="list-content-item">
                  <h-progress
                    :percent="item.percent"
                    :status="item.status"
                    :stroke-width="6"
                    style="width: 180px"
                  />
                </div>
              </div>
            </h-list-item>
          </template>
        </h-list>
      </h-card>
    </div>
  </page-container>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from 'vue';
import { PlusOutlined } from '@ant-design/icons-vue';
import { queryFakeList } from '@/api/list/basic-list';
import type { TableListItem } from '../typing';
import Info from './info.vue';
import dayjs from 'dayjs';
import type { RequestParams } from '@/utils/hooks/useFetchData';
import { useFetchData } from '@/utils/hooks/useFetchData';

export default defineComponent({
  setup() {
    const { reload, context } = useFetchData<{
      data: TableListItem[];
      success: boolean;
      total: number;
    }>(
      (params?: RequestParams) => {
        return new Promise<{
          data: TableListItem[];
          success: boolean;
          total: number;
        }>((resolve, reject) => {
          queryFakeList({ count: params?.pageSize || 10 })
            .then((res: TableListItem[]) => {
              resolve({
                data: res,
                success: true,
                total: 10,
              });
            })
            .catch(reject);
        });
      },
      {
        loading: false,
        pageSize: 5,
      },
    );

    const { total, pageSize } = toRefs(context);

    const options = reactive({
      showSizeChanger: true,
      showQuickJumper: true,
      pageSizeOptions: ['5', '10', '15', '20'],
      pageSize,
      total,
      onChange: (page: number, pageSize: number) => {
        context.current = page;
        context.pageSize = pageSize;
        reload();
      },
      onShowSizeChange: (_current: number, size: number) => {
        context.pageSize = size;
        reload();
      },
    });

    const showEditModal = item => {
      console.log(item);
    };
    const editAndDelete = (key, item) => {
      console.log(key, item);
    };
    return {
      dayjs,
      showEditModal,
      editAndDelete,
      options,
      context,
    };
  },
  components: {
    Info,
    PlusOutlined,
  },
});
</script>

<style lang="less" scoped>
.basic-list-standard-list {
  :deep(.ant-card-head-title) {
    padding: 24px 0;
    line-height: 32px;
  }
  :deep(.ant-input-search),
  .extra-content-search {
    width: 272px;
    margin-left: 16px;
  }
}
.basic-list-card {
  :deep(.ant-card-extra) {
    flex: 1;
  }
  .list-content {
    font-size: 0;
    .list-content-item {
      display: inline-block;
      margin-left: 40px;
      color: @text-color-secondary;
      font-size: @font-size-base;
      vertical-align: middle;
      > span {
        line-height: 20px;
      }
      > p {
        margin-top: 4px;
        margin-bottom: 0;
        line-height: 22px;
      }
    }
  }
}
</style>
