<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="物料编码">
            <el-input
              v-model="query.material_code"
              size="mini"
              clearable
              placeholder="物料编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="供应商">
            <el-input
              v-model="query.supp_code"
              size="mini"
              clearable
              placeholder="供应商编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <rrOperation />
        </el-form>
      </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="1100px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="物料选择" prop="cust_code">
                <el-select
                  v-model="form.material_id"
                  clearable
                  filterable
                  placeholder="可搜索"
                  class="filter-item"
                  style="width: 180px"
                  :disabled="crud.status.edit > 0"
                >
                  <el-option
                    v-for="item in materialList"
                    :key="item.material_id"
                    :label="item.material_name"
                    :value="item.material_id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商 " prop="cust_name">
                <el-select
                  v-model="form.supp_id"
                  clearable
                  filterable
                  placeholder="可搜索"
                  class="filter-item"
                  style="width: 180px"
                  :disabled="crud.status.edit > 0"
                >
                  <el-option
                    v-for="item in suppList"
                    :key="item.supp_id"
                    :label="item.supp_name"
                    :value="item.supp_id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="数量">
                <el-input-number
                  v-model="form.qty"
                  :controls="false"
                  :precision="3"
                  :min="0"
                  style="width: 180px"
                />
              </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="material_code" label="物料编码" />
        <el-table-column prop="material_name" label="物料名称" />
        <el-table-column prop="supp_name" label="供应商名称" width="250px" show-overflow-tooltip/>
        <el-table-column prop="qty" label="数量" :formatter="crud.formatNum3" />
        <el-table-column prop="create_name" label="创建者" />
        <el-table-column prop="create_time" label="创建时间" width="150" />
        <el-table-column prop="update_name" label="修改者" />
        <el-table-column prop="update_time" label="修改时间" width="150" />
        <el-table-column
          v-permission="['admin','customerbase:edit','customerbase: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 crudSpendrowqtyfrom from '@/api/wms/basedata/pdm/spendrowqty'
import CRUD, { crud, form, header, presenter } 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'

const defaultForm = {
  spend_id: null,
  material_id: null,
  supp_id: null,
  qty: null,
  remark: null,
  create_id: null,
  create_name: null,
  create_time: null,
  update_id: null,
  update_name: null,
  update_time: null
}
export default {
  name: 'Spendrowqty',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '预排量维护',
      url: 'api/spendRowQty',
      optShow: {
        add: true,
        reset: true
      },
      idField: 'spend_id',
      sort: 'spend_id,desc',
      crudMethod: { ...crudSpendrowqtyfrom }
    })
  },
  data() {
    return {
      permission: {},
      materialList: [],
      suppList: [],
      rules: {
        material_id: [
          { required: true, message: '物料不能为空', trigger: 'blur' }
        ],
        supp_id: [
          { required: true, message: '供应商不能为空', trigger: 'blur' }
        ],
        qty: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    crudSpendrowqtyfrom.getMaterialList().then(res => {
      this.materialList = res
    })
    crudSpendrowqtyfrom.getSuppList().then(res => {
      this.suppList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
