<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-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.LaserPtzType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.buildId"
          filterable
          clearable
          size="small"
          placeholder="请选择建筑名称"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </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" style="width: 280px" />
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" size="small" style="width: 280px" />
            </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="form.type"
                size="small"
                style="width: 280px"
              >
                <el-option
                  v-for="item in optionsObj.LaserPtzType"
                  :key="item.value"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="建筑名称" prop="buildId">
              <el-select
                v-model="form.buildId"
                size="small"
                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="cameraId">
              <el-select
                v-model="form.cameraId"
                size="small"
                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="高度" prop="height">
              <el-input-number
                v-model="form.height"
                size="small"
                :min="0"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="起始角度" prop="startAngle">
              <el-input-number
                v-model="form.startAngle"
                size="small"
                :min="0" :max="360"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="结束角度" prop="endAngle">
              <el-input-number
                v-model="form.endAngle"
                size="small"
                :min="0" :max="360"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="半径" prop="radius">
              <el-input-number
                v-model="form.radius"
                size="small"
                :min="0"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="浓度标签" prop="concentrationTag">
              <tag-select-simple
                v-model="form.concentrationTag"
                size="small"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="光强标签" prop="lightIntensityTag">
              <tag-select-simple
                v-model="form.lightIntensityTag"
                size="small"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="水平位置标签" prop="panTag">
              <tag-select-simple
                v-model="form.panTag"
                size="small"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="垂直位置标签" prop="tileTag">
              <tag-select-simple
                v-model="form.tileTag"
                size="small"
                style="width: 280px"
              />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                autosize
                clearable
                size="small"
                style="width: 280px"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16">
          <relative-map
            v-if="crudCU"
            ref="RelativeMap"
            :buildFloor="buildFloor"
            v-model="mapData"
            drawShow
            drawMode="Point"
            :lineLengthShow="true"
          />
        </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 laserPtzConfigConfigTableOptions"
        :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', 'leak:laserptz:config:edit', 'leak:laserptz: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 />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudMethod from '@/api/leak/laserPtz/config'
import { getOptions as getOptions2 } from '@/api/options'
import { getOptions } from '@/api/leak/laserPtz/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 TagSelectSimple from '@/components/TagSelectSimple'
import { laserPtzConfigConfigTableOptions } from '../../tableConfig'

import RelativeMap from '@/components/RelativeMap'

const defaultForm = {
  id: '0',
  code: '', //编号
  name: '', //名称
  type:'',//类型
  buildId: '', //建筑ID
  floorNo: 0, //楼层号
  location: {}, //坐标
  height: 0,//高度
  startAngle: 0,//起始角度
  endAngle: 0,//结束角度
  radius:0,//半径
  cameraId: null,//摄像头ID
  concentrationTag: null,//浓度标签
  lightIntensityTag: null,//光强标签
  panTag: null,//水平位置标签
  tileTag: null,//垂直位置标签
  remark: '', //备注
}

export default {
  name: 'LeakLaserPtzConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, RelativeMap, TagSelectSimple },
  cruds() {
    return CRUD({
      title: '泄露检测激光云台',
      url: 'leak/laserPtz/config/getList',
      listOrder: [],
      crudMethod: { ...crudMethod },
    })
  },
  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
    },
  },
  data() {
    return {
      laserPtzConfigConfigTableOptions,
      permission: {
        add: ['admin', 'leak:laserptz:config:add'],
        edit: ['admin', 'leak:laserptz:config:edit'],
        del: ['admin', 'leak:laserptz:config:del'],
      },
      rules: {
        code: [{ required: true, message: '请输入编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        buildId: [{ required: true, message: '请在地图上画点', trigger: 'blur' }],
        cameraId: [{ required: true, message: '请选择摄像头', trigger: 'blur' }],
      },
      optionsObj: {
        Build: [],
        Camera: [],
        LaserPtzType: [],
      },
      iconOptions: [],
      mapData: [],
      buildFloor: {},
    }
  },
  created() {
    this.getOptions()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.mapData = []
      this.buildFloor = {}
    },
    // 打开
    [CRUD.HOOK.beforeToEdit]() {
      this.mapData = [
        {
          buildId: this.form.buildId,
          floorNo: 0,
          geometries: [
            {
              type: 'Point',
              points: [{ x: this.form.location.x, y: this.form.location.y }],
            },
          ],
        },
      ]
      this.buildFloor = {
        buildId: this.form.buildId,
        floorNo: 0,
      }
    },
    //验证前处理
    [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 = 0
        this.form.location = {
          buildId: this.mapData[0].buildId,
          floorNo: 0,
          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]() {},
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        if (key == 'Build') {
          try {
            let res = await getOptions2(key)
            if (res.code === 20000) this.optionsObj[key] = res.data.content
          } catch (error) {
            console.log(error)
          }
        } else {
          try {
            let res = await getOptions(key)
            if (res.code === 20000) this.optionsObj[key] = res.data.content
          } catch (error) {
            console.log(error)
          }
        }
      })
    },
  },
}
</script>

<style></style>
