<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="materialNumber">
        <el-input
          v-model="queryParams.materialNumber"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          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"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col> -->

    </el-row>

    <el-table v-loading="loading" :data="definitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="物料编号" align="center" prop="materialNumber" />
      <el-table-column label="物料块描写" align="center" prop="materialDescription" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="包装单位" align="center" prop="packagingUnit" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="牌号" align="center" prop="grade" />
      <el-table-column label="净重" align="center" prop="netWeight" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="到货天数" align="center" prop="materialDay" />
      <el-table-column label="加工天数" align="center" prop="workDay" />
      <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)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <!-- 添加或修改物料定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal="false" modal-append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编号" prop="materialNumber">
          <el-input v-model="form.materialNumber" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="物料块描写" prop="materialDescription">
          <el-input v-model="form.materialDescription" placeholder="请输入物料块描写" />
        </el-form-item>
        <el-form-item label="供应商编码" prop="supplierCode">
          <el-input v-model="form.supplierCode" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="包装单位" prop="packagingUnit">
          <el-input v-model="form.packagingUnit" placeholder="请输入包装单位" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="牌号" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入牌号" />
        </el-form-item>
        <el-form-item label="净重" prop="netWeight">
          <el-input v-model="form.netWeight" 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="materialDay">
          <el-input v-model="form.materialDay" placeholder="请输入物料到货天数" />
        </el-form-item>
        <el-form-item label="加工天数" prop="workDay">
          <el-input v-model="form.workDay" placeholder="请输入物料加工天数" />
        </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 { listDefinition, getDefinition, delDefinition, addDefinition, updateDefinition } from "@/api/aps_material_definition/definition";

export default {
  name: "Definition",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料定义表格数据
      definitionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNumber: null,
        supplierCode: null,
        packagingUnit: null,
        model: null,
        grade: null,
        netWeight: null,
        unit: null,
        materialDescription: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialNumber: [
          { required: true, message: "物料编号不能为空", trigger: "blur" }
        ],
        supplierCode: [
          { required: true, message: "供应商编码不能为空", trigger: "blur" }
        ],
        packagingUnit: [
          { required: true, message: "包装单位不能为空", trigger: "blur" }
        ],
        // model: [
        //   { required: true, message: "型号不能为空", trigger: "blur" }
        // ],
        // grade: [
        //   { required: true, message: "牌号不能为空", trigger: "blur" }
        // ],
        netWeight: [
          { required: true, message: "净重不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        materialDescription: [
          { required: true, message: "物料块描写不能为空", trigger: "blur" }
        ],
        materialDay: [
          { required: true, message: "到料天数不能为空", trigger: "blur" }
        ],
        workDay: [
          { required: true, message: "生产天数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料定义列表 */
    getList() {
      this.loading = true;
      listDefinition(this.queryParams).then(response => {
        this.definitionList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNumber: null,
        supplierCode: null,
        packagingUnit: null,
        model: null,
        grade: null,
        netWeight: null,
        unit: null,
        materialDescription: null,
        materialDay: null,
        workDay: null
      };

    },
    /** 搜索按钮操作 */
    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
      getDefinition(id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改物料定义";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDefinition(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDefinition(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物料定义编号为"' + ids + '"的数据项？').then(function() {
        return delDefinition(ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('aps_material_definition/definition/export', {
      //   ...this.queryParams
      // }, `definition_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
