<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model.trim="query.blurry" clearable size="small" placeholder="输入楼层名称/负责人/联系方式进行搜索" style="width: 350px;" class="filter-item"  @keyup.enter.native="crud.toQuery" />
        <!-- <label class="el-form-item-label">楼层名称</label>
        <el-input v-model="query.floorName" clearable placeholder="楼层名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">楼层负责人(person in charge)</label>
        <el-input v-model="query.floorPic" clearable placeholder="楼层负责人(person in charge)" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">联系方式</label>
        <el-input v-model="query.contacts" clearable placeholder="联系方式" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="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="floorName">
            <el-input v-model.trim="form.floorName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="负责人" prop="floorPic">
            <el-input v-model.trim="form.floorPic" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系方式" prop="contacts">
            <el-input v-model.trim="form.contacts" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="排序" prop="sortIndex">
            <!-- <el-input v-model.trim="form.sortIndex" style="width: 370px;" type="number" :min="0" :max="999" /> -->
            <el-input-number v-model.number="form.sortIndex" :min="0" :max="999" controls-position="right" 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" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="floorName" label="楼层名称" />
        <el-table-column prop="floorPic" label="楼层负责人" />
        <el-table-column prop="contacts" label="联系方式" />
        <el-table-column prop="sortIndex" label="排序" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column v-if="checkPer(['admin','dmFloor:edit','dmFloor:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmFloor from '@/api/dm/floor/dmFloor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { isvalidPhone } from '@/utils/validate'

const defaultForm = { floorId: null, floorName: null, floorPic: null, contacts: null, sortIndex: null, createBy: null, updateBy: null, createTime: null, updateTime: null }

// 自定义验证
const validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入联系方式'))
  } else if (!isvalidPhone(value)) {
    callback(new Error('请输入正确的11位手机号码'))
  } else {
    callback()
  }
}

const validSortIndex = (rule, value, callback) => {
  // 有值的话,进行校验
  if (value) {
    if (!Number.isInteger(Number(value))) {
      callback(new Error('请输入数值'))
    }
  }
  callback()
}
export default {
  name: 'DmFloor',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '楼层', url: 'api/dmFloor', idField: 'floorId', sort: 'sortIndex,asc', crudMethod: { ...crudDmFloor }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmFloor:add'],
        edit: ['admin', 'dmFloor:edit'],
        del: ['admin', 'dmFloor:del']
      },

      rules: {
        floorId: [
          { required: true, message: '主键不能为空', trigger: 'blur' }
        ],
        floorName: [
          { required: true, message: '楼层名称不能为空', trigger: 'blur' }
        ],
        floorPic: [
          { required: true, message: '楼层负责人不能为空', trigger: 'blur' }
        ],
        contacts: [
          { required: true, trigger: 'blur', validator: validPhone }
        ],
        sortIndex: [
          { required: false, trigger: 'blur', validator: validSortIndex }
        ]
      },
      queryTypeOptions: [
        { key: 'floorName', display_name: '楼层名称' },
        { key: 'floorPic', display_name: '楼层负责人(person in charge)' },
        { key: 'contacts', display_name: '联系方式' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }

  }
}
</script>

<style scoped>
::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
