<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="风机名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入风机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
  
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="风机名称" align="center" prop="deviceName" width="120"/>
      <el-table-column label="风机编号" align="center" prop="deviceId" width="120"/>
      <el-table-column label="风机安装描述" align="center" prop="installationLocation" width="120" />
      <el-table-column label="电源电压预警值(V)" align="center" prop="powerSupplyVoltage" width="140" />
      <el-table-column label="电机电流预警值(A)" align="center" prop="motorCurrent" width="140" />
      <el-table-column label="运行频率预警值(HZ)" align="center" prop="operatingFrequency" width="140" />
      <el-table-column label="设定频率预警值(HZ)" align="center" prop="setFrequency" width="140" />
      <el-table-column label="风压预警值(Kpa)" align="center" prop="windPressure" width="120" />
      <el-table-column label="风速预警值(m/s)" align="center" prop="windSpeed" width="120" />
      <el-table-column label="风量预警值(m³/s)" align="center" prop="airVolume" width="140" />
      <el-table-column label="前轴温预警值(℃)" align="center" prop="frontAxisTemperature" width="120" />
      <el-table-column label="后轴温预警值(℃)" align="center" prop="rearAxisTemperature" width="120" />
      <el-table-column label="电机温度预警值(℃)" align="center" prop="motorTemperature" width="140" />
      <el-table-column label="前振动预警值(mm/s)" align="center" prop="frontVibration" width="150" />
      <el-table-column label="后振动预警值(mm/s)" align="center" prop="rearVibration"  width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风机基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="风机名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入风机名称" />
        </el-form-item>
        <el-form-item label="风机编号" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入风机编号" />
        </el-form-item>
        <el-form-item label="风机安装描述" prop="installationLocation">
          <el-input v-model="form.installationLocation" placeholder="请输入风机安装描述" />
        </el-form-item>
        <el-divider content-position="left">设置设备参数预警值</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电源电压预警值(V)" prop="powerSupplyVoltage">
              <el-input v-model="form.powerSupplyVoltage" placeholder="请输入电源电压预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电机电流预警值(A)" prop="motorCurrent">
              <el-input v-model="form.motorCurrent" placeholder="请输入电机电流预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运行频率预警值(HZ)" prop="operatingFrequency">
              <el-input v-model="form.operatingFrequency" placeholder="请输入运行频率预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设定频率预警值(HZ)" prop="setFrequency">
              <el-input v-model="form.setFrequency" placeholder="请输入设定频率预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="风压预警值(Kpa)" prop="windPressure">
              <el-input v-model="form.windPressure" placeholder="请输入风压预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="风速预警值(m/s)" prop="windSpeed">
              <el-input v-model="form.windSpeed" placeholder="请输入风速预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="风量预警值(m³/s)" prop="airVolume">
              <el-input v-model="form.airVolume" placeholder="请输入风量预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="前轴温预警值(℃)" prop="frontAxisTemperature">
              <el-input v-model="form.frontAxisTemperature" placeholder="请输入前轴温预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="后轴温预警值(℃)" prop="rearAxisTemperature">
              <el-input v-model="form.rearAxisTemperature" placeholder="请输入后轴温预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电机温度预警值(℃)" prop="motorTemperature">
              <el-input v-model="form.motorTemperature" placeholder="请输入电机温度预警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="前振动预警值(mm/s)" prop="frontVibration">
              <el-input v-model="form.frontVibration" placeholder="请输入前振动预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后振动预警值(mm/s)" prop="rearVibration">
              <el-input v-model="form.rearVibration" placeholder="请输入后振动预警值" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getFanInfo, listFanInfo, addFanInfo, updateFanInfo, delFanInfo} from "@/api/tyler/fanDev";

  export default {
  name: "Info",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风机基本信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        deviceId: null,
        installationLocation: null,
        powerSupplyVoltage: null,
        motorCurrent: null,
        operatingFrequency: null,
        setFrequency: null,
        windPressure: null,
        windSpeed: null,
        airVolume: null,
        frontAxisTemperature: null,
        rearAxisTemperature: null,
        motorTemperature: null,
        frontVibration: null,
        rearVibration: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        deviceName: [
          { required: true, message: "风机名称不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "风机编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询风机基本信息列表 */
    getList() {
      this.loading = true;
      listFanInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceName: null,
        deviceId: null,
        installationLocation: null,
        powerSupplyVoltage: null,
        motorCurrent: null,
        operatingFrequency: null,
        setFrequency: null,
        windPressure: null,
        windSpeed: null,
        airVolume: null,
        frontAxisTemperature: null,
        rearAxisTemperature: null,
        motorTemperature: null,
        frontVibration: null,
        rearVibration: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风机基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFanInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风机基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFanInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFanInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除风机基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delFanInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  
  }
};
</script>
