<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container">
      <div class="device-overview">
        <div class="title">设备总览</div>
        <el-row>
          <el-col :span="6">
            <el-progress
              type="circle"
              :percentage="deviceOverview.sumPercent"
              :stroke-width="15"
              :width="90"
              stroke-linecap="butt"
            />
            <div class="desc">
              <div class="value">{{ deviceOverview.countSum }}</div>
              <div class="label">设备总量</div>
            </div>
          </el-col>
          <el-col :span="6">
            <el-progress
              type="circle"
              :percentage="deviceOverview.yunPercent"
              :stroke-width="15"
              :width="90"
              stroke-linecap="butt"
              color="rgb(81,211,81)"
            />
            <div class="desc">
              <div class="value">{{ deviceOverview.countYun }}</div>
              <div class="label">当前运行中</div>
            </div>
          </el-col>
          <el-col :span="6">
            <el-progress
              type="circle"
              :percentage="deviceOverview.tingPercent"
              :stroke-width="15"
              :width="90"
              stroke-linecap="butt"
              color="rgb(254,192,61)"
            />
            <div class="desc">
              <div class="value">{{ deviceOverview.countTing }}</div>
              <div class="label">当前空闲</div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="run-day">
              <div class="">整体运行天数</div>
              <div class="">{{ deviceOverview.countDay }}<span class="unit">天</span></div>
            </div>
            <div class="run-time">
              <div class="">开始运行 <span>{{ deviceOverview.state }}</span></div>
              <div class="">最近运行 <span>{{ deviceOverview.end }}</span></div>
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="device-run-trend">
        <div class="title">打印设备运行趋势</div>
        <el-card>
          <LineChart
            :xaxis="deviceTrend.xaxis"
            :chart-data="deviceTrend.data"
          />
        </el-card>
      </div>
      <div class="device-print-task">
        <div class="title" style="display: flex;justify-content: space-between;align-items: center;">
          设备打印任务
          <span>
            <DateRangePicker v-model="buildDate" @change="changeDate" />
            <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetDate">重置</el-button>
          </span>
        </div>
        <!--表格渲染-->
        <el-table
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          :max-height="350"
          style="width: 100%;margin-top: 10px;"
        >
          <el-table-column :show-overflow-tooltip="true" prop="buildDate" label="时间">
            <template slot-scope="{ row }">
              {{ row.buildDate ? row.buildDate.slice(0, 10) : '' }}
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="name" label="设备名称" />
          <el-table-column :show-overflow-tooltip="true" prop="code" label="设备ID" />
          <el-table-column :show-overflow-tooltip="true" prop="floor.floorName" label="所在楼层" />
          <el-table-column :show-overflow-tooltip="true" prop="status" width="135" label="当前状态">
            <template slot-scope="{ row }">
              {{ row.status === 'true' ? '运行' : '空闲' }}
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="unPrintingCount" width="135" label="待打印任务数" />
          <el-table-column :show-overflow-tooltip="true" prop="yiPrintingCount" width="135" label="已打印任务数" />
        </el-table>
        <!--分页组件-->
        <pagination />
      </div>
    </div>
  </div>
</template>

<script>
import crudHome from '@/api/home'
import { deviceOverview, deviceTrend } from '@/api/home'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import LineChart from '@/views/dashboard/LineChart'

import CRUD, { presenter, header, crud } from '@crud/crud'

export default {
  name: 'Dashboard',
  components: {
    pagination,
    DateRangePicker,
    LineChart
  },
  cruds() {
    return CRUD({ title: '文印管理系统-首页', url: 'api/dmDevice/selectDevice', idField: 'id', sort: '', crudMethod: { ...crudHome }})
  },
  mixins: [presenter(), header(), crud()],
  data() {
    return {
      buildDate: [],
      // 设备总览
      deviceOverview: {
        countDay: '',
        countSum: '',
        countTing: '',
        countYun: '',
        end: '',
        state: '',
        sumPercent: 0,
        yunPercent: 0,
        tingPercent: 0
      },
      // 打印设备运行趋势
      deviceTrend: {
        xaxis: [],
        data: []
      }
    }
  },
  created() {
    this.getDeviceOverview()
    this.getDeviceTrend()
  },
  methods: {
    changeDate() {
      this.crud.query.buildStateDate = this.buildDate[0] || ''
      this.crud.query.buildEndDate = this.buildDate[1] || ''
      this.crud.toQuery()
    },
    resetDate() {
      this.buildDate = []
      this.changeDate()
    },
    // 设备总览
    getDeviceOverview() {
      deviceOverview().then(res => {
        this.deviceOverview.countDay = res.countDay
        this.deviceOverview.countSum = res.countSum
        this.deviceOverview.countTing = res.countTing
        this.deviceOverview.countYun = res.countYun
        this.deviceOverview.end = res.end
        this.deviceOverview.state = res.state
        // this.deviceOverview.sumPercent = res.countDay
        this.deviceOverview.yunPercent = Number((res.countYun / res.countSum * 100).toFixed(2))
        this.deviceOverview.tingPercent = Number((res.countTing / res.countSum * 100).toFixed(2))
      })
    },
    // 打印设备运行趋势
    getDeviceTrend() {
      deviceTrend().then(res => {
        const xaxis = []
        const data = []
        for (const item of res) {
          xaxis.push(item.date)
          data.push({
            value: item.num
          })
        }
        this.deviceTrend.xaxis = xaxis
        this.deviceTrend.data = data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .dashboard-editor-container {
    padding: 10px 15px;
    background-color: rgb(240, 242, 245);
    position: relative;
    .title {
      font-size: 14px;
      font-weight: 600;
      margin-bottom: 0;
    }

    .device-overview {
      width: 100%;
      height: 150px;
      background-color: #FFF;
      padding: 15px;
      border: 1px sloid rgb(233,233,233);
      .el-row {
        padding: 10px 0;
        .el-col {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          &:nth-child(3) {
            border-right: 1px solid rgb(233,233,233);
          }

          .desc {
            margin-left: 20px;
            .value {
              font-size: 24px;
              font-weight: 600;
              color: rgb(102,103,112);
              text-align: center;
            }
            .label {
              font-size: 16px;
              margin-top: 5px;
              color: rgb(159,159,159);
            }
          }
          &:nth-child(4) {
            padding: 0 10px;
            display: flex;
            flex-direction: column;
            .run-day {
              width: 100%;
              height: 63px;
              border-bottom: 1px solid rgb(233,233,233);
              display: flex;
              flex-direction: column;
              justify-content: center;
              align-items: center;
              >div:nth-child(1) {
                font-size: 14px;
                color: rgb(159,159,159);
                text-align: center;
              }
              >div:nth-child(2) {
                color: rgb(41,180,242);
                font-size: 24px;
                font-weight: 600;
                text-align: center;
                .unit {
                  color: rgb(102,103,112);
                  font-size: 14px;
                }
              }
            }
            .run-time {
              width: 100%;
              height: 27px;
              display: flex;
              justify-content: space-between;
              align-items: center;
              div {
                color: rgb(159,159,159);
                font-size: 12px;
                span {
                  color: rgb(102,103,112);
                }
              }
            }
          }
        }
      }
    }
    .device-run-trend {
      height: 300px;
      background-color: #FFF;
      padding: 15px;
      border: 1px solid rgb(233,233,233);
      margin-top: 15px;

      .el-card {
        width: 100%;
        height: 240px;
        margin-top: 10px;

        ::v-deep .el-card__body {
          width: 100%;
          height: 100%;
        }
      }
    }
    .device-print-task {
      height: 450px;
      background-color: #FFF;
      padding: 15px;
      border: 1px solid rgb(233,233,233);
      margin-top: 15px;
      margin-bottom: 40px;

      ::v-deep .el-pagination {
        text-align: right;
      }
    }
  }
</style>
