<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.address"
          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="address">
              <el-input
                v-model="form.address"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="登录类型" prop="loginType">
              <el-input
                v-model="form.loginType"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceType">
              <el-input
                v-model="form.deviceType"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="功能" prop="function">
              <el-input
                v-model="form.function"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <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="location">
              <el-input
                v-model="form.location"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="数据标签" prop="dataTag">
              <el-input
                v-model="form.dataTag"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="经度" prop="positionX">
              <el-input
                v-model="form.positionX"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="纬度" prop="positionY">
              <el-input
                v-model="form.positionY"
                size="small"
                class="filter-item"
                style="width: 280px"
                disabled
              />
            </el-form-item>
            <el-form-item label="高度" prop="positionZ">
              <el-input
                v-model="form.positionZ"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="摄像头1" prop="cameraID">
              <el-select
                v-model="form.cameraID"
                clearable
                size="small"
                placeholder="请选择摄像头1"
                class="filter-item"
                style="width: 280px"
              >
                <el-option
                  v-for="item in optionsObj.Camera"
                  :key="item.value"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="摄像头2" prop="cameraID2">
              <el-select
                v-model="form.cameraID2"
                clearable
                size="small"
                placeholder="请选择摄像头1"
                class="filter-item"
                style="width: 280px"
              >
                <el-option
                  v-for="item in optionsObj.Camera"
                  :key="item.value"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16">
          <fire-map v-if="crudCU" ref="fireMap" :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 fireTableOptions"
        :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', 'rail:edit', 'rail: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 />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudFire from '@/api/alarmConfig/fire'
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 { fireTableOptions } from '../tableConfig'
import FireMap from './fireMap.vue'
import dayjs from 'dayjs'
const defaultForm = {
  // id: null,
  address: '', //设备地址
  loginType: '', //登录类型
  deviceType: '', //设备类型
  function: '', //功能
  code: '', //编号
  location: '', //安装位置
  dataTag: '', //数据标签
  positionX: '', // 经度
  positionY: '', //纬度
  positionZ: '', //高度
  cameraID: 0, //摄像头1
  cameraID2: 0, //摄像头2
  points: []
}

export default {
  name: 'FireConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, FireMap },
  cruds() {
    return CRUD({
      title: '围栏',
      url: 'fire/tag/getList',
      listOrder: [],
      crudMethod: { ...crudFire }
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    }
  },
  data() {
    return {
      fireTableOptions,
      permission: {
        add: ['admin', 'rail:add'],
        edit: ['admin', 'rail:edit'],
        del: ['admin', 'rail: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]() {
      const PointLayer = this.$refs.fireMap.PointLayer
      if (PointLayer) {
        const baseRelativeCoordinate0 = this.$refs.fireMap.baseRelativeCoordinate0
        // let points = []
        let Geometry = PointLayer.getFirstGeometry()

        if (Geometry) {
          console.log(Geometry)
          let Coordinates = Geometry.getCoordinates()
          this.form.positionX = Math.round(Coordinates.x) + baseRelativeCoordinate0.x
          this.form.positionY = -Math.round(Coordinates.y) - baseRelativeCoordinate0.y
          // if (points.length < 3) {
          //   this.$message.error('请绘制正确的围栏图形')
          //   return
          // }
        } else {
          this.$message.error('请绘制围栏图形')
          return
        }
        // this.form.points = points
      }
    },
    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)
        }
      })
    }
  }
}
</script>

<style></style>
