<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.code"
          clearable
          size="small"
          placeholder="输入摄像头编号"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入摄像头名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-cascader
          v-model="query.department"
          :options="optionsObj.Department"
          :props="{ checkStrictly: true, emitPath: false }"
          filterable
          clearable
          size="small"
          placeholder="请选择部门"
          style="width: 160px"
          class="filter-item"
          @change="crud.toQuery"
        />
        <el-select
          v-model="query.buildId"
          filterable
          clearable
          size="small"
          placeholder="请选择建筑名称"
          class="filter-item"
          style="width: 160px"
          @change="handleChangeBuildId"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.floorNo"
          filterable
          clearable
          size="small"
          placeholder="请选择楼层"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.FloorNo"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.type"
          filterable
          clearable
          size="small"
          placeholder="请选类型"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.CameraType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.streamProtocol"
          filterable
          clearable
          size="small"
          placeholder="请选择码流协议"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.StreamProtocol"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.enable"
          clearable
          size="small"
          placeholder="请选择是否启用"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == '1' ? '启用' : '禁用'"
            :value="item == '1' ? true : false"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!-- 表单组件 -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="1200px"
    >
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="编号" prop="code">
              <el-input v-model="form.code" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="部门" prop="department">
              <el-cascader
                v-model="form.department"
                :options="optionsObj.Department"
                :props="{ checkStrictly: true, emitPath: false }"
                filterable
                clearable
                size="small"
                style="width: 280px"
                class="filter-item"
              />
            </el-form-item>
            <el-form-item label="建筑名称" prop="buildId">
              <el-select
                v-model="form.buildId"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              >
                <el-option
                  v-for="item in optionsObj.Build"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="楼层" prop="floorNo">
              <el-input
                v-model="form.floorNo"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="高度" prop="height">
              <el-input
                v-model="form.height"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="form.type"
                filterable
                size="small"
                class="filter-item"
                style="width: 280px"
              >
                <el-option
                  v-for="item in optionsObj.CameraType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="图标" prop="icon">
              <el-select
                v-model="form.icon"
                filterable
                size="small"
                class="filter-item"
                style="width: 280px"
              >
                <el-option
                  v-for="item in iconOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <img
                    style="float: right; width: 34px; height: 34px"
                    :src="VUE_APP_BASE_API + item.path"
                  />
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="ip地址" prop="ip">
              <el-input v-model="form.ip" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="播放端口号" prop="playPort">
              <el-input
                v-model="form.playPort"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="控制端口号" prop="controlPort">
              <el-input
                v-model="form.controlPort"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="用户名" prop="userName">
              <el-input
                v-model="form.userName"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                show-password
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="码流协议" prop="streamProtocol">
              <el-select
                v-model="form.streamProtocol"
                filterable
                size="small"
                class="filter-item"
                style="width: 280px"
              >
                <el-option
                  v-for="item in optionsObj.StreamProtocol"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="播放通道号" prop="playChannel">
              <el-input
                v-model="form.playChannel"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="控制通道号" prop="controlChannel">
              <el-input
                v-model="form.controlChannel"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="码流类型" prop="streamIndex">
              <el-input
                v-model="form.streamIndex"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="协议参数" prop="protocolParam">
              <el-input
                v-model="form.protocolParam"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="云台功能" prop="ptzEnable">
              <el-switch
                v-model="form.ptzEnable"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="启用" prop="enable">
              <el-switch
                v-model="form.enable"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16">
          <relative-map
            v-if="crudCU"
            ref="RelativeMap"
            v-model="mapData"
            drawShow
            :buildFloor="buildFloor"
            drawMode="Point"
            :symbols="pointSymbols"
          />
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU"> 取消 </el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">
          确认
        </el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
      @row-dblclick="play"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="item in cameraConfigTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
        <template slot-scope="scope">
          <img
            style="width: 32px; height: 32px"
            v-if="item.prop === 'iconPath'"
            :src="VUE_APP_BASE_API + scope.row.iconPath"
            alt=""
          />
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'camera:config:edit', 'camera:config:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <image-video :cameraData.sync="cameraData" :visible.sync="cameraVisible"></image-video>
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudCamera from '@/api/camera/config'
import { getOptions } from '@/api/camera/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import DateRangePicker from '@/components/DateRangePicker'
import { cameraConfigTableOptions } from '../tableConfig'
import { initData } from '@/api/data'
//import CameraMap from './cameraMap'
import {
  isPosNumber,
  isNegNumber,
  isNumber,
  isPosInteger,
  isNegInteger,
  isInteger,
} from '@/utils/validate'
import RelativeMap from '@/components/RelativeMap'
import ImageVideo from '@/components/ImageVideo'


const defaultForm = {
  id: '0',
  code: '', //编号
  name: '', //名称
  department: null, //部门
  buildId: '', //建筑ID
  floorNo: null, //楼层号
  location: null, //坐标
  height: 0, //高度
  enable: true, //是否显示在GIS
  icon: '244450840335618105', //图标
  type: '', //类型
  ip: '', //ip地址
  playPort: '', //播放端口号
  controlPort: '', //控制端口号
  userName: '', //用户名
  password: '', // 密码
  streamProtocol: '', //码流协议
  playChannel: '', //播放通道号
  controlChannel: '', //控制通道号
  streamIndex: '', //码流类型索引
  protocolParam: '', //协议参数
  ptzEnable: false,
  remark: '', //备注
}

export default {
  name: 'CameraConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, RelativeMap, ImageVideo },
  cruds() {
    return CRUD({
      title: '摄像头',
      url: 'camera/config/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudCamera },
      optShow: {
        add: true,
        edit: true,
        del: true,
        reset: true,
        import: true,
        export: true,
      },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
    VUE_APP_BASE_WS() {
      return 'ws://' + window.location.host + process.env.VUE_APP_BASE_API
    },
    pointSymbols() {
      let p = null
      let icon = this.iconOptions.find((a) => a.id == this.form.icon)
      if (icon) {
        p = {
          markerFile: this.VUE_APP_BASE_API + icon.path,
          markerWidth: 40,
          markerHeight: 40,
          markerDx: 0,
          markerDy: 20,
        }
      }
      return {
        Point: p,
      }
    },
  },
  data() {
    var chackHeight = (rule, value, callback) => {
      if (!isPosNumber(value)) return callback(new Error('请输入正确的高度'))
      return callback()
    }
    var chackChannel = (rule, value, callback) => {
      if (!isPosInteger(value)) return callback(new Error('请输入正确的通道号'))
      return callback()
    }
    var chackStreamIndex = (rule, value, callback) => {
      if (!isPosInteger(value)) return callback(new Error('请输入正确的码流类型'))
      return callback()
    }
    return {
      cameraConfigTableOptions,
      permission: {
        add: ['admin', 'camera:config:add'],
        edit: ['admin', 'camera:config:edit'],
        del: ['admin', 'camera:config:del'],
      },
      rules: {
        code: [{ required: true, message: '请选输入编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        buildId: [{ required: true, message: '请在地图上画点', trigger: 'blur' }],
        floorNo: [{ required: true, message: '请在地图上画点', trigger: 'blur' }],
        height: [{ required: true, validator: chackHeight, trigger: 'blur' }],
        icon: [{ required: true, message: '请选择图标', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        ip: [{ required: true, message: '请输入IP地址', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        streamProtocol: [{ required: true, message: '请选择码流类型', trigger: 'blur' }],
        playChannel: [{ required: true, validator: chackChannel, trigger: 'blur' }],
        controlChannel: [{ required: true, validator: chackChannel, trigger: 'blur' }],
        streamIndex: [{ required: true, validator: chackStreamIndex, trigger: 'blur' }],
      },
      optionsObj: {
        CameraType: [],
        StreamProtocol: [],
        Department: [],
        Build: [],
        FloorNo: [],
      },
      iconOptions: [],
      mapData: [],
      buildFloor: {},
      cameraData: {},
      cameraVisible: false
    }
  },
  created() {
    this.getOptions()
    this.getIcons()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.mapData = []
      this.buildFloor = {}
    },
    // 打开
    [CRUD.HOOK.beforeToEdit]() {
      this.mapData = [
        {
          buildId: this.form.buildId,
          floorNo: this.form.floorNo,
          geometries: [
            {
              type: 'Point',
              points: [{ x: this.form.location.x, y: this.form.location.y }],
            },
          ],
        },
      ]
      this.buildFloor = {
        buildId: this.form.buildId,
        floorNo: this.form.floorNo,
      }
    },
    //验证前处理
    [CRUD.HOOK.beforeValidateCU]() {
      if (
        this.mapData &&
        this.mapData.length > 0 &&
        this.mapData[0].geometries &&
        this.mapData[0].geometries.length > 0 &&
        this.mapData[0].geometries[0].points &&
        this.mapData[0].geometries[0].points.length > 0
      ) {
        this.form.buildId = this.mapData[0].buildId
        this.form.floorNo = this.mapData[0].floorNo
        this.form.location = {
          buildId: this.mapData[0].buildId,
          floorNo: this.mapData[0].floorNo,
          x: this.mapData[0].geometries[0].points[0].x,
          y: this.mapData[0].geometries[0].points[0].y,
        }
      } else {
        this.$message.error('请绘制正确的点')
        return false
      }
    },
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() { },
    async handleChangeBuildId(val) {
      await this.getFloorNoOption(val)
      this.crud.toQuery()
    },
    getIcons() {
      initData('system/icon/getPageListJson', { type: '200003' }).then((res) => {
        this.iconOptions = res.data.content
      })
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        if (key == 'FloorNo') return
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
    async getFloorNoOption(buildId) {
      try {
        let res = await getOptions('FloorNo', { buildId: buildId })
        if (res.code === 20000) {
          this.optionsObj.FloorNo = res.data.content
          this.query.floorNo = null
        }
      } catch (error) { }
    },
    play(row, column, event) {
      console.log(row, column, event)
      this.cameraData = row;
      this.cameraVisible = true;
    }
  },
}
</script>

<style></style>
