<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>{{ title }}</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showEdit">编辑</el-button>
      </div>
      <el-carousel v-loading="loading" class="system-content" :autoplay="false" arrow="never" trigger="click">
        <el-carousel-item v-for="(page, index) in carouselData" :key="'carousel_' + index">
          <div class="system-box">
            <div v-for="item in page" :key="item.id" class="system-item-box" @click="jumpTo(item.url)">
              <el-avatar :size="56" fit="fill" :src="item.icon" />
              <div class="system-title">{{ item.systemName }}</div>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </el-card>
    <system-edit ref="systemEdit" :title="title.startsWith('我的') ? title.substr(2) : title" />
  </div>
</template>

<script>
import SystemEdit from './components/SystemEdit'

import { fetchMySystemList } from '@/api/system'
import { isNull, hasValidRecords } from '@/utils/convert'

export default {
  components: { SystemEdit },
  props: {
    title: {
      type: String,
      default: '我的应用'
    },
    pageSize: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      rawData: [],
      carouselData2: [],
      loading: false
    }
  },
  computed: {
    carouselData() {
      const result = []
      if (!isNull(this.rawData)) {
        const len = this.rawData.length
        const page = Math.ceil(len / this.pageSize)
        for (var i = 0; i < page; i++) {
          result.push(this.rawData.slice(i * this.pageSize, (i + 1) * this.pageSize))
        }
      }
      return result
    }
  },
  created() {
    this.getMySystemData()
  },
  methods: {
    jumpTo(link) {
      window.open(link, '_blank')
    },
    showEdit() {
      this.$refs.systemEdit.open()
    },
    getMySystemData() {
      this.loading = true
      const params = {
        page: 1,
        size: 100,
        params: {
          delFlag: 0
        }
      }
      fetchMySystemList(params).then(response => {
        this.loading = false
        if (hasValidRecords(response)) {
          this.rawData = response.data.records
        } else {
          this.rawData = []
        }
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取我的应用出错：' + error.message
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.system-content {
  ::v-deep {
    .el-carousel__container {
      height: 100%;
      // width: 100%;
    }
    .el-carousel__button {
      background-color: #cccccc;
      opacity: 1;
    }
    .el-carousel__indicator.is-active .el-carousel__button {
      background-color: #0056dd;
    }
    .el-carousel__indicator--horizontal {
      padding: 12px 4px 2px 4px;
    }
  }

  .system-box {
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-around;
    align-content: space-around;
    // width: 790px !important;
    // margin: 0 auto;
    height: 100%;
    padding: 10px 0;

    .system-item-box {
      width: 170px;
      height: 130px;
      border: 1px solid rgba(0,0,0,0.09);
      border-radius:  10px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin: 5px calc(10% - 85px);
      cursor: pointer;

      .system-title {
        font-size: 16px;
        color: rgba(0,0,0,0.65);
        margin-top: 10px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }

  @media only screen and (min-width: 1680px) {
    .system-item-box {
      width: 170px !important;
      margin: 5px calc(10% - 85px) !important;
    }
  }

  @media only screen and (min-width: 1440px) and (max-width: 1679px) {
    .system-item-box {
      width: 140px !important;
      margin: 5px calc(10% - 70px) !important;
    }
  }

  @media only screen and (max-width: 1439px) {
    .system-item-box {
      width: 100px !important;
      margin: 5px calc(10% - 50px) !important;
    }
  }
}
</style>
