<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.name"
          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.code"
          clearable
          size="small"
          placeholder="输入设备编码"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <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="name">
          <el-select
            v-model="form.name"
            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="code">
          <el-input v-model="form.code" style="width: 370px" />
        </el-form-item>
        <el-form-item label="设备位置" prop="location">
          <el-input v-model="form.location" style="width: 370px" />
        </el-form-item>
        <el-form-item label="识别类型" prop="types">
          <el-checkbox-group v-model="form.types" style="width: 340px">
            <el-checkbox label="1">安全帽监测</el-checkbox>
            <el-checkbox label="2">工装监测</el-checkbox>
            <el-checkbox label="3">接打手机</el-checkbox>
            <el-checkbox label="4">吸烟监测</el-checkbox>
            <el-checkbox label="5">越线监测</el-checkbox>
            <el-checkbox label="6">跌倒监测</el-checkbox>
            <el-checkbox label="7">人员异物入侵</el-checkbox>
            <el-checkbox label="8">明烟明火</el-checkbox>
          </el-checkbox-group>
        </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 variablesTableOptions"
        :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', 'variables:edit', 'variables: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 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 { variablesTableOptions } from '../tableConfig'
const defaultForm = {
  id: null,
  name: '',
  code: '',
  location: '',
  types: [],
  type1: '',
  type2: '',
  type3: '',
  type4: '',
  type5: '',
  type6: '',
  type7: '',
  type8: '',
}

export default {
  name: 'variables',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: 'AI设备',
      url: 'positioning/label/getList',
      listOrder: [],
      crudMethod: { ...crudLabel },
    })
  },
  computed: { 
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getOptions()
  },
  data() {
    return {
      variablesTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'variables:add'],
        edit: ['admin', 'variables:edit'],
        del: ['admin', 'variables:del'],
      },
      rules: {
        name: [{ required: true, message: '请选择设备名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
      },
      optionsObj: {
        Build: [],
      },
    }
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {},
    [CRUD.HOOK.beforeToEdit]() {
      if (!this.form.types?.length) {
        this.form.types = []
      }
    },
    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>
