<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
        <el-row>
          <el-col :span="4">
            物料查询：
            <el-input
              v-model="query.material_code"
              clearable
              style="width: 150px"
              size="mini"
              placeholder="物料编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-col>
          <el-col :span="4">
            批次查询：
            <el-input
              v-model="query.pcsn"
              clearable
              style="width: 150px"
              size="mini"
              placeholder="批次"
              @keyup.enter.native="crud.toQuery"
            />
          </el-col>
          <el-col :span="4">
            载具编码：
            <el-input
              v-model="query.storagevehicle_code"
              clearable
              style="width: 150px"
              size="mini"
              placeholder="载具编码"
              @keyup.enter.native="crud.toQuery"
            />
          </el-col>
          <el-col :span="4">
            组盘状态：
            <el-select
              v-model="query.status"
              clearable
              style="width: 150px"
              size="mini"
              placeholder="状态"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.GROUP_STATUS"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="4">
            <rrOperation />
          </el-col>
        </el-row>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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="1200px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="110px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="物料编码" prop="material_code" >
                <el-input v-model="form.material_code" @focus="getMaterial" style="width: 200px;" :disabled="crud.status.edit > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item  label="物料名称" prop="material_name">
                <el-input disabled v-model="form.material_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item  label="规格" prop="material_spec">
                <label slot="label">规&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格</label>
                <el-input disabled v-model="form.material_spec" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="载具编码" prop="storagevehicle_code">
                <label slot="label">载具编码</label>
                <el-input v-model="form.storagevehicle_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="物料批号" prop="pcsn">
                <el-input v-model="form.pcsn" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="物料数量" prop="qty">
                <el-input-number v-model="form.qty" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="单位" prop="qty_unit_name">
                <el-select
                  v-model="form.qty_unit_name"
                  style="width: 100px"
                  placeholder=""
                  @change="unitChange"
                >
                  <el-option
                    v-for="item in tableEnum.md_pb_measureunit"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value+'@'+item.label"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="源单号" prop="ext_code">
                <el-input v-model="form.ext_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="源单类型" prop="ext_type">
                <el-input v-model="form.ext_type" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column prop="storagevehicle_code" label="载具编码" :min-width="flexWidth('storagevehicle_code',crud.data,'载具编码')" />
        <el-table-column prop="material_code" label="物料编码" :min-width="flexWidth('material_code',crud.data,'物料编码')" />
        <el-table-column prop="material_name" label="物料名称" :min-width="flexWidth('material_name',crud.data,'物料名称')" />
        <el-table-column prop="pcsn" label="批次" :min-width="flexWidth('pcsn',crud.data,'批次')" />
        <el-table-column prop="status" label="状态" :formatter="formattStatus" :min-width="flexWidth('status',crud.data,'状态')" />
        <el-table-column prop="qty" label="组盘数量" :formatter="crud.formatNum3" :min-width="100" />
        <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" />
        <el-table-column prop="load_port" label="用料位置" :min-width="flexWidth('load_port',crud.data,'用料位置')" />
        <el-table-column prop="create_name" label="组盘人" :min-width="flexWidth('create_name',crud.data,'组盘人')" />
        <el-table-column prop="create_time" label="组盘时间" :min-width="flexWidth('create_time',crud.data,'组盘时间')" />
        <el-table-column
          v-permission="['admin','Supplierbase:edit','Supplierbase:del']"
          label="操作"
          width="150px"
          lign="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :is-visiable-edit="false"
              :disabled-dle="scope.row.status === '02'"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <!--放引用的组件-->
    <MaterialDialog :dialog-show.sync="materialDialog" @materialChoose="materialChoose" />
    <AddDialog @AddChanged="querytable" />

  </div>
</template>

<script>
import crudGroup from '@/views/wms/basedata/group/group'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import MaterialDialog from '@/views/wms/basedata/material/MaterialDialog'
import AddDialog from '@/views/wms/basedata/group/AddDialog'

const defaultForm = {
  group_id: null,
  storagevehicle_code: null,
  material_id: null,
  material_name: null,
  material_spec: null,
  pcsn: null,
  qty_unit_id: null,
  qty_unit_name: null,
  qty: null,
  remark: null,
  status: null,
  create_id: null,
  create_name: null,
  create_time: null,
  ext_code: null,
  ext_type: null
}
export default {
  name: 'Group',
  components: { pagination, MaterialDialog, crudOperation, rrOperation, udOperation, AddDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  tableEnums: ['md_pb_measureunit#unit_name#measure_unit_id'],
  // 数据字典
  dicts: ['is_used', 'GROUP_STATUS'],
  cruds() {
    return CRUD({
      title: '组盘记录',
      url: 'api/group',
      optShow: {
        add: true,
        reset: true
      },
      idField: 'group_id',
      sort: 'group_id,desc',
      crudMethod: { ...crudGroup }
    })
  },
  data() {
    return {
      permission: {},
      materialDialog: false,
      classes: [],
      rules: {
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    querytable() {
      this.crud.toQuery()
    },
    formattStatus(row) {
      return this.dict.label.GROUP_STATUS[row.status]
    },
    getMaterial() {
      this.materialDialog = true
    },
    unitChange(row) {
      const split = row.split('@')
      this.form.qty_unit_id = split[0]
      this.form.qty_unit_name = split[1]
    },
    materialChoose(row) {
      this.form.material_name = row.material_name
      this.form.material_id = row.material_id
      this.form.material_code = row.material_code
      this.form.material_spec = row.material_spec
    }
  }
}
</script>

<style scoped>

</style>
