<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="数据所属部门(政或党的id)" prop="dataDeptId">
        <el-input
          v-model="queryParams.dataDeptId"
          placeholder="请输入数据所属部门(政或党的id)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头ID" prop="cameraId">
        <el-input
          v-model="queryParams.cameraId"
          placeholder="请输入摄像头ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道编号" prop="channelCode">
        <el-input
          v-model="queryParams.channelCode"
          placeholder="请输入通道编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          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"
          v-hasPermi="['camerachannel:camerachannel: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="['camerachannel:camerachannel: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="['camerachannel:camerachannel: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="['camerachannel:camerachannel:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getCameraChannelList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="camerachannelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="主键id" align="center" prop="id" />-->
      <!--<el-table-column label="数据状态(1-可用 2-不可用 )" align="center" prop="dataStatus" />-->
      <!--<el-table-column label="数据所属部门(政或党的id)" align="center" prop="dataDeptId" />-->
      <!--<el-table-column label="摄像头ID" align="center" prop="cameraId" />-->
      <el-table-column label="通道编号" align="center" prop="channelCode"/>
      <el-table-column label="通道名称" align="center" prop="channelName"/>
      <el-table-column label="通道类型" align="center" prop="channelType" :formatter="channelTypeFormat"/>
      <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-video-camera"-->
<!--            @click="preview(scope.row)"-->
<!--          >预览-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['camerachannel:camerachannel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['camerachannel:camerachannel: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="getCameraChannelList"
    />

    <!-- 添加或修改摄像头通道对话框 -->
    <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="数据所属部门(政或党的id)" prop="dataDeptId">
          <el-input v-model="form.dataDeptId" placeholder="请输入数据所属部门(政或党的id)" />
        </el-form-item>-->
        <!--<el-form-item label="摄像头ID" prop="cameraId">
          <el-input v-model="form.cameraId" placeholder="请输入摄像头ID" />
        </el-form-item>-->
        <el-form-item label="通道编号" prop="channelCode">
          <el-input v-model="form.channelCode" placeholder="请输入通道编号"/>
        </el-form-item>
        <el-form-item label="通道名称" prop="channelName">
          <el-input v-model="form.channelName" placeholder="请输入通道名称"/>
        </el-form-item>
        <div v-if="cameraUse == 6">
          <el-form-item label="设备编码" prop="deviceCode">
            <el-input v-model="form.deviceCode" placeholder="请输入设备编码"/>
          </el-form-item>
          <el-form-item label="唯一编码" prop="deviceSn">
            <el-input v-model="form.deviceSn" placeholder="请输入唯一编码"/>
          </el-form-item>
          <el-form-item label="接入协议" prop="deviceProtocolType">
            <el-input v-model="form.deviceProtocolType" placeholder="请输入接入协议"/>
          </el-form-item>
          <el-form-item label="登录类型" prop="deviceLoginType">
            <el-input v-model="form.deviceLoginType" placeholder="请输入登录类型"/>
          </el-form-item>
        </div>
        <el-form-item label="通道类型" prop="channelType">
          <el-select v-model="form.channelType" placeholder="请输入通道类型">
            <el-option
              v-for="item in channelTypeOptions"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </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 {
    listCamerachannel,
    getCamerachannel,
    delCamerachannel,
    addCamerachannel,
    updateCamerachannel
  } from "@/api/centerMonitor/camera/camerachannel";
  import deptselector from '@/views/components/deptselector'

  /** ----------------------------------摄像头预览开始------------------------------------- */
  import {getDahuaVideoServer} from "@/api/centerMonitor/dahua/dahua";
  import DHWs from '@/daHua/lib/DHWs'

  /** ----------------------------------摄像头预览结束------------------------------------- */
  export default {
    name: "Camerachannel",
    components: {
      deptselector
    },
    data() {
      return {
        /** ----------------------------------摄像头预览开始------------------------------------- */
        activePanel: 'key1',
        isLogin: false,
        cameraParams: [],
        /** ----------------------------------摄像头预览结束------------------------------------- */
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 摄像头通道表格数据
        camerachannelList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dataStatus: null,
          cameraId: null,
          channelCode: null,
          channelName: null,
          channelType: null,
          deviceCode: null,
          deviceSn: null,
          deviceProtocolType: null,
          deviceLoginType: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          channelCode: [
            {required: true, message: "通道编号不能为空", trigger: "blur"},
          ],
          channelName: [
            {required: true, message: "通道名称不能为空", trigger: "blur"},
          ],
          channelType: [
            {required: true, message: "通道类型不能为空", trigger: "blur"},
          ],
        },
        channelTypeOptions: [
          {value: "1", label: '红外'},
          {value: "2", label: '高清'}
        ],
      };
    },
    created() {
      this.getCameraChannelList()
    },
    props: {
      sonId: "",
      channelCameraIp: "",
      channelCameraPort: "",
      channelCameraAccount: "",
      channelCameraPasword: "",
      deviceCode:  "",
      deviceSn:  "",
      deviceProtocolType: "",
      deviceLoginType:  "",
      cameraUse:  "",
    },
    mounted() {
    },
    methods: {
      // 通道类型字典翻译
      channelTypeFormat(row, column) {
        var name = "";
        this.channelTypeOptions.forEach(function (item, index) {
          if (row.channelType == item.value) {
            name = item.label;
          }
        });
        return name;
      },
      /** 查询摄像头通道列表 */
      getCameraChannelList() {
        this.loading = true;
        this.queryParams.cameraId = this.sonId;
        listCamerachannel(this.queryParams).then(response => {
          this.camerachannelList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          dataStatus: "0",
          cameraId: null,
          channelCode: null,
          channelName: null,
          channelType: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.cameraId = this.sonId;
        this.getCameraChannelList();
      },
      /** 重置按钮操作 */
      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.form.cameraId = this.sonId;
        this.open = true;
        this.title = "添加摄像头通道";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form.cameraId = this.sonId;
        const id = row.id || this.ids;
        getCamerachannel(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改摄像头通道";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.cameraId = this.sonId;
            if (this.form.id != null) {
              updateCamerachannel(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getCameraChannelList();
              });
            } else {
              addCamerachannel(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getCameraChannelList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据项？').then(function () {
          return delCamerachannel(ids);
        }).then(() => {
          this.getCameraChannelList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('center-monitor/camerachannel/export', {
          ...this.queryParams
        }, `camerachannel_${new Date().getTime()}.xlsx`)
      },
      // beforeunloadHandler(e) {
      //   let thisNode = document.getElementById("iframeCamerachannel");
      //   thisNode.src = 'about:blank';
      //   thisNode.remove();
      // }
    },
    // beforeDestroy: function () {
    //   this.beforeunloadHandler();
    // },
  };
</script>
