<template>
  <div class="app-container home">
    <div class="box01">
      <div class="top-main">
        <span>商品扫码分析</span>
        <el-button type="primary" @click="showHandle">扫描日志</el-button>
      </div>
      <div class="chart-box">
        <div class="l01">
          <div class="title">
            <span>总扫描次数</span>
          </div>
          <div class="show-main col">
            <span class="num">{{ scanNum }}</span>
            <div class="msg">最近{{time}}更新</div>
          </div>
        </div>
        <div class="line">
          <div class="title">
            <span>扫码时间分布</span>
            <el-select v-model="select" style="width: 120px;" placeholder="请选择" @change="loadLine">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
          <div class="line-chart" id="line-index"></div>
        </div>
      </div>
    </div>
    <div class="box02">
      <div class="con">
        <div class="title">
          <span>广告</span>
        </div>
        <div class="show-main">
            <div>
              <span class="num">{{ adNum }}</span>
              <span class="spec">个</span>
            </div>
          </div>
      </div>
      <div class="con">
        <div class="title">
          <span>设备</span>
          <div class="eqiup-state">
            <div class="success"><span class="label-state">正常使用：</span>{{suc?suc:0}}台</div>
            <div class="disad"><span class="label-state">禁止使用：</span>{{dis?dis:0}}台</div>
            <!-- <div class="maintenance"><span class="label-state">维护中：</span>{{maintenance}}台</div> -->
          </div>
        </div>
        <div class="show-main">
            <div>
              <span class="num">{{ equipNum }}</span>
              <span class="spec">台</span>
            </div>
          </div>
      </div>
    </div>
    <scanModal ref="modalView" />
  </div>
</template>

<script setup name="Index">
  import {equipTotal,scanTotal,planTotal,scanTime,scanLog} from '@/api/base/home'
  import { computed,getCurrentInstance,ref,markRaw,shallowRef,defineEmits } from "vue";
  const { proxy } = getCurrentInstance();
  import * as echarts from 'echarts';
  import scanModal from './components/scanModal.vue';
  const scanNum = ref(0);
  const time = ref('');
  const adNum = ref(0);
  const equipNum = ref(0);
  const suc = ref(0)
  const dis = ref(0)
  const maintenance = ref(0)
  const select = ref('month')
  const options = ref([
    {
      label:'日',
      value:'day'
    },
    {
      label:'月',
      value:'month'
    },
    {
      label:'年',
      value:'year'
    }
  ])

  const showHandle = () => {
    proxy.$refs.modalView.setShow(true)
  }

  const initLine = data => {
    let chartDom = document.getElementById('line-index');
    let myChart = echarts.init(chartDom);
    let option;
    let dataX = [];
    let dataY = [];
    for(let i = data.length - 1; i >= 0; i--){
      let item  = data[i];
      dataX.push(item.time);
      dataY.push(item.count)
    }
    option = {
      xAxis: {
        type: 'category',
        data: dataX
      },
      yAxis: {
        type: 'value'
      },
      tooltip: {
        trigger: 'axis'
      },
      series: [
        {
          data: dataY,
          type: 'line'
        }
      ]
    };

    option && myChart.setOption(option);
  }
  const getEquip = () => {
    scanTotal().then(res => {
      scanNum.value = res.data.total;
      time.value = res.data.time;
    }).catch(() =>{})
  }

  const getAdCount = () => {
    planTotal().then(res => {
      adNum.value = res.data.total;
      // time.value = res.data.time;
    }).catch(() =>{})
  }

  const getEquipTotal = () => {
    equipTotal().then(res => {
      equipNum.value = res.data.total;
      dis.value = res.data[0]
      suc.value = res.data[1]
      maintenance.value = res.data[2]
    }).catch(() =>{})
  }

  const loadLine = () => {
    scanTime({option:select.value}).then(res => {
      initLine(res.data)
    }).catch(() =>{})
  }

  getEquip();
  loadLine();
  getAdCount();
  getEquipTotal();
</script>

<style scoped lang="scss">
.label-state{
  width: 100px;
  text-align: right;
  display: inline-block;
}
.home {
  width: calc(100% - 64px);
  height: calc(100% - 48px);
  box-sizing: border-box;
  margin: 16px 16px 32px;
  .box01{
    width: 100%;
    height: calc(60% - 16px);
    box-sizing: border-box;
    // padding: 0 24px 24px;
    margin-bottom: 16px;
    // border: solid 1px #BBB;
    .top-main{
      width: 100%;
      height: 70px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      // border-bottom: solid 1px #BBB;
      box-sizing: border-box;
      margin-bottom: 16px;
      >span{
        font-size: 28px;
        font-weight: 700;
      }
    }
    .chart-box{
      width: 100%;
      height: calc(100% - 86px);
      display: flex;
      .l01{
        width: 40%;
        height: 100%;
        box-sizing: border-box;
        background-color:#f0f8ff;
        padding: 16px;
        // border-right: solid 1px #BBB;
      }
      .line{
        width: calc(60% - 16px);
        height: 100%;
        box-sizing: border-box;
        margin-left: 16px;
        padding: 16px;
        background-color:#f0f8ff;
        .line-chart{
          height: 100%;
          width: 100%;
        }
      }
    }
  }
  .box02{
    width: 100%;
    height: 40%;
    box-sizing: border-box;
    // padding: 16px;
    // border: solid 1px #BBB;
    display: flex;
    .eqiup-state{
      font-size: 12px;
      .success{
        color: #00AE66;
        padding-bottom: 4px;
      }
      .disad{
        color: #f00;
        padding-bottom: 4px;
      }
      .maintenance{
        color: dodgerblue;
      }
    }
    
    .con{
      width: calc(50% - 8px);
      height: 100%;
      box-sizing: border-box;
      padding: 16px;
      background-color:#f0f8ff;
      .spec{
        font-size: 18px;
        padding-left: 16px;
      }
    }
    .con:first-child{
      margin-right: 8px;
      // border-right: solid 1px #BBB;
    }
    .con:last-child{
      margin-left: 8px;
    }
  }
  .title{
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    >span{
      font-size: 18px;
    }
  }
  .show-main{
    height: calc(100% - 50px);
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .num{
      font-size: 72px;
    }
    .msg{
      font-size: 14px;
      color: #666;
    }
  }
  .col{
    flex-direction: column;
  }
}

</style>

