<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.barCode" clearable placeholder="key" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.itemCode" clearable placeholder="品番" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.code" clearable placeholder="纳入场所" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.door" clearable placeholder="门" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.type" clearable placeholder="箱种" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />

        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU"  :height="crud.tableHeight" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="100px">
          <el-form-item label="部品品番" prop="item" >
            <el-select v-model="form.item"  value-key="id" filterable placeholder="请选择部品品番" style="width: 180px;">
              <el-option
                  v-for="item in itemList"
                  :key="item.id"
                  :label="item.name+item.code"
                  :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="纳入场所">
            <el-input v-model="form.code" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="key">
            <el-input v-model="form.barCode" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="门">
            <el-input v-model="form.door" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="箱种">
            <el-input v-model="form.type" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="收容数">
            <el-input type="number" v-model="form.quantity" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 380px;" />
          </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%;"   :height="crud.tableHeight" @selection-change="crud.selectionChangeHandler" border>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="beSupplier" label="供应商" />
        <el-table-column prop="barCode" label="key" width="200px" align="center"/>
        <el-table-column prop="item.code" label="部品品番" width="120px"/>
        <el-table-column prop="item.name" label="部品品名" />
        <el-table-column prop="code" label="纳入场所" />
        <el-table-column prop="door" label="门" />
        <el-table-column prop="type" label="箱种" />
        <el-table-column prop="quantity" label="收容数" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" width="200px" align="center">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.createTime)}}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','itemRelation:edit','itemRelation:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudItemRelation from '@/api/itemRelation'
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 { getItemsList } from '@/api/item'
import { formatDate } from '@/utils/commonUtils'

const defaultForm = { id: null, beSupplier: null, barCode: null, item: null, code: null, door: null, type: null, quantity: null, deptId: null, description: null, enabled: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'ItemRelation',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '纳所维护', url: 'api/itemRelation', idField: 'id', sort: 'id,desc', crudMethod: { ...crudItemRelation }})
  },
  data() {
    return {
      itemList: [],
      permission: {
        add: ['admin', 'itemRelation:add'],
        edit: ['admin', 'itemRelation:edit'],
        del: ['admin', 'itemRelation:del']
      },
      rules: {
        item: [
          { required: true, message: '品番不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'beSupplier', display_name: '供应商' },
        { key: 'barCode', display_name: 'key' },
        { key: 'itemId', display_name: '品番' },
        { key: 'code', display_name: '纳入场所' },
        { key: 'door', display_name: '门' },
        { key: 'type', display_name: '箱种' }
      ]
    }
  },
  mounted() {
    this.getItemList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItemList() {
      getItemsList().then(res => {
        this.itemList = res.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
  }
}
</script>

<style scoped>

</style>
