<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"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.mac"
          clearable
          size="small"
          placeholder="输入MAC"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.rfid"
          clearable
          size="small"
          placeholder="输入RFID"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.bindingPersonName"
          clearable
          size="small"
          placeholder="输入绑定人员姓名"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.workStatus"
          clearable
          size="small"
          placeholder="请选择工作状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == '1' ? '运动' : '休眠'"
            :value="item"
          />
        </el-select>
        <el-select
          v-model="query.lowBattery"
          clearable
          size="small"
          placeholder="电量低于"
          style="width: 160px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in electricOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.positioningOnline"
          clearable
          size="small"
          placeholder="请选择定位状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == '1' ? '在线' : '离线'"
            :value="item == '1' ? true : false"
          />
        </el-select>
        <el-select
          v-model="query.commOnline"
          clearable
          size="small"
          placeholder="请选择通讯状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in 2"
            :key="item"
            :label="item == '1' ? '在线' : '离线'"
            :value="item == '1' ? true : false"
          />
        </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="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="建筑名称" prop="positioningBuildId">
          <el-select
            v-model="form.positioningBuildId"
            :disabled="crud.status.edit > 0"
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <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="name">
          <el-input v-model="form.name" clearable style="width: 370px" />
        </el-form-item>
        <el-form-item label="MAC" prop="mac">
          <el-input v-model="form.mac" :disabled="crud.status.edit > 0" style="width: 370px" />
        </el-form-item>
        <el-form-item label="RFID" prop="rfid">
          <el-input v-model="form.rfid" clearable style="width: 370px" />
        </el-form-item>
      </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 labelTableOptions"
        :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', 'label:edit', 'label: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 />
    <scanner-or-reader :enable="true" @output="handleReaderOut" />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudLabel from '@/api/positioning/label'
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 { labelTableOptions } from '../tableConfig'
import ScannerOrReader from '@/components/ScannerOrReader'

const defaultForm = {
  id: null,
  positioningBuildId: '',
  name: '',
  mac: '',
  rfid: '',
}

export default {
  name: 'Label',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, ScannerOrReader },
  cruds() {
    return CRUD({
      title: '标签',
      url: 'positioning/label/getList',
      listOrder: [],
      crudMethod: { ...crudLabel },
      optShow: { add: true, edit: true, del: true, reset: true, import: true, export: true },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getOptions()
  },
  destroyed() {},
  data() {
    return {
      labelTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'label:add'],
        edit: ['admin', 'label:edit'],
        del: ['admin', 'label:del'],
      },
      rules: {
        positioningBuildId: [{ required: true, message: '请选择建筑名称', trigger: 'blur' }],
        mac: [
          { required: true, message: '请输入MAC', trigger: 'blur' },
          {
            pattern: /^[A-Fa-f\d]{12}$/,
            message: '请输入正确的MAC地址，不带符号，如：123ABC456DEF',
            trigger: 'blur',
          },
        ],
      },
      electricOptions: [
        { label: '低于25%', value: 0 },
        { label: '低于50%', value: 1 },
        { label: '低于75%', value: 2 },
        { label: '低于100%', value: 3 },
      ],
      optionsObj: {
        Build: [],
      },
    }
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.form.buildId = this.query.buildId
      if (this.form.id) {
        delete this.form.id
      }
    },
    handleReaderOut(code) {
      if (this.crudCU) this.form.rfid = code
      else {
        this.$set(this.query, 'rfid', code)
        // this.query.rfid = code
        this.crud.toQuery()
      }
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) {
            this.optionsObj[key] = res.data.content
            if (key == 'Build') {
              this.query.buildId = this.optionsObj.Build[0].value
              this.crud.toQuery()
            }
          }
        } catch (error) {
          console.log(error)
        }
      })
    },
  },
}
</script>

<style></style>
