<template>
  <div class="system-container">
    <el-card class="type-box">
      <div slot="header" class="clearfix card-title">
        <span>Test</span>
      </div>
      <el-radio-group v-model="formData.appType" @input="searchTable">
        <el-radio-button label="">All</el-radio-button>
        <el-radio-button v-for="item in typeData" :key="item.id" :label="item.value">{{ item.label }}</el-radio-button>
      </el-radio-group>
    </el-card>
    <div class="system-box">
      <div class="list-filter">
        <el-form ref="filterForm" :model="formData" inline>
          <el-form-item label="name">
            <el-input v-model="formData.systemName" class="filter-item" clearable />
          </el-form-item>
          <el-form-item label="name2">
            <el-input v-model="formData.deptName" class="filter-item" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchTable">Search</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="list-button">
        <el-button type="primary" @click="showEdit('ADD')">Add</el-button>
        <el-upload action="" class="upload-btn">
          <el-button>Upload</el-button>
        </el-upload>
      </div>
      <div v-loading="loading" class="list-box">
        <el-scrollbar class="list-scrollbar">
          <div class="system-item-box">
            <div v-for="item in tableData" :key="item.id" class="system-item">
              <div class="item-header">
                <div class="item-type">{{ formatDictData(item.appType) }}</div>
                <div class="item-btn">
                  <el-button type="text" @click="showEdit('VIEW', item)">Detail</el-button>
                  <el-button type="text" @click="showEdit('EDIT', item)">Edit</el-button>
                  <el-button type="text" @click="deleteSystem(item.id)">Delete</el-button>
                </div>
              </div>
              <div class="item-content">
                <div class="item-content-left">
                  <el-avatar :size="56" :src="item.icon" />
                  <span class="item-name">{{ item.systemName }}</span>
                </div>
                <el-button class="item-link" size="small" @click="jumpTo(item.url)">Open</el-button>
              </div>
              <div class="item-footer">{{ item.deptName }}</div>
            </div>
          </div>
        </el-scrollbar>
        <div v-if="total > 0" class="page">
          <el-pagination
            layout="total, prev, pager, next, jumper"
            :current-page="current"
            :total="total"
            :page-sizes="pageSizeAll"
            :page-size="size"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <system-detail ref="systemDetail" :type-data="typeData" />
  </div>
</template>

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

import { fetchDictData } from '@/api/dict'
import { fetchAllSystemList, pushDeleteSystem } from '@/api/system'

import { hasValidRecords, formatDictData, isNull } from '@/utils/convert'

export default {
  components: { SystemDetail },
  data() {
    return {
      // type
      dictType: 'app_type',
      typeData: [],
      // table
      current: 1,
      size: 16,
      total: 0,
      pageSizeAll: [10, 20, 50, 100, 200, 500],
      tableData: [],
      // filter
      formData: {
        appType: '',
        systemName: '',
        deptName: ''
      },
      // others
      loading: false
    }
  },
  created() {
    this.getTypeData()
    this.searchTable()
  },
  methods: {
    // 改变每页显示条数
    handleSizeChange(val) {
      this.current = 1
      this.size = val
      this.getTablelist()
    },
    // 切换第几页
    handleCurrentChange(val) {
      this.current = val
      this.getTablelist()
    },
    // 重置搜索项
    resetTable(formName) {
      this.$refs[formName].resetFields()
      this.getTablelist()
    },
    // 点击搜索按钮
    searchTable() {
      this.current = 1
      this.getTablelist()
    },
    // 获取table数据
    getTablelist() {
      this.loading = true
      const params = {
        page: this.current,
        size: this.size,
        params: {
          delFlag: 0,
          appType: this.formData.appType,
          systemName: this.formData.systemName,
          deptName: this.formData.deptName
        }
      }
      fetchAllSystemList(params).then(response => {
        this.loading = false
        if (hasValidRecords(response)) {
          this.tableData = response.data.records
          this.total = response.data.total
        } else {
          this.tableData = []
          this.total = 0
        }
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: ': ' + error.message
        })
      })
    },
    jumpTo(link) {
      window.open(link, '_blank')
    },
    showEdit(type, data) {
      this.$refs.systemDetail.open(type, data)
    },
    deleteSystem(id) {
      this.loading = true
      pushDeleteSystem(id).then(res => {
        this.getTablelist()
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: ': ' + error.message
        })
      })
    },
    getTypeData() {
      fetchDictData(this.dictType).then(response => {
        if (!isNull(response.data)) {
          this.typeData = response.data
        } else {
          this.typeData = []
        }
      }).catch(error => {
        console.log(error)
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取应用类型出错: ' + error.message
        })
      })
    },
    formatDictData(type) {
      return formatDictData(this.typeData, type)
    }
  }
}
</script>

<style lang="scss" scoped>
.system-container {
  display: flex;

  .type-box {
    width: 230px;
    min-width: 230px;
    margin-right: 5px;

    .el-radio-group {
      width: 100%;
    }
    .el-radio-button {
      display: block;
    }

    ::v-deep {
      .el-card__header {
        height: 60px;
      }
      .el-card__body {
        padding: 20px 0;
      }

      .el-radio-button__inner {
        width: 100%;
        border: 0;
        text-align: left;
      }
      .el-radio-button__orig-radio:checked + .el-radio-button__inner {
        background-color: #ebf2fd;
        color: #0056dd;
      }
    }
  }

  .card-title {
    font-size: 18px;
    line-height: 24px;
    color: rgba(0,0,0,0.85);
    font-weight: bold;
  }

  .system-box {
    padding-left: 20px;
    background-color: #fff;
    width: 100%;
    height: 100%;

    .upload-btn {
      display: inline-block;
      margin-left: 10px;
    }

    .page {
      margin-right: 20px;
      text-align: right;
    }

    ::v-deep {
      .el-scrollbar__wrap{
        overflow-x: hidden;
      }

      .el-scrollbar__bar.is-horizontal {
        display: none;
      }
    }
  }

  .list-filter {
    height: 60px;
    width: 800px;
    padding-top: 10px;
  }

  .list-button {
    height: 50px;
  }

  .list-box {
    min-width: 800px;
    height: calc(100% - 110px);
  }

  .list-scrollbar {
    height: calc(100% - 32px);
  }

  .system-item-box {
    display: flex;
    flex-wrap: wrap;
  }

  .system-item {
    width: 380px;
    height: 150px;
    border: 1px solid rgba(0,0,0,0.09);
    border-radius: 4px;
    margin-right: 16px;
    margin-bottom: 16px;
    padding: 0 10px 10px 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .item-header {
      height: 35px;
    }

    .item-type, .item-btn {
      height: 100%;
    }

    .item-type {
      display: inline-block;
      font-size: 14px;
      line-height: 35px;
      color: rgba(0,0,0,0.65);
    }

    .item-btn {
      float: right;
    }

    .item-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .item-content-left {
      display: flex;
      align-items: center;
    }

    .item-name {
      font-size: 14px;
      color: rgba(0,0,0,0.85);
      margin-left: 10px;
      display: inline-block;
      width: 230px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }

    .item-link {
      float: right;
    }

    .item-footer {
      font-size: 12px;
      color: rgba(0,0,0,0.65);
    }

    ::v-deep {
      .el-button--small {
        font-size: 14px;
      }
    }
  }

}
</style>

