<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="fEquipmentCode">
        <el-input
          v-model="queryParams.fEquipmentCode"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="fEquipmentName">
        <el-input
          v-model="queryParams.fEquipmentName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型：1.老化柜、2.标定车、3.机械臂、4.镭雕机、5.AOI检测设备" prop="fEquipmentType">
        <el-select v-model="queryParams.fEquipmentType" placeholder="请选择设备类型：1.老化柜、2.标定车、3.机械臂、4.镭雕机、5.AOI检测设备" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="厂家" prop="fEquipmentFactory">
        <el-input
          v-model="queryParams.fEquipmentFactory"
          placeholder="请输入厂家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="fBeyongDepartment">
        <el-input
          v-model="queryParams.fBeyongDepartment"
          placeholder="请输入所属部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP地址" prop="fIp">
        <el-input
          v-model="queryParams.fIp"
          placeholder="请输入IP地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口号" prop="fPort">
        <el-input
          v-model="queryParams.fPort"
          placeholder="请输入端口号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="fInstallLocation">
        <el-input
          v-model="queryParams.fInstallLocation"
          placeholder="请输入安装位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否巡查点：1.是，2.否" prop="fIsInspect">
        <el-input
          v-model="queryParams.fIsInspect"
          placeholder="请输入是否巡查点：1.是，2.否"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="fResponsiblePerson">
        <el-input
          v-model="queryParams.fResponsiblePerson"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="fResponsiblePersonMobile">
        <el-input
          v-model="queryParams.fResponsiblePersonMobile"
          placeholder="请输入负责人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="fCreateTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.fCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="fUpdateTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.fUpdateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态：0在线，1离线，2故障" prop="fStatus">
        <el-select v-model="queryParams.fStatus" placeholder="请选择状态：0在线，1离线，2故障" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="报警时间" prop="fAlarmTime">
        <el-input
          v-model="queryParams.fAlarmTime"
          placeholder="请输入报警时间"
          clearable
          size="small"
          @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"
          v-hasPermi="['equipment:equipment: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="['equipment:equipment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:equipment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['equipment:equipment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="生产设备id" align="center" prop="fEquipmentId" />
      <el-table-column label="设备编号" align="center" prop="fEquipmentCode" />
      <el-table-column label="设备名称" align="center" prop="fEquipmentName" />
      <el-table-column label="设备类型：1.老化柜、2.标定车、3.机械臂、4.镭雕机、5.AOI检测设备" align="center" prop="fEquipmentType" />
      <el-table-column label="厂家" align="center" prop="fEquipmentFactory" />
      <el-table-column label="所属部门" align="center" prop="fBeyongDepartment" />
      <el-table-column label="IP地址" align="center" prop="fIp" />
      <el-table-column label="端口号" align="center" prop="fPort" />
      <el-table-column label="安装位置" align="center" prop="fInstallLocation" />
      <el-table-column label="是否巡查点：1.是，2.否" align="center" prop="fIsInspect" />
      <el-table-column label="负责人" align="center" prop="fResponsiblePerson" />
      <el-table-column label="负责人电话" align="center" prop="fResponsiblePersonMobile" />
      <el-table-column label="创建时间" align="center" prop="fCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="fUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态：0在线，1离线，2故障" align="center" prop="fStatus" />
      <el-table-column label="报警时间" align="center" prop="fAlarmTime" />
      <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)"
            v-hasPermi="['equipment:equipment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:equipment: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="fEquipmentCode">
          <el-input v-model="form.fEquipmentCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="fEquipmentName">
          <el-input v-model="form.fEquipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型：1.老化柜、2.标定车、3.机械臂、4.镭雕机、5.AOI检测设备" prop="fEquipmentType">
          <el-select v-model="form.fEquipmentType" placeholder="请选择设备类型：1.老化柜、2.标定车、3.机械臂、4.镭雕机、5.AOI检测设备">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="厂家" prop="fEquipmentFactory">
          <el-input v-model="form.fEquipmentFactory" placeholder="请输入厂家" />
        </el-form-item>
        <el-form-item label="所属部门" prop="fBeyongDepartment">
          <el-input v-model="form.fBeyongDepartment" placeholder="请输入所属部门" />
        </el-form-item>
        <el-form-item label="IP地址" prop="fIp">
          <el-input v-model="form.fIp" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="端口号" prop="fPort">
          <el-input v-model="form.fPort" placeholder="请输入端口号" />
        </el-form-item>
        <el-form-item label="安装位置" prop="fInstallLocation">
          <el-input v-model="form.fInstallLocation" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="是否巡查点：1.是，2.否" prop="fIsInspect">
          <el-input v-model="form.fIsInspect" placeholder="请输入是否巡查点：1.是，2.否" />
        </el-form-item>
        <el-form-item label="负责人" prop="fResponsiblePerson">
          <el-input v-model="form.fResponsiblePerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="fResponsiblePersonMobile">
          <el-input v-model="form.fResponsiblePersonMobile" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="创建时间" prop="fCreateTime">
          <el-date-picker clearable size="small"
            v-model="form.fCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="fUpdateTime">
          <el-date-picker clearable size="small"
            v-model="form.fUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态：0在线，1离线，2故障">
          <el-radio-group v-model="form.fStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警时间" prop="fAlarmTime">
          <el-input v-model="form.fAlarmTime" placeholder="请输入报警时间" />
        </el-form-item>
      </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 { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment, exportEquipment } from "@/api/equipment/equipment";

export default {
  name: "Equipment",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产设备信息表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fEquipmentCode: null,
        fEquipmentName: null,
        fEquipmentType: null,
        fEquipmentFactory: null,
        fBeyongDepartment: null,
        fIp: null,
        fPort: null,
        fInstallLocation: null,
        fIsInspect: null,
        fResponsiblePerson: null,
        fResponsiblePersonMobile: null,
        fCreateTime: null,
        fUpdateTime: null,
        fStatus: null,
        fAlarmTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产设备信息列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fEquipmentId: null,
        fEquipmentCode: null,
        fEquipmentName: null,
        fEquipmentType: null,
        fEquipmentFactory: null,
        fBeyongDepartment: null,
        fIp: null,
        fPort: null,
        fInstallLocation: null,
        fIsInspect: null,
        fResponsiblePerson: null,
        fResponsiblePersonMobile: null,
        fCreateTime: null,
        fUpdateTime: null,
        fStatus: "0",
        fAlarmTime: 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.fEquipmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fEquipmentId = row.fEquipmentId || this.ids
      getEquipment(fEquipmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fEquipmentId != null) {
            updateEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fEquipmentIds = row.fEquipmentId || this.ids;
      this.$confirm('是否确认删除生产设备信息编号为"' + fEquipmentIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEquipment(fEquipmentIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产设备信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportEquipment(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
