<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      label-position="left"
    >
      <el-form-item label="项目" prop="projectId">
        <el-select
          v-model="form.projectId"
          size="small"
          style="width: 100%"
          filterable
          placeholder="项目"
          @change="projectChange()"
        >
          <el-option
            v-for="item in projectData"
            :key="item.id"
            style="width: 100%"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="站点" prop="deviceId">
        <el-select
          v-model="form.deviceId"
          size="small"
          style="width: 100%"
          filterable
          placeholder="站点"
          @change="devicedChange()"
        >
          <el-option
            v-for="item in deviceData"
            :key="item.id"
            style="width: 100%"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="传感器" prop="sensorId">
        <el-select
          v-model="form.sensorId"
          size="small"
          style="width: 100%"
          filterable
          placeholder="传感器"
        >
          <el-option
            v-for="item in sensorData"
            :key="item.id"
            style="width: 100%"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报警级别" prop="alarmLevel">
        <el-select
          v-model="form.alarmLevel"
          size="small"
          style="width: 100%"
          filterable
          placeholder="报警级别"
        >
          <el-option
            v-for="item in levelData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报警类别" prop="alarmCategory">
        <el-select
          v-model="form.alarmCategory"
          size="small"
          style="width: 100%"
          filterable
          placeholder="报警类别"
        >
          <el-option
            v-for="item in categoryData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报警内容" prop="alarmContent">
        <el-input
          v-model="form.alarmContent"
          size="small"
          type="textarea"
          :rows="2"
          placeholder="报警内容"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button size="small" @click="close">取 消</el-button>
      <el-button type="primary" size="small" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doAdd, alarmLevelDict, alarmCategoryDict } from '@/api/alarm';
import { getDeviceData } from '@/api/device';
import { getList as getProjectList } from '@/api/project';
import { getList as getSensorData } from '@/api/sensor';
export default {
  data() {
    return {
      form: {
        projectId: '',
        deviceId: '',
        sensorId: '',
        alarmContent: '',
        alarmLevel: '',
        alarmCategory: '',
      },
      rules: {
        projectId: [
          { required: true, trigger: 'change', message: '请选择项目' },
        ],
        deviceId: [
          { required: true, trigger: 'change', message: '请选择站点' },
        ],
        sensorId: [
          { required: true, trigger: 'change', message: '请选择传感器' },
        ],
        alarmContent: [
          { required: true, trigger: 'blur', message: '请输入报警内容' },
        ],
        alarmLevel: [
          { required: true, trigger: 'change', message: '请选择报警级别' },
        ],
        alarmCategory: [
          { required: true, trigger: 'change', message: '请选择报警类别' },
        ],
      },
      title: '',
      projectData: [],
      levelData: [],
      categoryData: [],
      deviceData: [],
      sensorData: [],
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    handleCountryChange(value) {
      if (value) {
        const len = value.length - 1;
        this.form.parentId = value[len];
      } else {
        this.form.parentId = 0;
      }
    },
    //项目
    async getProjectData() {
      const { data } = await getProjectList({ page: 1, size: 100 });
      this.projectData = data.items;
    },
    projectChange() {
      this.getDevicedData();
    },
    devicedChange(row) {
      this.getSensorData();
    },
    //站点
    async getDevicedData() {
      var parem = {
        page: 1,
        size: 100,
        projectId: this.form.projectId,
      };
      const { data } = await getDeviceData(parem);
      this.deviceData = data.items;
      // if (this.deviceData != []) {
      //   this.form.deviceId = this.deviceData[0].id;
      //   this.getSensorData();
      // }
    },
    //传感器
    async getSensorData() {
      var parem = {
        page: 1,
        size: 100,
        deviceId: this.form.deviceId,
      };
      const { data } = await getSensorData(parem);
      this.sensorData = data.items;
      // if (this.sensorData != []) {
      //   this.form.sensorId = this.sensorData[0].id;
      // }
    },
    //包括类型
    async getLevel() {
      const { data } = await alarmLevelDict();
      this.levelData = data;
    },
    //报警类别
    async getCategory() {
      const { data } = await alarmCategoryDict();
      this.categoryData = data;
    },
    showEdit() {
      this.title = '添加报警信息';
      // const projectId = sessionStorage.getItem('projectId');
      // this.form.projectId = parseInt(projectId);
      this.dialogFormVisible = true;
      this.getProjectData();
      this.getLevel();
      this.getCategory();
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.form.name = this.form.title;
          const { msg } = await doAdd(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
