<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="输入建筑Id"
          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.positioningBuildId"
          filterable
          clearable
          size="small"
          placeholder="请选择定位建筑Id"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in positioningBuildList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="1000px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="220px">
        <!-- 基础信息 -->
        <div class="info-title">
          <div></div>
          <span>基础信息</span> 
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="建筑ID" prop="code" label-width="110px">
              <el-input v-model="form.code" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="定位系统建筑" prop="positioningBuildId" label-width="110px">
              <el-select
                v-model="form.positioningBuildId"
                filterable
                size="small"
                placeholder="请选择定位系统建筑"
                class="filter-item"
                style="width: 200px"
                @change="crud.toQuery"
              >
                <el-option
                  v-for="item in positioningBuildList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="建筑名称" prop="name" label-width="110px">
              <el-input v-model="form.name" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="排序" prop="sort" label-width="110px">
              <el-input v-model="form.sort" style="width: 200px;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="名称缩写" prop="abbName" label-width="110px">
              <el-input v-model="form.abbName" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="默认楼层" prop="defaultFloor"  label-width="110px">
              <el-select
                v-model="form.defaultFloor"
                filterable
                size="small"
                placeholder="请选择建筑"
                class="filter-item"
                style="width: 200px"
                @change="crud.toQuery"
              >
                <el-option
                  v-for="item in form.floorList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.floorList && form.floorList.length > 0">
          <div class="info-title">
            <div></div>
            <span>楼层信息</span> 
          </div>
          <el-table
            :data="form.floorList"
            border
            style="width: 96%; margin: 20px">
            <el-table-column
              prop="sort"
              label="序号"
              width="120"
              align="center">
            </el-table-column>
            <el-table-column
              prop="name"
              label="楼层名称"
              align="center">
            </el-table-column>
          </el-table>
        </el-row>
        <!-- 地图配置 -->
        <div class="info-title">
          <div></div>
          <span>地图与面板配置</span> 
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="地图投影" prop="projection" label-width="110px">
              <el-input v-model="form.projection" style="width: 200px;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="地图旋转角度" prop="mapBearing" label-width="110px">
              <el-input v-model="form.mapBearing" style="width: 200px;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="地图倾斜角度" prop="mapPitch" label-width="110px">
              <el-input v-model="form.mapPitch" style="width: 200px;" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 面板配置 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="人员统计面板显示" prop="countareaPanelDisplay" label-width="140px">
              <el-switch
                v-model="form.countareaPanelDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="是"
                inactive-text="否"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">       
            <el-form-item label="人员统计面板状态" prop="countareaPanelOpen" label-width="140px">
              <el-switch
                v-model="form.countareaPanelOpen"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="开"
                inactive-text="关"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="调试面板显示" prop="debugPanelDisplay" label-width="140px">
              <el-switch
                v-model="form.debugPanelDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="是"
                inactive-text="否"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>   
          </el-col>
          <el-col :span="6">
            <el-form-item label="楼层选择面板显示" prop="floorPanelDisplay" label-width="140px">
              <el-switch
                v-model="form.floorPanelDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="是"
                inactive-text="否"
                :active-value="true"
                :inactive-value="false"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row> 
        <el-row>
          <el-col :span="12">
            <el-form-item label="GPS地图默认缩放" prop="gpsMapZoom">
              <el-input v-model="form.gpsMapZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="GPS地图最大缩放" prop="gpsMapMaxZoom">
              <el-input v-model="form.gpsMapMaxZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="GPS地图最小缩放" prop="gpsMapMinZoom">
              <el-input v-model="form.gpsMapMinZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="GPS地图最大分辨率" prop="gpsResolution0">
              <el-input v-model="form.gpsResolution0" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="GPS地图最大分辨率倍数" prop="gpsResolutionMax">
              <el-input v-model="form.gpsResolutionMax" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="左上角GPS坐标（WGS84）经度" prop="gpsCoordinate0_Longitude">
              <el-input v-model="form.gpsCoordinate0_Longitude" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="左上角GPS坐标（WGS84）纬度" prop="gpsCoordinate0_Latitude">
              <el-input v-model="form.gpsCoordinate0_Latitude" style="width: 200px;" />
            </el-form-item>     
             <el-form-item label="右下角GPS坐标（WGS84）经度" prop="gpsCoordinate1_Longitude">
              <el-input v-model="form.gpsCoordinate1_Longitude" style="width: 200px;" />
            </el-form-item>    
            <el-form-item label="右下角GPS坐标（WGS84）纬度" prop="gpsCoordinate1_Latitude">
              <el-input v-model="form.gpsCoordinate1_Latitude" style="width: 200px;" />
            </el-form-item>       
          </el-col>
          <el-col :span="12">
            <el-form-item label="相对坐标地图默认缩放" prop="relativeMapZoom">
              <el-input v-model="form.relativeMapZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="相对坐标地图最大缩放" prop="relativeMapMaxZoom">
              <el-input v-model="form.relativeMapMaxZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="相对坐标地图最小缩放" prop="relativeMapMinZoom">
              <el-input v-model="form.relativeMapMinZoom" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="相对坐标地图最大分辨率" prop="relativeResolution0">
              <el-input v-model="form.relativeResolution0" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="相对坐标地图最大分辨率倍数" prop="relativeResolutionMax">
              <el-input v-model="form.relativeResolutionMax" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="左上角相对坐标X值，单位：毫米" prop="relativeCoordinate0_X">
              <el-input v-model="form.relativeCoordinate0_X" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="左上角相对坐标Y值，单位：毫米" prop="relativeCoordinate0_Y">
              <el-input v-model="form.relativeCoordinate0_Y" style="width: 200px;" />
            </el-form-item>
            <el-form-item label="右下角相对坐标X值，单位：毫米" prop="relativeCoordinate1_X">
              <el-input v-model="form.relativeCoordinate1_X" style="width: 200px;" />
            </el-form-item>          
            <el-form-item label="右下角相对坐标Y值，单位：毫米" prop="relativeCoordinate1_Y">
              <el-input v-model="form.relativeCoordinate1_Y" style="width: 200px;" />
            </el-form-item>       
          </el-col>
        </el-row>

      </el-form>
      <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 buildTableOptions"
        :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', 'build:edit', 'build: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 crudBuild from '@/api/system/build'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import { getOptions } from '@/api/system/public'

// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { buildTableOptions } from '../tableConfig'
import { getToken } from '@/utils/auth'
import getters from '@/store/getters'
import { initData } from '@/api/data'

const defaultForm = {
  id: '',
  code: '', //建筑ID
  name:"",//建筑名称
  abbName:"",//建筑缩写名称
  positioningBuildId: '', //定位系统建筑Id
  sort:0,//排序
  defaultFloor:"",//默认楼层
  floorList:[],//楼层列表
  countareaPanelDisplay: false,//是否显示人员统计面板
  countareaPanelOpen:false,//人员统计面板是否默认打开
  debugPanelDisplay:false,//是否显示调试面板
  floorPanelDisplay:false,//是否显示楼层选择面板

  mapBearin: 0,//地图旋转角度
  mapPitch:0,//地图倾斜角度

  //投影地图参数
  projection:"EPSG:3857",//地图投影
  gpsMapMaxZoom:24,//GPS地图最大缩放
  gpsMapMinZoom:5,//GPS地图最小缩放
  gpsMapZoom:19,//GPS地图默认缩放
  gpsResolution0:156543,//GPS地图最大分辨率
  gpsResolutionMax:30,//GPS地图最大分辨率倍数

  gpsCoordinate0:{longitude: 116.522816340649, latitude: 40.23528138525907},
  gpsCoordinate0_Latitude:40.23528138525907,//左上角GPS坐标（WGS84）纬度
  gpsCoordinate0_Longitude:116.522816340649,//左上角GPS坐标（WGS84）经度
  gpsCoordinate1:{longitude: 116.52493439907232, latitude: 40.23339492316251},
  gpsCoordinate1_Latitude:40.23339492316251,//右下角GPS坐标（WGS84）纬度
  gpsCoordinate1_Longitude:116.52493439907232,//右下角GPS坐标（WGS84）经度

  //相对坐标地图参数
  relativeMapMaxZoom:10,//相对地图最大缩放
  relativeMapMinZoom:1,//相对地图最小缩放
  relativeMapZoom:6.5,//相对地图默认缩放
  relativeResolution0:10240,//相对地图最大分辨率
  relativeResolutionMax:10,//地图最大分辨率倍数

  relativeCoordinate0:{x: 0, y: 0},
  relativeCoordinate0_X:0,//左上角相对坐标X值，单位：毫米
  relativeCoordinate0_Y:0,//左上角相对坐标y值，单位：毫米y
  relativeCoordinate1:{x: 180000, y: 210000},
  relativeCoordinate1_X:180000,//右下角相对坐标X值，单位：毫米
  relativeCoordinate1_Y:210000,//右下角相对坐标y值，单位：毫米

}
export default {
  name: 'Build',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '建筑',
      url: 'system/build/getList',
      listOrder: [],
      crudMethod: { ...crudBuild }
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    }
  },
  data() {
    return {
      buildTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'build:add'],
        edit: ['admin', 'build:edit'],
        del: ['admin', 'build:del']
      },
      rules: {
        code: [{ required: true, message: '请输入建筑Id', trigger: 'blur' }],
        name: [{ required: true, message: '请输入建筑名称', trigger: 'blur' }],
        positioningBuildId: [{ required: true, message: '请选择定位系统Id', trigger: 'blur' }],
      },
      optionsObj: {
        IocnType: []
      },
      token: getToken() ?? '',
      fileList: [],
      positioningBuildList:[]
    }
  },
  created() {
    this.getOptions()
    this.getPositioningBuildData()
  },
  methods: {
    getOptions() {
      Object.keys(this.optionsObj).forEach(async key => {
        try {
          let res
          if (key === 'Label') {
            res = await getOptions(key, { id: null })
          } else {
            res = await getOptions(key)
          }
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
    //获取弹窗内的建筑信息
    async getPositioningBuildData() {
      try {
        const res = await initData('positioning/house/getListJson', {
          PageIndex: 0,
          PageSize: 0,
        })
        const data = res.data.content
        // console.log(data);
        this.positioningBuildList = data.map((item) => {
          return {
            id: item.buildId,
            name: item.buildName,
          }
        })
      } catch (error) {
        console.log(error)
      }
    },

    beforeUpload(file) {
      if (file.size > 1024 * 1024 * 5) {
        this.$message.warning('文件最大限制为 5MB')
        return false
      }
      let fileName = file.name
      let fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase()
      const whiteList = ['jpg', 'jpeg', 'gif', 'png']
      if (!whiteList.includes(fileExtension)) {
        this.$message.warning(`只能上传扩展名为 ${whiteList.join(' / ')} 的文件`)
        return false
      }
    },
    handleSuccess(res, file, fileList) {
      if (res.code === 20000) {
        this.form.path = res.data
      } else {
        this.$message.error(res.msg)
      }
    },
    handleUploadError(err, file, fileList) {
      console.log(err)
      this.$message.error('上传失败')
    },
    handleUploadRemove(file, fileList) {
      if (fileList.length == 0) {
        this.form.Path = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .info-title {display: flex;margin-left: 6px;margin-bottom: 10px;}
::v-deep .info-title div{width: 4px; height: 15px;background: blue;margin-right: 8px;}
::v-deep .info-title span{font-size: 14px;font-weight: bold;}
</style>
