<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="工厂" prop="factoryCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.factoryCode"-->
<!--          placeholder="请输入工厂"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仓库" prop="waCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.waCode"-->
<!--          placeholder="请输入仓库"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="库区" prop="wlCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.wlCode"-->
<!--          placeholder="请输入库区"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="物料编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商" prop="supplyName">-->
<!--        <el-input-->
<!--          v-model="queryParams.supplyName"-->
<!--          placeholder="请输入供应商"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="计划数量" prop="amnountPlan">-->
<!--        <el-input-->
<!--          v-model="queryParams.amnountPlan"-->
<!--          placeholder="请输入计划数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="收货数量" prop="amountReal">-->
<!--        <el-input-->
<!--          v-model="queryParams.amountReal"-->
<!--          placeholder="请输入收货数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="userDefined1">-->
<!--        <el-input-->
<!--          v-model="queryParams.userDefined1"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="userDefined2">-->
<!--        <el-input-->
<!--          v-model="queryParams.userDefined2"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="userDefined3">-->
<!--        <el-input-->
<!--          v-model="queryParams.userDefined3"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="userDefined4">-->
<!--        <el-input-->
<!--          v-model="queryParams.userDefined4"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="userDefined5">-->
<!--        <el-input-->
<!--          v-model="queryParams.userDefined5"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="gmtCreate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.gmtCreate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新人" prop="modifiedBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.modifiedBy"-->
<!--          placeholder="请输入更新人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="gmtModified">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.gmtModified"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择更新时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['wms:raw:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['wms:raw:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['wms:raw:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:raw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" :index="indexMethod" label="序号" width="50" align="center"></el-table-column>
<!--      <el-table-column label="工厂" align="center" prop="factoryCode" />-->
      <el-table-column label="仓库" align="center" prop="waCode" />
<!--      <el-table-column label="库区" align="center" prop="wlCode" />-->
      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="单位" align="center" prop="unit" />
<!--      <el-table-column label="供应商" align="center" prop="supplyName" />-->
<!--      <el-table-column label="计划数量" align="center" prop="amnountPlan" />-->
      <el-table-column label="库存数量" align="center" prop="amountReal" />
      <el-table-column label="安全库存" align="center" prop="userDefined1" />
      <el-table-column label="需求库存" align="center" prop="userDefined2" />
<!--      <el-table-column label="备用" align="center" prop="userDefined3" />-->
<!--      <el-table-column label="备用" align="center" prop="userDefined4" />-->
<!--      <el-table-column label="备用" align="center" prop="userDefined5" />-->
<!--      <el-table-column label="创建时间" align="center" prop="gmtCreate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="更新人" align="center" prop="modifiedBy" />-->
<!--      <el-table-column label="更新时间" align="center" prop="gmtModified" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.gmtModified, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['wms:raw:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['wms:raw:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改白坯原材料入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="主键ID" prop="id">-->
<!--          <el-input v-model="form.id" placeholder="请输入主键ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="工厂" prop="factoryCode">-->
<!--          <el-input v-model="form.factoryCode" placeholder="请输入工厂" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="仓库" prop="waCode">-->
<!--          <el-input v-model="form.waCode" placeholder="请输入仓库" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="库区" prop="wlCode">-->
<!--          <el-input v-model="form.wlCode" placeholder="请输入库区" />-->
<!--        </el-form-item>-->
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
<!--        <el-form-item label="单位" prop="unit">-->
<!--          <el-input v-model="form.unit" placeholder="请输入单位" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="供应商" prop="supplyName">-->
<!--          <el-input v-model="form.supplyName" placeholder="请输入供应商" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="计划数量" prop="amnountPlan">-->
<!--          <el-input v-model="form.amnountPlan" placeholder="请输入计划数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="收货数量" prop="amountReal">
          <el-input v-model="form.amountReal" placeholder="请输入收货数量" />
        </el-form-item>
<!--        <el-form-item label="备用" prop="userDefined1">-->
<!--          <el-input v-model="form.userDefined1" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="userDefined2">-->
<!--          <el-input v-model="form.userDefined2" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="userDefined3">-->
<!--          <el-input v-model="form.userDefined3" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="userDefined4">-->
<!--          <el-input v-model="form.userDefined4" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="userDefined5">-->
<!--          <el-input v-model="form.userDefined5" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="创建时间" prop="gmtCreate">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.gmtCreate"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择创建时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新人" prop="modifiedBy">-->
<!--          <el-input v-model="form.modifiedBy" placeholder="请输入更新人" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新时间" prop="gmtModified">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.gmtModified"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择更新时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRaw, getRaw, delRaw, addRaw, updateRaw } from "@/api/wms/raw";

export default {
  name: "Raw",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tableData: [
        // ... 表格数据
      ],
      currentPage: 1, // 当前页码
      pageSize: 10 ,   // 每页显示的条数
      // 白坯原材料入库表格数据
      rawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        waCode: null,
        wlCode: null,
        materialCode: null,
        materialName: null,
        unit: null,
        supplyName: null,
        amnountPlan: null,
        amountReal: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        gmtCreate: null,
        modifiedBy: null,
        gmtModified: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    indexMethod(index) {
      // 当前页码减1乘以每页的条数再加上索引，计算出序号
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    /** 查询白坯原材料入库列表 */
    getList() {
      this.loading = true;
      listRaw(this.queryParams).then(response => {
        this.rawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        waCode: null,
        wlCode: null,
        materialCode: null,
        materialName: null,
        unit: null,
        supplyName: null,
        amnountPlan: null,
        amountReal: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        createBy: null,
        gmtCreate: null,
        modifiedBy: null,
        gmtModified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加白坯原材料入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRaw(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改白坯原材料入库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRaw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRaw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除白坯原材料入库编号为"' + ids + '"的数据项？').then(function() {
        return delRaw(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/raw/export', {
        ...this.queryParams
      }, `raw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
