<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <label class="el-form-item-label" v-show="false">物料编码</label>
      <el-select v-show="false" v-model="query.itemCode" @focus="getItem" value-key="id" filterable multiple
                 placeholder="请选择物料" style="width: 150px;" class="filter-item" @keyup.enter.native="crud.toQuery">
        <el-option
          v-for="item in items"
          :key="item.id"
          :label="item.code"
          :value="item.code"
        />
      </el-select>
      <!--
            <el-input v-model="query.itemCode" clearable placeholder="请输入物料编码" style="width: 140px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
      -->
      <label class="el-form-item-label">物料编码</label>
      <el-input v-model="query.itemCodeOne" clearable placeholder="请输入物料编码" style="width: 150px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <label class="el-form-item-label">物料名称</label>
      <el-input v-model="query.itemName" clearable placeholder="请输入物料名称" style="width: 150px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>

      <label class="el-form-item-label">批次号</label>
      <el-input v-model="query.propC1" clearable placeholder="请输入批次号" style="width: 150px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"/>

      <label class="el-form-item-label">容器号</label>
      <el-input v-model="query.stockName" clearable placeholder="请输入容器号" style="width: 150px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"/>
      <rrOperation :crud="crud"/>

      <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery">重置
      </el-button>
      
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name"/>
      <!--表单组件-->
      <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="point">
            <el-select v-model="form.point" filterable placeholder="请选点位" value-key="id" style="width: 370px;">
              <el-option
                v-for="point in points"
                :key="point.id"
                :label="point.code"
                :value="point"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="容器" prop="stock">
            <el-select v-model="form.stock" value-key="id" filterable placeholder="请选择容器" style="width: 370px;">
              <el-option
                v-for="stock in stocks"
                :key="stock.id"
                :label="stock.code"
                :value="stock"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="form.quantity" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="分配数">
            <el-input v-model="form.queuedQty" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="重量">
            <el-input v-model="form.weight" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="体积">
            <el-input v-model="form.volume" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description" :rows="3" type="textarea" 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"
        show-summary
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
        height="70vh"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column :show-overflow-tooltip="true" prop="itemCode" label="物料编码">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.item.code }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="itemName" label="物料名称">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.item.name }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="areaName" label="区域">
          <template slot-scope="scope">
            <div>{{ scope.row.point.area.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="pointCode" label="点位号"/>
        <el-table-column prop="stockCode" label="容器号"/>
        <el-table-column prop="quantity" label="数量"/>
        <el-table-column prop="queuedQty" label="占用数"/>
        <el-table-column prop="propC1" label="批次号">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC1 }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="orderNumber" label="工单号">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.orderNumber }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column v-if="checkPer(['admin','fileManagement:edit','fileManagement:del'])" label="操作"
                         width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-popconfirm title="确定删除吗？"
                           @confirm="deleteDataM(scope.row.id)">
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudInventory from '@/api/inventory'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import {getItems} from "@/api/item"
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  billCode: null,
  itemKey: null,
  point: null,
  pointCode: null,
  stock: null,
  stockCode: null,
  bigStockId: null,
  quantity: null,
  queuedQty: null,
  weight: null,
  volume: null,
  description: null,
  beLock: null,
  beReject: null,
  status: null,
  storageDate: null,
  deptId: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  itemCodeOne: null
}
export default {
  name: 'Inventory',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      query: {
        quantity: 0
      },
      title: '库存信息',
      url: 'api/inventory',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudInventory},
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: false,
        download: true
      }
    })
  },
  data() {
    return {
      points: [],
      stocks: [],
      items: [],
      permission: {
        add: ['admin', 'inventory:add'],
        edit: ['admin', 'inventory:edit'],
        del: ['admin', 'inventory:del']
      },
      rules: {}
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItem() {
      getItems({}).then(res => {
        this.items = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    deleteDataM(id) {//删除数据的方法
      let ids = [];
      ids.push(id);
      crudInventory.del(ids).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS);
        this.crud.toQuery();
      }).catch(err => {
        this.crud.notify('删除失败！', CRUD.NOTIFICATION_TYPE.ERROR);
      })
      this.crud.toQuery();
    },
    resetQuery() {
      this.crud.resetQuery()
    }
  }
}
</script>

<style scoped>

</style>
