<template>
  <div v-loading="loading" class="ems-dashboard-editor-container" style="background-color: #ffffff">
    <el-form ref="form" :model="form" label-position="top">
      <el-form-item
          label="站点"
          prop="siteId"
          :rules="[{ required: true, message: '请选择站点' }]"
      >
        <el-checkbox-group v-model="form.siteId">
          <el-checkbox v-for="(item,index) in siteList" :key="index+'zdListSearch'" :label="item.siteId">
            {{ item.siteName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="设备" prop="deviceCategory" :rules="[{ required: true, message: '请选择设备' }]">
        <el-radio-group v-model="form.deviceCategory" >
          <el-radio v-for="([value,name],index) in deviceCategoryList" :key="index+'sbListSearch'" :label="value">
            {{ name }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <div style="display: flex">
        <el-form-item label="点位" prop="point" :rules="[{ required: true, message: '请输入点位' }]" style="width: 200px;margin-right: 50px">
          <el-input
            v-model="form.point"
            placeholder="请输入点位"
            type="text"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="横坐标" prop="xAxios" :rules="[{ required: true, message: '请选择横坐标' }]">
          <el-radio-group v-model="form.xAxios">
            <el-radio label="hour">时</el-radio>
            <el-radio label="minute">分</el-radio>
            <el-radio label="second">秒</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm">生成图表</el-button>
      </el-form-item>
    </el-form>
    <el-card shadow="always" class="common-card-container common-card-container-body-no-padding time-range-card">
      <div slot="header" class="time-range-header">
        <span class="card-title"></span>
        <date-range-select ref="dateRangeSelect" @updateDate="updateDate"/>
      </div>
      <div style="height: 310px" id="searchChart"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import resize from '@/mixins/ems/resize'
import {mapState} from "vuex";
import {getAllSites} from "@/api/ems/zddt";
import DateRangeSelect from "@/components/Ems/DateRangeSelect/index.vue";
export default {
  name: "Search",
  mixins: [resize],
  components: {DateRangeSelect},
  computed: {
    ...mapState({
      deviceCategoryOptions: state => state?.ems?.deviceCategoryOptions || {},
    }),
    //设备列表
    deviceCategoryList() {
      // deviceCategoryOptions:{'PCS':'PCS','STACK':'电池堆','CLUSTER':'电池簇','COOLING':'液冷','BATTERY':'单体电池','AMMETER':'电表'},//设备类别
      return Object.entries(this.deviceCategoryOptions).filter(([key, value]) => {
        return ['STACK', 'CLUSTER', 'BATTERY', 'AMMETER'].includes(key)
      })
    },
  },
  data() {
    return {
      chart: null,
      timeRange:[],
      siteList: [],//站点列表
      form: {
        siteId: [],//当前选中的站点id 默认选中第一个站点
        deviceCategory: '',//设备
        point: '',//点位
        xAxios: '',//横坐标
      },
      loading: false,
    }
  },
  methods: {
    showLoading(){
      this.chart && this.chart.showLoading()
    },
    hideLoading(){
      this.chart && this.chart.hideLoading()
    },
    initChart() {
      this.chart = echarts.init(document.querySelector('#searchChart'))
    },
    // 更新时间范围 重置图表
    updateDate(data){
      this.timeRange=data
      this.getDate()
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        valid && this.getDate()
      })
    },
    getZdList() {
      return getAllSites().then(response => {
        this.siteList = response.data || []
        console.log("获取站点列表返回数据", this.siteList)
        // this.siteList.length > 0 && (this.form.siteId = [this.siteList[0].siteId])
      }).finally(() => {
        this.loading = false;
        this.searchLoading = false
      })
    },
    getDate(){

    },
    init() {

    },
  },
  mounted() {
    this.getZdList()
    this.initChart()
    this.$refs.dateRangeSelect.init()
  }
}
</script>

<style lang="scss" scoped>

</style>
