<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="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序列号" prop="devsn">
        <el-input
          v-model="queryParams.devsn"
          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="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:camera:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['web:camera:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="register"
          v-hasPermi="['web:camera:reboot']"
        >重启服务</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cameraList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column min-width="60" align="center" label="序号">
          <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
      </el-table-column>
      <el-table-column label="设备名" align="center" prop="name" />
      <el-table-column label="设备ip" align="center" prop="ip" />
      <el-table-column label="设备序列号" align="center" prop="devsn" />
      <el-table-column label="首次注册时间" align="center" prop="firstTime" />
      <el-table-column label="心跳时间" align="center" prop="updateTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
            <span v-if="scope.row.status == 1"><el-tag type="success">在线</el-tag></span>
            <span v-if="scope.row.status == 0"><el-tag type="info">离线</el-tag></span>
        </template>
      </el-table-column>
      <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-setting"
            @click="openFtpCfg(scope.row)"
            v-hasPermi="['web:camera:remove']"
          >FTP配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="showVideo(scope.row)"
            v-hasPermi="['web:camera:remove']"
          >查看视频</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:camera:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="reboot(scope.row)"
            v-hasPermi="['web:camera:remove']"
          >重启</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web: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="devsn">
          <el-input v-model="form.devsn" placeholder="" :disabled="true"/>
        </el-form-item>
        <el-form-item label="设备名" prop="name">
          <el-input v-model="form.name" 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>

    <!-- ftp配置对话框 -->
    <el-dialog title="FTP配置" :visible.sync="ftp.open">
      <div class="container">
        <!-- 表单 -->
        <div class="ftpform">
          <el-form :model="ftp.form" :label-width="ftp.width">
            <el-form-item label="启用">
              <el-switch v-model="ftp.run"></el-switch>
            </el-form-item>
            <el-form-item label="服务器地址">
              <el-input v-model="ftp.form.address"></el-input>
            </el-form-item>
            <el-form-item label="服务器端口">
              <el-input v-model="ftp.form.port"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="ftp.form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="ftp.form.password"></el-input>
            </el-form-item>
            <el-form-item label="FTP图片路径">
              <el-input v-model="ftp.form.pathRule"  type="textarea" :rows="3" ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <!-- 参考表格 -->
        <div class="table">
          <div class="head">路径配置参考</div>
          <div class="content">
            <div class="tips">
              <span>%Y: 年</span>
              <span>%M: 月</span>
              <span>%D: 日</span>
              <span>%h: 时</span>
              <span>%m: 分</span>
              <span>%s: 秒</span>
              <span>%u: 毫秒</span>
              <span>%c: 通道</span>
              <span>%n: 设备序列号</span>
              <span>%i: 设备ip</span>
              <span>%e: 事件名称</span>
              <span>%a: MAC地址</span>
            </div>
            <div class="notice">
              <div>注意: </div>
              <div>
                文件夹名以/隔开; 多个%%自动转为一个%;
                不可出现路径代码提示中没有的"%"+字符组合, 否则将保存失败!
              </div>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ftp.open = false">取 消</el-button>
        <el-button type="primary" @click="ftpConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Camera",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像机表格数据
      cameraList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        devsn: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // ftp配置
      ftp: {
        open: false,
        form: {
          address: '',
          port: '',
          username: '',
          password: '',
          pathRule: '',
          isEnable: '',
          devsn: ''
        },
        run: false,
        width: '100px'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询摄像机列表 */
    async getList() {
      this.loading = true;
      let res = await this.$axiosGet('/web/camera/list',this.queryParams);
      if(res.code === 0){
          this.cameraList = res.data;
          this.total = res.count;
      }
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        devsn: null,
        status: "0"
      };
      this.resetForm("form");
    },
    // 表单重置
    ftpCfgReset() {
      this.ftp.form = {
         address: '',
         port: '',
         username: '',
         password: '',
         pathRule: '',
         isEnable: '',
         devsn: ''
      };
      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 = "添加摄像机";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      let res = await this.$axiosGet('/web/camera/info/'+id);
      if(res.code === 0){
          this.form = res.data;
          this.open = true;
          this.title = "修改";
      }
    },
    async openFtpCfg(row){
        this.ftpCfgReset();
        const id = row.id;
        let res = await this.$axiosGet('/camera/ftp/getNetCfg',{'cameraId': id});
        if(res.code === 0){
            this.ftp.form = res.data;
            this.ftp.run = this.ftp.form.isEnable == 1;
            this.ftp.open = true;
        }
    },
    // 发送ftp配置表单
    async ftpConfirm() {
      this.ftp.form.isEnable = this.ftp.run ? 1 : 0;
      let res = await this.$axiosPost('/camera/ftp/editNetCfg',this.ftp.form);
      if(res.code === 0){
          this.$modal.msgSuccess("修改成功");
          this.ftp.open = false;
      }
    },
    async reboot(row) {
      const id = row.id;
      const status = row.status;
      if(status != 1){
        this.$modal.msg("设备未在线");
        return;
      }
      let res = await this.$axiosGet('/web/camera/reboot/'+id);
      if(res.code === 0){
          this.$modal.msgSuccess("执行成功");
          this.getList();
      }
    },
    async showVideo(row) {
      const id = row.id
      const status = row.status;
      if(status != 1){
        this.$modal.msg("设备未在线");
        return;
      }
      let res = await this.$axiosGet('/web/camera/showVideo/'+id);
      if(res.code === 500){
          this.$modal.msg(res.msg);
      }
    },
    async register() {
        let res = await this.$axiosGet('/web/camera/register');
        if(res.code === 0){
            this.$modal.msgSuccess("执行成功");
        }
    },
    async addMethod(){
        let res = await this.$axiosPost('/web/camera/add',this.form);
        if(res.code === 0){
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
        }
    },
    async editMethod(){
        let res = await this.$axiosPost('/web/camera/edit',this.form);
        if(res.code === 0){
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
        }
    },
    /** 提交按钮 */
    submitForm() {
        this.$refs["form"].validate(valid => {
            if (valid) {
                if (this.form.id != null) {
                    this.editMethod();
                } else {
                    this.addMethod();
                }
            }
        });
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const ids = row.id || this.ids;
      let res = await this.$axiosDelete('/web/camera/delete/'+ids);
      if(res.code === 0){
          this.$modal.msgSuccess("删除成功");
          this.getList();
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('web/camera/export', {
        ...this.queryParams
      }, `camera_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
  .container {
    display: flex;
  }
  .ftpform {
    width: 300px;
    margin-right: 50px;
  }
  .ftpform .el-form {
    width: 100%;
  }
  .table {
    flex: 1;
  }
  .head {
    font-size: 24px;
    line-height: 36px;
    text-align: center;
    height: 36px;
    margin: 0 auto;
    border-radius: 2px;
    box-shadow:
      1px 1px 2px hsla(200, 88%, 55%, .22),
      inset -2px -2px 2px hsla(200, 88%, 55%, .22),
      inset 0 0 1px hsla(200, 88%, 55%, .22);
  }
  .tips {
    margin: 10px 0;
  }
  .tips span {
    display: grid;
    grid: 1fr/1fr 10px;
  }
  .notice {
    color: #f33;
    padding-right: 20px;
    display: flex;
  }
  .notice div:first-child {
    width: 80px;
  }
</style>
