<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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</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="deviceId" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="安装描述" align="center" prop="installationLocation" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.drainage_dev_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      

      <el-table-column label="液位高度预警值" align="center" prop="heghtThreshold" />
      <el-table-column label="电机电流预警值" align="center" prop="motorCurrentAlert" />
      <el-table-column label="电机轴承温度预警值" align="center" prop="motorBearingTempAlert" />
      <el-table-column label="水泵轴承温度预警值" align="center" prop="pumpBearingTempAlert" />
      <el-table-column label="水泵流量预警值" align="center" prop="pumpFlowAlert" />
      <el-table-column label="排水管路压力预警值" align="center" prop="drainagePressureAlert" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <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)"
            v-if="!scope.row.status"
          >删除</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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceId">
          <el-input :disabled="disabledType" 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-form-item label="设备类型" prop="deviceType">
          <el-select @change="deviceTypeChange" v-model="form.deviceType" placeholder="请选择设备类型" :disabled="disabledType">
            <el-option
              v-for="dict in dict.type.drainage_dev_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <div v-show="form.deviceType==='1'">
          <el-form-item label="电机电流预警值" prop="motorCurrentAlert">
            <el-input v-model="form.motorCurrentAlert" placeholder="请输入电机电流预警值" />
          </el-form-item>
          <el-form-item label="电机轴承温度预警值" prop="motorBearingTempAlert">
            <el-input v-model="form.motorBearingTempAlert" placeholder="请输入电机轴承温度预警值" />
          </el-form-item>
          <el-form-item label="水泵轴承温度预警值" prop="pumpBearingTempAlert">
            <el-input v-model="form.pumpBearingTempAlert" placeholder="请输入水泵轴承温度预警值" />
          </el-form-item>
          <el-form-item label="水泵流量预警值" prop="pumpFlowAlert">
            <el-input v-model="form.pumpFlowAlert" placeholder="请输入水泵流量预警值" />
          </el-form-item>
          <el-form-item label="排水管路压力预警值" prop="drainagePressureAlert">
            <el-input v-model="form.drainagePressureAlert" placeholder="请输入排水管路压力预警值" />
          </el-form-item>
        </div>

        <div v-show="form.deviceType==='2'">
          <el-form-item label="液位高度预警值" prop="heghtThreshold">
            <el-input v-model="form.heghtThreshold" placeholder="请输入液位高度预警值" />
          </el-form-item>
        </div>
      </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 {getInfo, listInfo, addInfo, updateInfo, delInfo } from "@/api/tyler/drainageDev";

  export default {
  name: "Info",
  dicts: ['drainage_dev_type'],
  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,
        deviceId: null,
        deviceName: null,
        installationLocation: null,
        deviceType: null,
        heghtThreshold: null,
        motorCurrentAlert: null,
        motorBearingTempAlert: null,
        pumpBearingTempAlert: null,
        pumpFlowAlert: null,
        drainagePressureAlert: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        installationLocation: [
          { required: true, message: "安装描述不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "change" }
        ]
      },
      disabledType:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排水设备信息列表 */
    getList() {
      this.loading = true;
      listInfo(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,
        deviceId: null,
        deviceName: null,
        installationLocation: null,
        deviceType: null,
        heghtThreshold: null,
        motorCurrentAlert: null,
        motorBearingTempAlert: null,
        pumpBearingTempAlert: null,
        pumpFlowAlert: null,
        drainagePressureAlert: null,
      };
      this.resetForm("form");
    },
    deviceTypeChange(val){
      // console.log(val,'val')
      if(val==1){
        this.form.heghtThreshold = null
      }else if(val==2){
        this.form.motorCurrentAlert = null
        this.form.motorBearingTempAlert = null
        this.form.pumpBearingTempAlert = null
        this.form.pumpFlowAlert = null
        this.form.drainagePressureAlert = null

      }
    },
    /** 搜索按钮操作 */
    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.disabledType = false;
      this.title = "添加排水设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabledType = true;
      this.title = "修改排水设备信息";
      this.form = { ...row };
      // console.log('form.deviceType:', this.form.deviceType); // 调试输出
      this.open = false; // 关闭弹窗
      this.$nextTick(() => {
        this.open = true; // 重新打开弹窗，强制重新渲染
      });
      // const id = row.id || this.ids
      // getInfo(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改排水设备信息";
      // });
    },
    
    /** 提交按钮 */
    submitForm() {
      if(this.form.deviceType==1){
        this.form.heghtThreshold = null
      }else if(this.form.deviceType==2){
        this.form.motorCurrentAlert = null
        this.form.motorBearingTempAlert = null
        this.form.pumpBearingTempAlert = null
        this.form.pumpFlowAlert = null
        this.form.drainagePressureAlert = null

      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
