<template>
  <div class="manual-list">
    <div class="header-toolbar">
      <div class="header-toolbar-left">
        <InputSearch v-model:value="searchValue" placeholder="请输入需要搜索的手册名称" enter-button="查询" style="width: 300px" @search="getManualList" />
        <!-- <span class="iconfont icon-date-asce" />
        <span class="iconfont icon-date-desc" /> -->
        <span v-if="showTable" class="iconfont icon-list" @click="showTable = !showTable" />
        <span v-else class="iconfont icon-tubiao" @click="showTable = !showTable" />
      </div>
      <div class="header-toolbar-right">
        <Button type="primary" shape="round" @click="handleAdd"> 新增手册 </Button>
        <Button v-if="showTable" type="primary" danger shape="round" :disabled="selectedRowKeys.length <= 0" @click="handleDelect()"> 删除选中 </Button>
      </div>
    </div>

    <div class="manual-data">
      <Table bordered v-if="showTable" size="small" :columns="columns" :data-source="dataSource" :pagination="pagination" rowKey="id" :row-selection="rowSelection">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'updateTime'">
            <span>{{ dateTimeFormat(record.updateTime) }}</span>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <Button type="link" @click="handleView(record)">查看</Button>
            <Button type="link" @click="handleEdit(record)">编辑</Button>
            <Button type="link" danger @click="handleDelect(record)">删除</Button>
          </template>
        </template>
      </Table>
      <List v-else item-layout="horizontal" size="small" :pagination="pagination" :data-source="dataSource">
        <template #renderItem="{ item }">
          <ListItem>
            <div class="list-item-content">
              <img v-if="item.cover" width="118" height="118" :src="`${resourceBaseUrl}/${item.cover}`" alt="封面" />
              <span v-else class="cover-none">暂无封面</span>
              <div class="list-item-fields">
                <div class="list-item-header">
                  <h3 class="field-name">{{ item.name }}</h3>
                  <div class="list-item-header-right">
                    <Button type="link" @click="handleView(item)">查看</Button>
                    <Button type="link" @click="handleEdit(item)">编辑</Button>
                    <Button type="link" danger @click="handleDelect(item)">删除</Button>
                  </div>
                </div>
                <div class="field-list">
                  <div class="field-item">
                    <span class="field-item-label"> <UserOutlined /> 编著： </span>
                    <span class="field-item-value">{{ item.compile }}</span>
                  </div>
                  <div class="field-item">
                    <span class="field-item-label"> <BookOutlined /> 出版社： </span>
                    <span class="field-item-value">{{ item.publish }}</span>
                  </div>
                </div>
                <div class="field-explain">
                  <span class="field-explain-label">简介：</span>
                  <span class="field-explain-value" :title="item.explain">{{ item.explain }}</span>
                </div>
              </div>
            </div>
          </ListItem>
        </template>
      </List>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, getCurrentInstance, watch, computed, unref, createVNode } from 'vue';
import { Row, Col, Divider, InputSearch, Button, Table, List, ListItem, message, Modal } from 'ant-design-vue';
import { UserOutlined, BookOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { useRouter } from 'vue-router';
import { getManualListApi, deleteManualApi } from '@/apis/manual';
import moment from 'moment';

const columns = [
  {
    title: '手册名称',
    dataIndex: 'name',
    width: '20%',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    width: '20%',
  },
  {
    title: '手册备注说明',
    dataIndex: 'explain',
    ellipsis: true,
    width: '40%',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    width: '20%',
  },
];

export default defineComponent({
  name: 'manual-list',
  components: { Row, Col, Divider, InputSearch, Button, Table, List, ListItem, UserOutlined, BookOutlined, ExclamationCircleOutlined },
  props: {
    manualClassify: { type: Number },
  },
  setup(props) {
    const router = useRouter();
    const searchValue = ref<string>('');
    const dataSource = ref<any>([]);
    const selectedRowKeys = ref<any>([]);
    const showTable = ref<boolean>(true);
    const pagination: any = {
      size: 'small',
      onChange: (page: number) => {
        console.log(page);
      },
      pageSize: 10,
    };
    const instance = getCurrentInstance();
    const resourceBaseUrl = instance?.appContext.config.globalProperties.$globalVariable.resourceBaseUrl;

    const rowSelection = computed(() => {
      return {
        selectedRowKeys: unref(selectedRowKeys),
        onChange: (keys: any) => {
          selectedRowKeys.value = keys;
        },
      };
    });

    const getManualList = async () => {
      const classify = props.manualClassify;
      let data = await getManualListApi(classify);
      if (searchValue.value) {
        data = data.filter((item: any) => {
          return item.name.toLowerCase().indexOf(searchValue.value.toLowerCase()) >= 0;
        });
      }
      dataSource.value = data;
    };

    watch(() => props.manualClassify, getManualList, { immediate: true });

    const dateTimeFormat = (dateTime: string) => {
      return dateTime ? moment(dateTime).format('YYYY-MM-DD HH:mm:ss') : '';
    };

    const handleAdd = () => {
      router.push({
        path: '/manual/edit',
        query: { classify: props.manualClassify },
      });
    };

    const handleView = (record: any) => {
      // window.history.pushState({ id: record.id }, '', 'app1#/manual/view');
      router.push({ 
        path: '/manual/view',
        query: { id: record.id }
      });
    };

    const handleEdit = (record: any) => {
      console.log('handleEdit', record);
      router.push({
        path: '/manual/edit',
        query: { id: record.id },
      });
    };

    const handleDelect = async (record?: any) => {
      Modal.confirm({
        title: '确定删除当前手册吗？',
        icon: createVNode(ExclamationCircleOutlined),
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk: async () => {
          await deleteManualApi({
            idArray: record ? [record.id] : selectedRowKeys.value,
            data: { delFlag: 1 },
          });
          message.success('手册删除成功');
          if (!record) selectedRowKeys.value = [];
          getManualList();
        },
      });
    };

    return {
      searchValue,
      dataSource,
      columns,
      selectedRowKeys,
      rowSelection,
      showTable,
      pagination,
      resourceBaseUrl,
      getManualList,
      dateTimeFormat,
      handleAdd,
      handleView,
      handleEdit,
      handleDelect,
    };
  },
});
</script>
<style lang="less" scoped>
.manual-list {
  .header-toolbar {
    padding: 10px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    &-left {
      display: flex;
      align-items: center;
      .iconfont {
        display: inline-block;
        height: 24px;
        line-height: 24px;
        font-size: 24px;
        margin: 0 6px;
      }
    }
    &-right {
      .ant-btn {
        margin: 0 6px;
      }
    }
  }
  .manual-data {
    /deep/ .ant-list {
      .ant-list-item {
        border: none;
        background-color: #f4f4f4;
        border-radius: 4px;
        margin-bottom: 12px;
        .list-item-content {
          display: flex;
          width: 100%;
          img {
            margin-right: 20px;
          }
          .cover-none {
            width: 118px;
            height: 118px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 20px;
            border: 1px solid #eee;
            border-radius: 4px;
          }
          .list-item-fields {
            flex: 1;
            .list-item-header {
              display: flex;
              justify-content: space-between;
              align-items: center;
            }
            .field-name {
              line-height: 32px;
              font-size: 20px;
              font-weight: 500;
              margin: 0;
            }
            .field-list {
              display: flex;
              margin-bottom: 10px;
              .field-item {
                display: flex;
                margin-right: 20px;
                color: #9b9b9b;
                font-size: 14px;
                line-height: 22px;
              }
            }
            .field-explain {
              width: 100%;
              height: 54px;
              line-height: 27px;
              white-space: normal;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
              word-break: break-all;
              &-label {
                font-weight: 700;
              }
            }
          }
        }
      }
    }
  }
}
</style>
