import ColEmpty from '@/components/col-empty'
import Pagination from '@/components/pagination'
import request from '@/helpers/request'
import styles from './index.module.less'
import { ElSkeleton, ElSkeletonItem } from 'element-plus'
import { defineComponent } from 'vue'
import MusicItem from '../../user-info/music-class/item'
import { getUserType } from '@/helpers/utils'

export default defineComponent({
  name: 'list',
  props: {
    auditStatus: {
      type: String,
      default: ''
    },
    onNumber: {
      type: Function,
      default: (data: any) => {}
    }
  },
  data() {
    return {
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      list: [] as any[],
      loading: false,
      dataShow: false // 是否有数据
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      try {
        const url =
          this.auditStatus === 'MYSCORE'
            ? '/api-website/music/sheet/my'
            : '/api-website/music/sheet/favorite'
        const { data } = await request.get(url, {
          params: {
            page: this.pageInfo.page,
            rows: this.pageInfo.limit,
            clientType: getUserType()
          }
        })
        this.list =
          (Array.isArray(data.rows) &&
            data.rows.map(n => {
              if (typeof n.paymentType === 'string')
                n.paymentType = n.paymentType.split(',')
              return n
            })) ||
          []
        this.pageInfo.total = data.total
        if (data.total <= 0) {
          this.dataShow = true
        }
      } catch {}
      if (this.dataShow) {
        this.loading = false
      } else {
        setTimeout(() => {
          this.loading = false
        }, 200)
      }
      // this.loading = true
    }
  },
  render() {
    return (
      <>
        <div class="mt-5">
          <ElSkeleton
            loading={this.loading}
            animated
            class=" w-full m-auto px-[14px] flex items-center flex-col"
            count={3}
            v-slots={{
              template: () => (
                <div class="h-[94px] flex items-center justify-between w-full pt-2 mb-2">
                  <div class="w-2/3 flex items-center">
                    <ElSkeletonItem
                      variant="circle"
                      style={{ width: '88px', height: '88px' }}
                    ></ElSkeletonItem>
                    <div class="w-1/2 pl-2">
                      <ElSkeletonItem variant="h3"></ElSkeletonItem>
                      <ElSkeletonItem
                        variant="p"
                        style={{ width: '50%' }}
                      ></ElSkeletonItem>
                    </div>
                  </div>
                  <ElSkeletonItem
                    variant="p"
                    style={{ width: '20%' }}
                  ></ElSkeletonItem>
                </div>
              )
            }}
          >
            {this.list.map((item: any) => (
              <MusicItem
                onClick={(item: any) => {
                  // console.log(item)
                  if (this.auditStatus === 'UNPASS') {
                    this.$router.push({
                      path: '/userInfo/musicOperation',
                      query: {
                        type: 'update',
                        id: item.id
                      }
                    })
                  } else {
                    // 跳转对应详情 个人中心不跳转到详情
                    this.$router.push({
                      path: '/muiscDetial',
                      query: { id: item.id }
                    })
                  }
                }}
                item={{
                  id: item.id,
                  addName: item.addName,
                  addUserAvatar: item.addUserAvatar,
                  musicSheetName: item.musicSheetName,
                  titleImg: item.titleImg,
                  subjectNames: item.subjectNames,
                  exquisiteFlag: item.exquisiteFlag,
                  albumNums: item.albumNums,
                  composer: item.composer,
                  chargeType: item.chargeType,
                  paymentType: item.paymentType || []
                }}
                class={[
                  styles.musicListItem,
                  'pt-2 cursor-pointer border-b border-solid border-[#E7E6E6] hover:bg-[#F4F4F4]'
                ]}
              />
            ))}
          </ElSkeleton>
        </div>

        <Pagination
          total={this.pageInfo.total}
          v-model:page={this.pageInfo.page}
          v-model:limit={this.pageInfo.limit}
          pageSizes={this.pageInfo.page_size}
          pagination={this.getList}
        />

        {this.dataShow && <ColEmpty style={{ marginBottom: '30px' }} />}
      </>
    )
  }
})
