<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.buildId"
          size="small"
          placeholder="请选择建筑名称"
          class="filter-item"
          style="width: 160px"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <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"
        />
        <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="buildId">
              <el-select
                v-model="form.buildId"
                clearable
                size="small"
                placeholder="请选择建筑名称"
                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="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="remark">
              <el-input
                v-model="form.remark"
                size="small"
                class="filter-item"
                style="width: 280px"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="16">
          <relative-map
            v-if="crudCU"
            ref="relativeMap"
            drawShow
            v-model="mapData"
            :buildFloor="buildFloor"
            drawMode="Polygon"
          />
        </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 houseTableOptions"
        :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', 'house:edit', 'house: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 crudHouse from '@/api/positioning/house'
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 { houseTableOptions } from '../tableConfig'
// import HouseMap from './houseMap.vue'
import RelativeMap from '@/components/RelativeMap'

const defaultForm = {
  // id: null,
  buildId: '',
  code: '',
  name: '',
  remark: '',
  points: [],
}

export default {
  name: 'House',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, RelativeMap },
  cruds() {
    return CRUD({
      title: '建筑物',
      url: 'positioning/house/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudHouse },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  data() {
    return {
      houseTableOptions,
      permission: {
        add: ['admin', 'house:add'],
        edit: ['admin', 'house:edit'],
        del: ['admin', 'house:del'],
      },
      rules: {
        buildId: [{ required: true, message: '请选择建筑名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入建筑物编号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入建筑物名称', trigger: 'blur' }],
      },
      optionsObj: { Build: [] },
      mapData: [],
      buildFloor: {},
    }
  },
  created() {
    this.getOptions()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      if (this.form.id) delete this.form.id
      this.mapData = []
      this.buildFloor = {}
      // console.log(this.form)
    },
    [CRUD.HOOK.beforeToEdit]() {
      this.mapData = [
        {
          buildId: this.form.buildId,
          floorNo: 0,
          geometries: [
            {
              type: 'Polygon',
              points: this.form.points,
            },
          ],
        },
      ]
      this.buildFloor = {
        buildId: this.form.buildId,
      }
    },
    //验证前处理
    [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 > 3
      ) {
        this.form.buildId = this.mapData[0].buildId
        this.mapData[0].geometries[0].points.pop()
        this.form.points = this.mapData[0].geometries[0].points
      } else {
        this.$message.error('请绘制正确的图形')
        return false
      }
    },
    async getOptions() {
      try {
        let res = await getOptions('Build')
        if (res.code === 20000) {
          this.optionsObj.Build = res.data.content
          this.query.buildId = this.optionsObj.Build[0].value
          this.crud.toQuery()
        }
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style></style>
