<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入摄像头名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <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="name">
              <el-input v-model="form.name" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="经度" prop="x">
              <el-input
                v-model="form.x"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="纬度" prop="y">
              <el-input
                v-model="form.y"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="高度" prop="z">
              <el-input v-model="form.z" size="small" class="filter-item" style="width: 280px" />
            </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="port">
              <el-input v-model="form.port" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="用户名" prop="user">
              <el-input v-model="form.user" size="small" class="filter-item" style="width: 280px" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="码流类型" prop="streamType">
              <el-input
                v-model="form.streamType"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="通道号" prop="channelID">
              <el-input
                v-model="form.channelID"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="零码流" prop="zeroChannel">
              <el-input
                v-model="form.zeroChannel"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16">
          <camera-map v-if="crudCU" ref="cameraMap" :form="form" />
        </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"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        v-for="item in cameraTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'camera:edit', 'camera:del'])"
        label="操作"
        width="160px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
          <!-- <div style="display: flex">
            <el-button
              size="mini"
              icon="el-icon-upload2"
              type="success"
              @click="toDetail(scope.row)"
            ></el-button>
          </div> -->
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudCamera from '@/api/video/camera'
import { getOptions } from '@/api/positioning/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 { cameraTableOptions } from '../tableConfig'
import CameraMap from './cameraMap'
import dayjs from 'dayjs'
const defaultForm = {
  id: '0',
  name: '', //名称
  x: '', // 经度
  y: '', //纬度
  z: '', //高度
  ip: '', //ip地址
  port: '', //端口号
  user: '', //用户名
  password: '', // 密码
  streamType: '', //码流类型
  channelID: '', //通道号
  zeroChannel: '', //零码流
}

export default {
  name: 'CameraConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, CameraMap },
  cruds() {
    return CRUD({
      title: '摄像头',
      url: 'video/camera/getList',
      listOrder: [],
      crudMethod: { ...crudCamera },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  data() {
    return {
      cameraTableOptions,
      permission: {
        add: ['admin', 'camera:add'],
        edit: ['admin', 'camera:edit'],
        del: ['admin', 'camera:del'],
      },
      rules: {
        buildId: [{ required: true, message: '请选择建筑名称', trigger: 'blur' }],
        floorNo: [{ required: true, message: '请输入楼层', trigger: 'blur' }],
        railName: [{ required: true, message: '请输入围栏名称', trigger: 'blur' }],
      },
      optionsObj: { Camera: [] },
    }
  },
  created() {
    // this.getOptions()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {},
    // 打开
    [CRUD.HOOK.beforeToEdit]() {},
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {},
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
    toDetail(row) {
      this.$router.push({ path: '/alarmDetail/' + row.id, query: { name: row.name } })
    },
  },
}
</script>

<style></style>
