<template>
  <Dialog
    title="扬尘监测历史记录"
    :visible="dialogVisible"
    width="70%"
    @change-visible="changeVisible"
  >
    <div class="dialog-body-container">
      <el-form inline :model="searchForm" class="search-form">
        <el-form-item label="时间：">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd"
            @change="changeTime"
          />
        </el-form-item>
        <el-form-item label="噪音：">
          <el-select v-model="searchForm.noise_status" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="超标" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="PM2.5：">
          <el-select v-model="searchForm.pm25_status" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="超标" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="PM10：">
          <el-select v-model="searchForm.pm10_status" clearable>
            <el-option label="正常" value="0"></el-option>
            <el-option label="超标" value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
      <DialogApiDataTable
        :has-index="true"
        ref="table"
        :columns="columns"
        :parameters="searchForm"
        :api="api"
        :auto-load="false"
        :path-id="projectId"
      >
        <template #pm25="{ data }">
          <span
            :class="[
              data.pm25_qualified == 0
                ? 'green'
                : data.pm25_qualified == 1
                ? 'yellow'
                : 'red',
            ]"
          >
            {{ data.pm25 }}
          </span>
        </template>
        <template #pm10="{ data }">
          <span
            :class="[
              data.pm10_qualified == 0
                ? 'green'
                : data.pm10_qualified == 1
                ? 'yellow'
                : 'red',
            ]"
          >
            {{ data.pm10 }}
          </span>
        </template>
        <template #noise="{ data }">
          <span
            :class="[
              data.noise_qualified == 0
                ? 'green'
                : data.noise_qualified == 1
                ? 'yellow'
                : 'red',
            ]"
          >
            {{ data.pm10 }}
          </span>
        </template>
      </DialogApiDataTable>
    </div>
  </Dialog>
</template>
  <script>
import { getDustHistory } from "@/api/index";
export default {
  name: "HistoryDust",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      searchForm: {},
      dateRange: [],
      columns: [
        { label: "更新时间", prop: "update_time" },
        { label: "温度(℃)", prop: "temperature" },
        { label: "湿度/%", prop: "humidity" },
        { label: "PM2.5/ug/m³", prop: "pm25", hasSlot: true },
        { label: "PM10/ug/m³", prop: "pm10", hasSlot: true },
        { label: "噪音/dB", prop: "noise", hasSlot: true },
      ],
      api: getDustHistory,
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("change-visible", val);
      },
    },
  },
  methods: {
    changeVisible(val) {
      console.log(val);
      this.dialogVisible = val;
    },
    handleSearch() {
      this.$refs.table.getData();
    },
    changeTime(tr) {
      this.searchForm.date_value_start = tr[0] ? tr[0] : "";
      this.searchForm.date_value_end = tr[1] ? tr[1] : "";
    },
  },

  watch: {
    visible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.table.getData();
        });
      }
    },
  },
};
</script>
      <style lang="less" scoped>
::v-deep .search-form .el-select {
  width: 100px !important;
}

::v-deep .el-input .el-input__inner {
  background: #072350 !important;
  border: none !important;

  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}
.dialog-body-container {
  height: calc(100% - 50px);
}
.icon-img {
  width: 50px;
  vertical-align: top;
  cursor: pointer;
}
.green {
  color: #1ecc6c;
}
.yellow {
  color: #e17e27;
}
.red {
  color: #ff6151;
}
</style>