<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <project-section-device-tree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <el-input
            v-model="listQuery.cameraNo"
            placeholder="摄像头序列号"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-input
            v-model="listQuery.cameraName"
            placeholder="摄像头名称"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleFilter"
          >查询</el-button>
          <el-button
            v-if="$requirePermission('BSET.DVMG.C')"
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleCreate"
          >新增</el-button>
        </div>
        <div class="content-container">
          <el-table
            v-loading="listLoading"
            row-key="id"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
            height="calc(100vh - 276px)"
          >
            <div slot="empty">
              <span v-if="listQuery.sectionId">没有数据</span>
              <span v-else>请选择标段</span>
            </div>
            <el-table-column label="序号" type="index" width="50" align="center" />
            <el-table-column label="摄像头编号" prop="cameraNo" align="center" />
            <el-table-column label="摄像头名称" prop="cameraName" align="center" />
            <el-table-column label="视频流" prop="videoStream" align="center" />
            <el-table-column label="视频类型" prop="cameraTypeName" align="center" show-overflow-tooltip />
            <el-table-column label="Sip编码" prop="sipCode" align="center" />
            <el-table-column label="通道Id" prop="channelId" align="center" />
            <el-table-column label="云台地址" prop="ptzIp" align="center" show-overflow-tooltip />
            <el-table-column label="RTMP流" prop="rtmpStream" align="center" show-overflow-tooltip />
            <el-table-column
              label="操作"
              align="center"
              width="120"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="{ row }">
                <el-button
                  v-if="$requirePermission('BSET.DVMG.U')"
                  type="primary"
                  size="mini"
                  @click="handleUpdate(row)"
                >修改</el-button>
                <el-button
                  v-if="$requirePermission('BSET.DVMG.D')"
                  size="mini"
                  type="danger"
                  @click="handleDelete(row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="total"
            :skip-count.sync="listQuery.skipCount"
            :max-result-count.sync="listQuery.maxResultCount"
            @pagination="getList"
          />
        </div>
        <el-dialog
          :title="textMap[dialogStatus]"
          :close-on-click-modal="false"
          :visible.sync="dialogFormVisible"
        >
          <el-form
            ref="dataForm"
            v-loading="formLoading"
            :rules="rules"
            :model="entity"
            label-position="right"
            label-width="120px"
          >
            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="摄像头编号" prop="cameraNo">
                  <el-input v-model="entity.cameraNo" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="摄像头名称" prop="cameraName">
                  <el-input v-model="entity.cameraName" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="摄像头类型" prop="cameraType">
                  <el-select v-model="entity.cameraType" class="w100p">
                    <el-option
                      v-for="item in cameraTypes"
                      :key="item.key"
                      :value="item.key"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="视频流">
                  <el-input v-model="entity.videoStream" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="entity.cameraType === 1" :gutter="16">
              <el-col :span="12">
                <el-form-item label="Sip编码" align="left">
                  <el-input v-model="entity.sipCode" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="通道Id" align="left">
                  <el-input v-model="entity.channelId" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="entity.cameraType === 1" :gutter="16">
              <el-col :span="12">
                <el-form-item label="云台地址" align="left">
                  <el-input v-model="entity.ptzIp" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="RTMP流" align="left">
                  <el-input v-model="entity.rtmpStream" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button
              :disabled="formLoading"
              @click="dialogFormVisible = false"
            >取消</el-button>
            <el-button
              :disabled="formLoading"
              type="primary"
              @click="dialogStatus === 'create' ? createData() : updateData()"
            >确认</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
  </el-container>
</template>

<script>
import * as interfaceApi from '@/api/camera-manage.js'
import ProjectSectionDeviceTree from '@/views/projects/components/ProjectSectionDeviceTree'

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import InnerSidebar from '@/components/InnerSidebar'

export default {
  name: 'CameraMng',
  directives: { waves },
  components: { Pagination, ProjectSectionDeviceTree, InnerSidebar },
  data() {
    return {
      list: [],
      cameraTypes: [
        { key: 1, name: 'GB28181' },
        { key: 2, name: '其他' }
      ],
      entity: {
        id: undefined,
        deviceManageId: undefined,
        cameraNo: '',
        cameraName: '',
        videoStream: '',
        sipCode: '',
        channelId: '',
        ptzIp: '',
        rtmpStream: '',
        cameraType: undefined
      },
      total: 0,
      listLoading: false,
      listQuery: {
        deviceManageId: undefined,
        cameraNo: '',
        cameraName: '',
        cameraType: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      formLoading: false,
      textMap: {
        update: '修改',
        create: '创建'
      },
      sections: [],
      rules: {
        cameraNo: [
          { required: true, message: '请输入摄像头编号', trigger: 'blur' },
          { max: 32, message: '长度必须少于32个字符', trigger: 'blur' }
        ],
        cameraName: [
          { required: true, message: '请输入摄像头名称', trigger: 'blur' },
          { max: 64, message: '长度必须少于64个字符', trigger: 'blur' }
        ],
        cameraType: [
          { required: true, message: '请选择摄像头类型', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 树形事件
    handleTreeClick(data, node) {
      if (data.isDevice) {
        this.entity.deviceManageId = data.id
        this.listQuery.deviceManageId = data.id
        this.getList()
      }
    },
    // 清空下拉
    delValue(parameter) {
      this.listQuery[parameter] = undefined
    },
    // 获取数据列表
    getList() {
      if (!this.listQuery.deviceManageId) {
        this.list = []
        return
      }
      this.listLoading = true
      interfaceApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 查询
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    // 创建清空
    resetStation() {
      this.entity = {
        id: undefined,
        deviceManageId: this.listQuery.deviceManageId,
        cameraNo: '',
        cameraName: '',
        videoStream: '',
        sipCode: '',
        channelId: '',
        pTZIp: '',
        rtmpStream: '',
        cameraType: undefined
      }
    },
    // 创建点击事件
    handleCreate() {
      if (!this.entity.deviceManageId) {
        this.verificationTips('请选择设备')
        return
      }
      this.resetStation()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 提示
    verificationTips(message) {
      this.$notify({
        title: '提示',
        message: message,
        type: 'error',
        duration: 2000
      })
    },
    // 创建
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.formLoading = true
          interfaceApi
            .create(this.entity)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    // 修改点击事件
    handleUpdate(row) {
      this.entity = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.formLoading = false
      // this.entity.site = this.entity.projectName + ' / ' + this.entity.sectionName + ' / ' + this.entity.stationName
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const submitData = Object.assign({}, this.entity)
          this.formLoading = true
          interfaceApi
            .update(submitData.id, submitData)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        interfaceApi
          .remove(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .finally(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>

<style  scoped>
.number >>> input{
  text-align: left !important;
}
.el-selects >>> .el-input--suffix{
  width: 150px !important;
}
</style>
