<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label=" 名称" prop="name"><el-input v-model="queryParams.name" placeholder="请输入 名称" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item label="ip地址" prop="ipAddres"><el-input v-model="queryParams.ipAddres" placeholder="请输入ip地址" clearable @keyup.enter.native="handleQuery" /></el-form-item>
      <el-form-item label="在线状态" prop="onlineState">
        <el-select v-model="queryParams.onlineState" placeholder="请选择在线状态" clearable>
          <el-option v-for="dict in onlineStateOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

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

    <el-table v-loading="loading" border :data="cameraList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="摄像头id" align="center" prop="cameraId" />
      <el-table-column label=" 名称" align="center" prop="name" />
      <el-table-column label="ip地址" align="center" prop="ipAddres" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="调用url" align="center" prop="callUrl" />
      <el-table-column label="坐标lat" align="center" prop="lat" />
      <el-table-column label="坐标lng" align="center" prop="lng" />
      <el-table-column label="在线状态" align="center" prop="onlineState" :formatter="onlineStateFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleOnLine(scope.row)" v-hasPermi="['management:camera:edit']">在线检查</el-button>
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['management:camera:edit']">修改</el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['management:camera: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="name"><el-input v-model="form.name" placeholder="请输入名称" /></el-form-item>
        <el-form-item label="ip地址" prop="ipAddres"><el-input v-model="form.ipAddres" placeholder="请输入ip地址" /></el-form-item>
        <el-form-item label="端口" prop="port"><el-input v-model="form.port" placeholder="请输入端口" /></el-form-item>
        <el-form-item label="用户名" prop="userName"><el-input v-model="form.userName" placeholder="请输入用户名" /></el-form-item>
        <el-form-item label="密码" prop="password"><el-input v-model="form.password" placeholder="请输入密码" /></el-form-item>
        <el-form-item label="调用url" prop="callUrl"><el-input v-model="form.callUrl" type="textarea" placeholder="请输入内容" /></el-form-item>
        <el-form-item label="坐标lat" prop="lat"><el-input v-model="form.lat" placeholder="请输入坐标lat" /></el-form-item>
        <el-form-item label="坐标lng" prop="lng"><el-input v-model="form.lng" placeholder="请输入坐标lng" /></el-form-item>
        <el-form-item label="在线状态" prop="onlineState">
          <el-select v-model="form.onlineState" placeholder="请选择在线状态">
            <el-option v-for="dict in onlineStateOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </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 { listCamera, getCamera, delCamera, addCamera, updateCamera, exportCamera, checkOnline } from '@/api/management/camera';

export default {
  name: 'Camera',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像头管理表格数据
      cameraList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 在线状态字典
      onlineStateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        ipAddres: null,
        onlineState: null,
        orderByColumn: 'createTime', //排序字段
        isAsc: 'desc' //排序方式
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '请输入名称' }],
        callUrl: [{ required: true, message: '请输入调用url' }]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('online_state').then(response => {
      this.onlineStateOptions = response.data;
    });
  },
  methods: {
    /** 查询摄像头管理列表 */
    getList() {
      this.loading = true;
      listCamera(this.queryParams).then(response => {
        this.cameraList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 在线状态字典翻译
    onlineStateFormat(row, column) {
      return this.selectDictLabel(this.onlineStateOptions, row.onlineState);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cameraId: null,
        name: null,
        ipAddres: null,
        port: null,
        userName: null,
        password: null,
        callUrl: null,
        lat: null,
        lng: null,
        onlineState: null,
        createTime: null,
        updateTime: 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.cameraId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加摄像头管理';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cameraId = row.cameraId || this.ids;
      getCamera(cameraId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改摄像头管理';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.cameraId != null) {
            updateCamera(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addCamera(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cameraIds = row.cameraId || this.ids;
      this.$confirm('是否确认删除摄像头管理编号为"' + cameraIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delCamera(cameraIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有摄像头管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          return exportCamera(queryParams);
        })
        .then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handleOnLine(row) {
      const cameraId = row.cameraId || this.ids;
      checkOnline(cameraId).then(res => {
        if (res.code == 200) {
          if (res.msg == '设备在线') {
            this.msgSuccess('设备在线');
          } else {
            this.msgError('设备离线');
          }
        }
      });
    }
  }
};
</script>
