<template>
  <div class="visitor">
    <layout>
      <template #left>
        <div class="left_visitor">
          <div class="flex_spection" style="height:50%;">
            <HeadlineTag value="访客登记信息列表"></HeadlineTag>
            <div class="box_arch_point">
              <div class="point_box" style="margin-top: 10px;margin-left: 15px;">
                <el-input v-model="input" placeholder="按访客名称搜索" />
              </div>
              <div style="height: 100%;" v-if="leftData.VisitorRegistration && leftData.VisitorRegistration.length > 0">
                <register :resultData="leftData.VisitorRegistration"></register>
              </div>
              <Empty :bottom="true" v-else></Empty>
              <!-- <div style="height: calc(100%);">
                <Empty></Empty>
              </div> -->
              <!-- <div class="center_in">
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                  layout="total, prev, pager, next" @pagination="getList" />
              </div> -->
            </div>
          </div>
          <div class="flex_spection" style="height:50%;">
            <HeadlineTag value="访客刷卡记录列表"></HeadlineTag>
            <div class="box_arch_point">
              <div class="point_box" style="margin-top: 10px;margin-left: 15px;">
                <el-input v-model="input" placeholder="按访客名称搜索" />
              </div>
              <div style="height: 100%;" v-if="leftData.VisitorsSwipeCards && leftData.VisitorsSwipeCards.length > 0">
                <swiping :resultData="leftData.VisitorsSwipeCards"></swiping>
              </div>
              <Empty :bottom="true" v-else></Empty>
              <!-- <div style="height: calc(100%);">
                <Empty></Empty>
              </div> -->
              <!-- <div class="center_in">
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                  layout="total, prev, pager, next" @pagination="getList" />
              </div> -->
            </div>
          </div>
        </div>
      </template>
      <template #right>
        <div class="right_visitor">
          <div class="flex_spection">
            <HeadlineTag type="right" value="今日访客统计"></HeadlineTag>
            <div class="box_arch">
              <!-- <starView></starView> -->
              <caller :resultData="leftData"></caller>
            </div>
          </div>
          <div class="flex_spection">
            <HeadlineTag type="right" value="通行趋势"></HeadlineTag>
            <div class="card_visitor_star">
              <transit :resultData="leftData"></transit>
            </div>
          </div>
          <div class="flex_spection">
            <HeadlineTag type="right" value="访客身份类型占比"></HeadlineTag>
            <div class="box_arch">
              <div ref="chartDom" style="width: 100%;height: 100%;" />
            </div>
          </div>
        </div>
      </template>
    </layout>
  </div>
</template>

<script setup>
import { getVisitor } from "@/api/system/visitor"
import layout from "@/components/layout_/index.vue";
import HeadlineTag from '@/components/HeadlineTag'
import Empty from '@/components/Empty'
import starView from './starView'
import transit from './transit.vue'
import caller from './caller.vue'
import register from './register.vue'
import swiping from './swiping.vue'
import * as echarts from 'echarts'
const total = ref(0);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
const input = ref('')
/** 查询列表 */
function getList() {
};

const chartDom = ref(null);
let chartInstance = null;
// 初始化图表
const initChart = (valueData) => {
  var colorList = [{
    type: 'linear',
    x: 0,
    y: 0,
    x2: 1,
    y2: 1,
    colorStops: [{
      offset: 0,
      color: 'rgba(51,192,205,0.01)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: 'rgba(51,192,205,0.57)' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  },
  {
    type: 'linear',
    x: 1,
    y: 0,
    x2: 0,
    y2: 1,
    colorStops: [{
      offset: 0,
      color: 'rgba(115,172,255,0.02)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: 'rgba(115,172,255,0.67)' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  },
  {
    type: 'linear',
    x: 1,
    y: 0,
    x2: 0,
    y2: 0,
    colorStops: [{
      offset: 0,
      color: 'rgba(158,135,255,0.02)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: 'rgba(158,135,255,0.57)' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  },
  {
    type: 'linear',
    x: 0,
    y: 1,
    x2: 0,
    y2: 0,
    colorStops: [{
      offset: 0,
      color: 'rgba(252,75,75,0.01)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: 'rgba(252,75,75,0.57)' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  },
  {
    type: 'linear',
    x: 1,
    y: 1,
    x2: 1,
    y2: 0,
    colorStops: [{
      offset: 0,
      color: 'rgba(253,138,106,0.01)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: '#FDB36ac2' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  },
  {
    type: 'linear',
    x: 0,
    y: 0,
    x2: 1,
    y2: 0,
    colorStops: [{
      offset: 0,
      color: 'rgba(254,206,67,0.12)' // 0% 处的颜色
    },
    {
      offset: 1,
      color: '#FECE4391' // 100% 处的颜色
    }
    ],
    globalCoord: false // 缺省为 false
  }
  ]
  var colorLine = ['#33C0CD', '#73ACFF', '#9E87FF', '#FE6969', '#FDB36A', '#FECE43']
  function getRich() {
    let result = {}
    colorLine.forEach((v, i) => {
      result[`hr${i}`] = {
        backgroundColor: colorLine[i],
        borderRadius: 3,
        width: 3,
        height: 3,
        padding: [3, 3, 0, -12]
      }
      result[`a${i}`] = {
        padding: [-11, 6, -20, 6],
        color: colorLine[i],
        backgroundColor: 'transparent',
        fontSize: 12
      }
    })
    return result
  }
  let data = valueData
  data.forEach((v, i) => {
    v.labelLine = {
      lineStyle: {
        width: 1,
        color: colorLine[i]
      }
    }
  })
  if (!chartInstance) {
    chartInstance = echarts.init(chartDom.value);
  }
  chartInstance.setOption({
    series: [{
      type: 'pie',
      radius: '60%',
      center: ['50%', '50%'],
      clockwise: true,
      avoidLabelOverlap: true,
      label: {
        show: true,
        position: 'outside',
        formatter: function (params) {
          const name = params.name
          const percent = params.percent + '%'
          const index = params.dataIndex
          return [`{a${index}|${name}:${percent}}`, `{hr${index}|}`].join('\n')
        },
        rich: getRich()
      },
      itemStyle: {
        normal: {
          color: function (params) {
            return colorList[params.dataIndex]
          }
        }
      },
      data,
      roseType: 'radius'
    }]
  });
};
// 生命周期
onMounted(() => {
  intervalId.value = setInterval(getVisitorData, 10000);
  getVisitorData()
  let data = [{
    'name': '客户',
    'value': 0
  }, {
    'name': '供应商',
    'value': 0
  }, {
    'name': '面试者',
    'value': 0
  }, {
    'name': '政府人员',
    'value': 0
  }, {
    'name': '其他',
    'value': 0
  }].sort((a, b) => {
    return b.value - a.value
  })
  initChart(data)
});
onUnmounted(() => {
  clearInterval(intervalId.value);
})
const intervalId = ref(null)
const leftData = ref({})
function getVisitorData() {
  getVisitor().then((res) => {
    if (res.code == 200) {
      leftData.value = res.data
    }
  })
}
watch(() => leftData.value, (newVal) => {
  if (chartInstance) {
    let data = [{
      'name': '客户',
      'value': newVal.Client
    }, {
      'name': '供应商',
      'value': newVal.Vendor
    }, {
      'name': '面试者',
      'value': newVal.Interviewees
    }, {
      'name': '政府人员',
      'value': newVal.GovernmentPersonnel
    }, {
      'name': '其他',
      'value': newVal.Other
    }].sort((a, b) => {
      return b.value - a.value
    })
    initChart(data)
  }
}, { deep: true, immediate: true } // 开启深度监听
)
// 窗口自适应
window.addEventListener('resize', () => {
  chartInstance?.resize();
});
</script>

<style scoped lang="scss">
.visitor {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.left_visitor {
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.right_visitor {
  position: absolute;
  right: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.box_arch_point {
  height: calc(100% - 40px);
}


.garden_box {
  height: 30%;
}

.flex_spection {
  flex: 1;
  color: #fff;
}

.box_arch {
  display: flex;
  align-items: center;
  height: calc(100% - 40px);
}
</style>

<style lang="scss" scoped>
.point_box :deep(.el-input__wrapper) {
  background-color: transparent !important;
  box-shadow: 0 0 0 1px rgb(58, 86, 117) inset !important;
}

.point_box :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409EFF inset !important;
}

.point_box :deep(.el-input__inner) {
  color: #ffffff !important;
}

.card_visitor_star {
  height: calc(100% - 40px);
  display: flex;
  flex-direction: column;
}

.item_visitor_top {
  flex: 1;
  margin: 5px 0px;
  padding-left: 20px;
  display: flex;
  align-items: center;
  border-left: 2px solid rgb(67, 182, 235);
  background-image: linear-gradient(to right, rgba(15, 73, 138, 1) 0%, rgba(13, 65, 125, 0.5) 21%, rgba(13, 65, 125, 0.5) 52%, rgba(0, 0, 0, 0) 78%, rgba(0, 0, 0, 0) 100%);
}

.circle_itor {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 2px solid rgb(103, 168, 202);
}

.title_box_itor {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-left: 20px;
}

.title_itor_13 {
  color: #ffffff;
  font-size: 13px;
}

.title_itor_18 {
  padding-top: 5px;
  font-weight: bold;
  font-size: 18px;
  background-image: -webkit-linear-gradient(bottom, rgb(20, 127, 228), rgb(229, 232, 236));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}


.visitor_star {
  width: 30px;
  height: 30px;
}
</style>