<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="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="materialCategories">
        <el-input
          v-model="queryParams.materialCategories"
          placeholder="请输入物料大类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料类型" prop="materialTypeId">
        <el-input
          v-model="queryParams.materialTypeId"
          placeholder="请输入物料类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次标识" prop="batchFlag">
        <el-input
          v-model="queryParams.batchFlag"
          placeholder="请输入批次标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="物料规格" prop="materialSpec">
        <el-input
          v-model="queryParams.materialSpec"
          placeholder="请输入物料规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['mes:materialinfo: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="['mes:materialinfo: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="['mes:materialinfo: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="['mes:materialinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料大类" align="center" prop="materialCategories" />
      <el-table-column label="物料类型" align="center" prop="materialTypeId" />
      <el-table-column label="批次标识" align="center" prop="batchFlag" />
      <el-table-column label="计量单位" align="center" prop="materialUnit" />
      <el-table-column label="物料规格" align="center" prop="materialSpec" />
      <el-table-column label="净重" align="center" prop="netWeight" />
      <el-table-column label="毛重" align="center" prop="grossWeight" />
      <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="['mes:materialinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:materialinfo: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="ERP的主键;对应FMATERIALID" prop="erpId">
          <el-input v-model="form.erpId" placeholder="请输入ERP的主键;对应FMATERIALID" />
        </el-form-item>
        <el-form-item label="物料编码;对应ERP的FNumber" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码;对应ERP的FNumber" />
        </el-form-item>
        <el-form-item label="旧物料编码" prop="oldMaterialCode">
          <el-input v-model="form.oldMaterialCode" placeholder="请输入旧物料编码" />
        </el-form-item>
        <el-form-item label="物料名称;对应ERP的FName" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称;对应ERP的FName" />
        </el-form-item>
        <el-form-item label="物料大类(1,原材料 2,半成品 3,成品)" prop="materialCategories">
          <el-input v-model="form.materialCategories" placeholder="请输入物料大类(1,原材料 2,半成品 3,成品)" />
        </el-form-item>
        <el-form-item label="物料小类;暂时不使用，预留" prop="materialSubclass">
          <el-input v-model="form.materialSubclass" placeholder="请输入物料小类;暂时不使用，预留" />
        </el-form-item>
        <el-form-item label="物料类型，关联mes_base_material_type的material_type_id" prop="materialTypeId">
          <el-input v-model="form.materialTypeId" placeholder="请输入物料类型，关联mes_base_material_type的material_type_id" />
        </el-form-item>
        <el-form-item label="批次标识，(0否，1是)" prop="batchFlag">
          <el-input v-model="form.batchFlag" placeholder="请输入批次标识，(0否，1是)" />
        </el-form-item>
        <el-form-item label="计量单位ID;对应FBaseUnitId" prop="materialUnitId">
          <el-input v-model="form.materialUnitId" placeholder="请输入计量单位ID;对应FBaseUnitId" />
        </el-form-item>
        <el-form-item label="计量单位名称" prop="materialUnit">
          <el-input v-model="form.materialUnit" placeholder="请输入计量单位名称" />
        </el-form-item>
        <el-form-item label="物料组" prop="materialMatkl">
          <el-input v-model="form.materialMatkl" placeholder="请输入物料组" />
        </el-form-item>
        <el-form-item label="物料规格;对应FSpecification" prop="materialSpec">
          <el-input v-model="form.materialSpec" placeholder="请输入物料规格;对应FSpecification" />
        </el-form-item>
        <el-form-item label="净重;FNETWEIGHT" prop="netWeight">
          <el-input v-model="form.netWeight" placeholder="请输入净重;FNETWEIGHT" />
        </el-form-item>
        <el-form-item label="毛重;FGROSSWEIGHT" prop="grossWeight">
          <el-input v-model="form.grossWeight" placeholder="请输入毛重;FGROSSWEIGHT" />
        </el-form-item>
        <el-form-item label="所属工厂，关联mes_base_factory_info的factory_id" prop="factoryId">
          <el-input v-model="form.factoryId" placeholder="请输入所属工厂，关联mes_base_factory_info的factory_id" />
        </el-form-item>
        <el-form-item label="创建组织;对应FCreateOrgId" prop="createOrgId">
          <el-input v-model="form.createOrgId" placeholder="请输入创建组织;对应FCreateOrgId" />
        </el-form-item>
        <el-form-item label="使用组织;对应FUseOrgId" prop="useOrgId">
          <el-input v-model="form.useOrgId" placeholder="请输入使用组织;对应FUseOrgId" />
        </el-form-item>
        <el-form-item label="所属产线;多个用,隔开" prop="prodlineId">
          <el-input v-model="form.prodlineId" placeholder="请输入所属产线;多个用,隔开" />
        </el-form-item>
        <el-form-item label="激活标识：1-是；0-否" prop="activeFlag">
          <el-input v-model="form.activeFlag" placeholder="请输入激活标识：1-是；0-否" />
        </el-form-item>
        <el-form-item label="删除标识(0否，1是)" prop="deletedFlag">
          <el-input v-model="form.deletedFlag" placeholder="请输入删除标识(0否，1是)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核日期;对应FApproveDate" prop="approveDate">
          <el-date-picker clearable
            v-model="form.approveDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核日期;对应FApproveDate">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="erp最后更新日期;对应FModifyDate" prop="erpModifyDate">
          <el-date-picker clearable
            v-model="form.erpModifyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择erp最后更新日期;对应FModifyDate">
          </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 { listMaterialinfo, getMaterialinfo, delMaterialinfo, addMaterialinfo, updateMaterialinfo } from "@/api/mes/materialinfo";

export default {
  name: "Materialinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息表格数据
      materialinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        erpId: null,
        materialCode: null,
        oldMaterialCode: null,
        materialName: null,
        materialCategories: null,
        materialSubclass: null,
        materialTypeId: null,
        batchFlag: null,
        materialUnitId: null,
        materialUnit: null,
        materialMatkl: null,
        materialSpec: null,
        netWeight: null,
        grossWeight: null,
        factoryId: null,
        createOrgId: null,
        useOrgId: null,
        prodlineId: null,
        activeFlag: null,
        deletedFlag: null,
        approveDate: null,
        erpModifyDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        erpId: [
          { required: true, message: "ERP的主键;对应FMATERIALID不能为空", trigger: "blur" }
        ],
        materialCode: [
          { required: true, message: "物料编码;对应ERP的FNumber不能为空", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "物料名称;对应ERP的FName不能为空", trigger: "blur" }
        ],
        materialCategories: [
          { required: true, message: "物料大类(1,原材料 2,半成品 3,成品)不能为空", trigger: "blur" }
        ],
        materialTypeId: [
          { required: true, message: "物料类型，关联mes_base_material_type的material_type_id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料信息列表 */
    getList() {
      this.loading = true;
      listMaterialinfo(this.queryParams).then(response => {
        this.materialinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        erpId: null,
        materialCode: null,
        oldMaterialCode: null,
        materialName: null,
        materialCategories: null,
        materialSubclass: null,
        materialTypeId: null,
        batchFlag: null,
        materialUnitId: null,
        materialUnit: null,
        materialMatkl: null,
        materialSpec: null,
        netWeight: null,
        grossWeight: null,
        factoryId: null,
        createOrgId: null,
        useOrgId: null,
        prodlineId: null,
        activeFlag: null,
        deletedFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        approveDate: null,
        erpModifyDate: 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.materialId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.materialId || this.ids
      getMaterialinfo(materialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialId != null) {
            updateMaterialinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterialinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$modal.confirm('是否确认删除物料信息编号为"' + materialIds + '"的数据项？').then(function() {
        return delMaterialinfo(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/materialinfo/export', {
        ...this.queryParams
      }, `materialinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
