<template>
  <div class="business-container">
    <el-card class="type-box">
      <div slot="header" class="clearfix card-title">
        <span>业务类型</span>
      </div>
      <el-radio-group v-model="formData.businessType" v-loading="typeLoading" @input="searchTable">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button v-for="item in typeData" :key="item.id" :label="item.id">{{ item.label }}</el-radio-button>
      </el-radio-group>
    </el-card>
    <div class="business-box">
      <div class="list-filter">
        <el-form ref="filterForm" :model="formData" inline>
          <el-form-item label="应用名称">
            <el-input v-model="formData.appName" class="filter-item" clearable />
          </el-form-item>
          <el-form-item label="业务名称">
            <el-input v-model="formData.businessName" class="filter-item" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchTable">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="list-button">
        <el-button type="primary" @click="showEdit('ADD')">新增</el-button>
        <el-upload action="" class="upload-btn">
          <el-button>导入</el-button>
        </el-upload>
      </div>
      <div v-loading="loading" class="list-box">
        <el-scrollbar class="list-scrollbar">
          <div class="business-item-box">
            <div v-for="item in tableData" :key="item.id" class="business-item">
              <div class="item-header">
                <div class="item-type">{{ formatDictData(item.businessType) }}</div>
                <div class="item-btn">
                  <el-button type="text" @click="showEdit('VIEW', item)">详情</el-button>
                  <el-button type="text" @click="showEdit('EDIT', item)">修改</el-button>
                  <el-button type="text" @click="deleteItem(item.id)">删除</el-button>
                </div>
              </div>
              <div class="item-content">
                <div class="item-content-left">
                  <el-avatar :size="56" :src="item.icon | formatFileUrl" />
                  <div class="item-title">
                    <div class="item-name">{{ item.businessName }}</div>
                    <div class="item-system">{{ item.appName }}</div>
                  </div>
                </div>
                <el-button class="item-link" size="small" @click="$jumpTo('business', item.id, item.businessNumber, item.url)">打开</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, sizes, prev, pager, next, jumper"
            :current-page="current"
            :total="total"
            :page-sizes="pageSizeAll"
            :page-size="size"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <detail-edit ref="businessDetail" :type-data="typeData" @refreshData="searchTable" />
  </div>
</template>

<script>
import { fetchDictData } from '@/api/dict'
import { fetchAllBizList, pushDeleteBusiness } from '@/api/business'

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

import DetailEdit from './components/DetailEdit'

export default {
  name: 'Business',
  components: { DetailEdit },
  data() {
    return {
      // type
      dictType: 'bisiness_type',
      typeData: [],
      // table
      current: 1,
      size: 16,
      total: 0,
      pageSizeAll: [10, 16, 20, 50, 100, 200, 500],
      tableData: [],
      // filter
      formData: {
        businessType: '',
        businessName: '',
        appName: ''
      },
      // others
      loading: false,
      typeLoading: false
    }
  },
  created() {
    this.getTypeData()
    this.searchTable()
  },
  methods: {
    // 改变每页显示条数
    handleSizeChange(val) {
      this.current = 1
      this.size = val
      this.getTablelist()
    },
    // 切换第几页
    handleCurrentChange(val) {
      this.current = val
      this.getTablelist()
    },
    // 重置搜索项
    resetForm(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,
          businessType: this.formData.businessType,
          businessName: this.formData.businessName,
          appName: this.formData.appName
        }
      }
      fetchAllBizList(params).then(response => {
        if (hasValidRecords(response)) {
          this.tableData = response.data.records
          this.total = response.data.total
        } else {
          this.tableData = []
          this.total = 0
        }
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取业务出错: ' + error.message
        })
      })
    },
    showEdit(viewType, data) {
      this.$refs.businessDetail.open(viewType, data)
    },
    deleteItem(id) {
      this.loading = true
      pushDeleteBusiness(id).then(res => {
        this.getTablelist()
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '删除业务出错: ' + error.message
        })
      })
    },
    getTypeData() {
      this.typeLoading = true
      fetchDictData(this.dictType).then(response => {
        if (!isNull(response.data)) {
          this.typeData = response.data
        } else {
          this.typeData = []
        }
        this.typeLoading = false
      }).catch(error => {
        console.log(error)
        this.typeLoading = false
        this.$message({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取业务类型出错: ' + error.message
        })
      })
    },
    formatDictData(type) {
      return formatDictData(this.typeData, type)
    }
  }
}
</script>

<style lang="scss" scoped>
.business-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: 56px;
        padding: 12px 20px;
      }
      .el-card__body {
        padding: 10px 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: 32px;
    height: 32px;
    color: rgba(0,0,0,0.85);
    font-weight: bold;
  }

  .business-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: 56px;
    width: 800px;
    padding-top: 10px;
  }

  .list-button {
    height: 50px;
  }

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

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

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

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

    .item-header {
      height: 35px;
    }

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

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

    .item-btn {
      float: right;

      .el-button {
        padding: 0 5px;
        position: relative;
      }
      .el-button + .el-button {
        margin-left: 5px;

        &:before{
          content: '|';
          position: absolute;
          left: -5px;
        }
      }
    }

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

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

    .item-title {
      display: inline-block;
      margin-left: 10px;

      .item-name, .item-system {
        width: 230px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }

      .item-name {
        font-size: 14px;
        font-weight: 550;
        color: rgba(0,0,0,0.85);
      }

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

    .item-link {
      float: right;
    }

    .item-footer {
      height: 30px;
      font-size: 12px;
      line-height: 30px;
      color: rgba(0,0,0,0.65);
      // border-top: 1px solid rgba(0,0,0,0.04);
    }

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

}
</style>

