<template>
  <div class="app-container">
    <div style="background-color: #fff;padding: 20px;border-radius: 10px; padding-bottom: 0;">
                  <p style="margin: 0; font-weight: 800;font-size: 16px;color: #343434;display: flex;align-items: center;margin-bottom: 20px;border-left: 6px solid #03BF8A;margin-left: -20px;">
                    <!-- <img src="../../../assets/images/icon_htgl_btzs.png" alt="" style="width: 16px;height: 20px;"> -->
                   <span style="margin-left: 10px;">搜索条件</span>
                  </p>
                  <div style="padding-bottom: 0;margin-bottom: 20px;">
                    <el-form class="qucheu" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
                      <el-row>

                        <el-col :span="24">
                          <el-col :span="8">
                            <el-form-item label="设备编号" prop="equipmentNum">
                                    <el-input
                                      v-model="queryParams.equipmentNum"
                                      placeholder="请输入设备编号"
                                      clearable
                                      @keyup.enter.native="handleQuery"
                                    />
                                  </el-form-item>
                          </el-col>
                          <el-col :span="8">
                           <el-form-item label="设备名称" prop="equipmentName">
                                   <el-input
                                   style="width: 100%;"
                                     v-model="queryParams.equipmentName"
                                     placeholder="请输入设备名称"
                                     clearable
                                     @keyup.enter.native="handleQuery"
                                   />
                                 </el-form-item>
                          </el-col>
                          <el-col :span="8">
                             <el-form-item label="安装时间" prop="equipmentTime">
                                     <el-date-picker style="width: 100%;" clearable
                                       v-model="queryParams.equipmentTime"
                                       type="date"
                                       value-format="yyyy-MM-dd"
                                       placeholder="请选择安装时间">
                                     </el-date-picker>
                                   </el-form-item>
                          </el-col>

                        </el-col>
                        <el-col :span="8">
                          <el-form-item label="运行状态" prop="runState">
                                 <el-select v-model="queryParams.manualType" placeholder="请选择运行状态" clearable>
                                    <el-option
                                    v-for="dict in dict.type.sys_job_status"
                                                                        :key="dict.value"
                                                                        :label="dict.label"
                                                                        :value="dict.value"
                                                                      />
                                                                    </el-select>
                                </el-form-item>

                        </el-col>

                        <el-col :span="8">
                          <el-form-item label="设备地址" prop="equipmentAddress">
                                  <el-input
                                    v-model="queryParams.equipmentAddress"
                                    placeholder="请输入设备地址"
                                    clearable
                                    @keyup.enter.native="handleQuery"
                                  />
                                </el-form-item>
                        </el-col>

                        <el-col :span="5">
                          <el-form-item class="butt" style="border: 0;">
                            <el-button style="background-color: #03BF8A; border-color: #03BF8A;" type="primary" size="mini"  @click="handleQuery">查询 </el-button>
                            <el-button size="mini"  @click="resetQuery">重置</el-button>
                          </el-form-item>
                        </el-col>
                      </el-row>

                    </el-form>
                  </div>
                </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:equipmentManage:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:equipmentManage:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:equipmentManage:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableMaxHeight" v-loading="loading" :data="equipmentManageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="equipmentNum" show-overflow-tooltip/>
            <el-table-column label="设备名称" align="center" prop="equipmentName" show-overflow-tooltip/>
            <el-table-column label="设备类型" align="center" prop="equipmentType" show-overflow-tooltip>
              <template slot-scope="scope">
                <dict-tag :options="dict.type.shebie_type" :value="scope.row.equipmentType"/>
              </template>
            </el-table-column>
            <el-table-column label="设备地址" align="center" prop="equipmentAddress" show-overflow-tooltip />
            <el-table-column label="设备区域" align="center" prop="equipmentRegion" show-overflow-tooltip />
            <el-table-column label="安装时间" align="center" prop="equipmentTime" width="180">
              <template slot-scope="scope">
                <span>{{ scope.row.equipmentTime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="设备状态" align="center" key="runState" >
              <template slot-scope="scope">
                <el-switch v-model="scope.row.runState" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="设备图片" align="center" prop="equipmentImage" width="100">
              <template slot-scope="scope">
                <image-preview :src="scope.row.equipmentImage" :width="50" :height="50"/>
              </template>
            </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="width: 42px;height: 26px;"
           type="success"
           plain
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:equipmentManage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            style="width: 42px;height: 26px;background: #FFF1EA;
                   border-radius: 4px;border: 1px solid #FE570E;color: #FE570E;"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:equipmentManage:remove']"
          >删除</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="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="equipmentNum">
                      <el-input v-model="form.equipmentNum" placeholder="请输入设备编号" />
                    </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="equipmentName">
                      <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
                    </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备区域" prop="equipmentRegion">
                      <el-input v-model="form.equipmentRegion" placeholder="请输入设备区域" />
                    </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备地址" prop="equipmentAddress">
                      <el-input v-model="form.equipmentAddress" placeholder="请输入设备地址" />
                    </el-form-item>
          </el-col>

          <el-col :span="12">
             <el-form-item label="安装时间" prop="equipmentTime">
                      <el-date-picker style="width: 100%;" clearable
                        v-model="form.equipmentTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择安装时间">
                      </el-date-picker>
                    </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="运行状态" prop="runState">
               <el-select style="width: 100%;" v-model="form.runState" placeholder="请选择运行状态" >
                    <el-option
                      v-for="dict in dict.type.sys_job_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                       />
                    </el-select>
             </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="设备类型" prop="equipmentType">
                       <el-select style="width: 100%;"  v-model="form.equipmentType" placeholder="请选择设备类型">
                         <el-option
                           v-for="dict in dict.type.shebie_type"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                         ></el-option>
                       </el-select>
                     </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
                      <el-input v-model="form.remark"  placeholder="请输入内容" />
                    </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备图片" prop="equipmentImage">
                      <image-upload v-model="form.equipmentImage"/>
                    </el-form-item>
          </el-col>

        </el-row>
      </el-form>
       <div slot="footer" class="dialog-footer">
              <el-button style="background-color:#03BF8A ; border-color: #03BF8A;" type="primary" @click="submitForm">确 定</el-button>
              <el-button style="background-color:#DDEBE7 ; border-color: #DDEBE7; color: #475669;" @click="cancel">取 消</el-button>
            </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipmentManage, getEquipmentManage, delEquipmentManage, addEquipmentManage, updateEquipmentManage } from "@/api/manage/equipmentManage"

export default {
  name: "EquipmentManage",
  dicts: ['sys_job_status', 'sys_notice_type','shebie_type'],
  data() {
    return {
      // 遮罩层
      // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 设备管理表格数据
            equipmentManageList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
              pageNum: 1,
              pageSize: 10,
              equipmentNum: null,
              equipmentName: null,
              equipmentType: null,
              equipmentAddress: null,
              equipmentRegion: null,
              equipmentTime: null,
              equipmentImage: null,
              runState: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
      tableMaxHeight:'200',
    }
  },
  created() {
    this.getList()
   window.onresize = () => {
        	      this.changeTableMaxHeight()
        	    }
        	    this.changeTableMaxHeight()
      },
      mounted() {
      	window.onresize = () => {
      	          this.changeTableMaxHeight()
      	        }
      	        this.changeTableMaxHeight()
      },
  methods: {
    /** 查询设备管理列表 */
        getList() {
          this.loading = true
          listEquipmentManage(this.queryParams).then(response => {
            this.equipmentManageList = response.rows
            this.total = response.total
            this.loading = false
          })
        },
        // 取消按钮
        cancel() {
          this.open = false
          this.reset()
        },
        // 获取当前时间
        gettime(){
          let nowDate = new Date()
          				  let date = {
          				    year: nowDate.getFullYear(),
          				    month: nowDate.getMonth() + 1,
          				    date: nowDate.getDate()
          				  }
                     if (parseInt(date.month) < 10) {
                        date.month = '0' + date.month
                      }
          				   if (parseInt(date.date) < 10) {
          				      date.date = '0' + date.date
          				    }
                      let nghe = date.year + '-' + date.month + '-' + date.date
          				  return nghe

        },
        // 表单重置
        reset() {
          this.form = {
            equipmentId: null,
            equipmentNum: null,
            equipmentName: null,
            equipmentType: null,
            equipmentAddress: null,
            equipmentRegion: null,
            equipmentTime: this.gettime(),
            equipmentImage: null,
            runState: '0',
            delFlag: null,
            createBy: null,
            createTime: null,
            updateBy: null,
            updateTime: null,
            remark: 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.equipmentId)
          this.single = selection.length!==1
          this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
          this.reset()
          this.open = true
          this.title = "添加设备管理"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
          this.reset()
          const equipmentId = row.equipmentId || this.ids
          getEquipmentManage(equipmentId).then(response => {
            this.form = response.data
            this.open = true
            this.title = "修改设备管理"
          })
        },
        /** 提交按钮 */
        submitForm() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.equipmentId != null) {
                updateEquipmentManage(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功")
                  this.open = false
                  this.getList()
                })
              } else {
                addEquipmentManage(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功")
                  this.open = false
                  this.getList()
                })
              }
            }
          })
        },
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用"
      updateEquipmentManage(row).then(response => {
        this.$modal.msgSuccess("操作成功")
        row.status = row.status === "0" ? "1" : "0"
        this.getList()

      })
    },
    /** 删除按钮操作 */
        handleDelete(row) {
          const equipmentIds = row.equipmentId || this.ids
          this.$modal.confirm('是否确认删除设备管理数据项？').then(function() {
            return delEquipmentManage(equipmentIds)
          }).then(() => {
            this.getList()
            this.$modal.msgSuccess("删除成功")
          }).catch(() => {})
        },
         handleExport() {
              this.download('system/equipmentManage/export', {
                ...this.queryParams
              }, `equipmentManage_${new Date().getTime()}.xlsx`)
            },
    // 获取屏幕高度
                showFilterForm () {
                      this.filterActive = !this.filterActive
                      this.changeTableMaxHeight()
                    },

                    changeTableMaxHeight () {
                      let height = document.body.offsetHeight // 网页可视区域高度
                      // if (this.filterActive) {
                      //   this.tableMaxHeight = height - 320
                      // } else {
                        this.tableMaxHeight = height - 250
                      // }
                      console.log(height)
                    }
  }
}
</script>
<style>
  .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th{
    background-color:#F5F6F8;
  }
  .el-checkbox__input.is-checked .el-checkbox__inner{
    background-color: #03BF8A;
    border-color: #03BF8A;
  }
  .el-checkbox__input.is-indeterminate .el-checkbox__inner{
    background-color: #03BF8A;
    border-color: #03BF8A;
  }


</style>
