<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="materialMatnr">
        <el-input
          v-model="queryParams.materialMatnr"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="materialMaktx">
        <el-input
          v-model="queryParams.materialMaktx"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标识" prop="materialMrbs">
        <el-select v-model="queryParams.materialMrbs" placeholder="请选择标识(100米/卷)" clearable>
          <el-option
            v-for="dict in dict.type.sap_segmentation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="materialSfyx">
        <el-select v-model="queryParams.materialSfyx" placeholder="请选择是否有效" clearable>
          <el-option
            v-for="dict in dict.type.sap_material_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['sapMaterial:sapMaterial: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="['sapMaterial:sapMaterial: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="['sapMaterial:sapMaterial: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="['sapMaterial:sapMaterial:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sapMaterialList" :row-class-name="rowSapMaterialIndex" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="index" width="80"/>
      <el-table-column label="物料id" align="center" prop="materialId" v-if="false"/>
      <el-table-column label="编码" align="center" prop="materialMatnr" />
      <el-table-column label="名称" width="250" prop="materialMaktx" />
      <el-table-column label="销售组织" align="center" prop="materialVkorg" />
      <el-table-column label="分销渠道" align="center" prop="materialVtweg" />
      <el-table-column label="单位" align="center" prop="materialMeins" />
      <el-table-column label="产品组" align="center" prop="materialSpart" />
      <el-table-column label="产品组描述" align="center" prop="materialVtext" />
      <el-table-column label="类别" align="center" prop="materialMatkl" />
      <el-table-column label="类别描述" align="center" prop="materialWgbez" />
      <el-table-column label="单位净重" align="center" prop="materialDwjz" />
      <el-table-column label="厂价" align="center" prop="materialChangj" />
      <el-table-column label="标识" align="center" prop="materialMrbs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sap_segmentation" :value="scope.row.materialMrbs"/>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="materialSfyx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sap_material_state" :value="scope.row.materialSfyx"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sapMaterial:sapMaterial:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sapMaterial:sapMaterial: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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="编码" prop="materialMatnr" label-width="100px">
              <el-input v-model="form.materialMatnr" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="名称" prop="materialMaktx" label-width="100px">
              <el-input v-model="form.materialMaktx" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售组织" prop="materialVkorg" label-width="100px">
              <el-input v-model="form.materialVkorg" placeholder="请输入销售组织" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="分销渠道" prop="materialVtweg" label-width="100px">
              <el-input v-model="form.materialVtweg" placeholder="请输入分销渠道" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="materialMeins" label-width="100px">
              <el-input v-model="form.materialMeins" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品组" prop="materialSpart" label-width="100px">
              <el-input v-model="form.materialSpart" placeholder="请输入产品组" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品组描述" prop="materialVtext" label-width="100px">
              <el-input v-model="form.materialVtext" placeholder="请输入产品组描述" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类别" prop="materialMatkl" label-width="100px">
              <el-input v-model="form.materialMatkl" placeholder="请输入类别" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类别描述" prop="materialWgbez" label-width="100px">
              <el-input v-model="form.materialWgbez" placeholder="请输入类别描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单位净重" prop="materialDwjz" label-width="100px">
              <el-input v-model="form.materialDwjz" placeholder="请输入单位净重" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标识" prop="materialMrbs" label-width="100px">
              <el-select v-model="form.materialMrbs" placeholder="请选择标识(100米/卷)">
                <el-option
                  v-for="dict in dict.type.sap_segmentation"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有效" prop="materialSfyx" label-width="100px">
              <el-select v-model="form.materialSfyx" placeholder="请选择是否有效">
                <el-option
                  v-for="dict in dict.type.sap_material_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listSapMaterial, getSapMaterial, delSapMaterial, addSapMaterial, updateSapMaterial } from "@/api/sapMaterial/sapMaterial";

export default {
  name: "SapMaterial",
  dicts: ['sap_segmentation', 'sap_material_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料管理表格数据
      sapMaterialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialMatnr: null,
        materialMaktx: null,
        materialMrbs: null,
        materialSfyx: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialMatnr: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        materialMaktx: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        materialVkorg: [
          { required: true, message: "销售组织不能为空", trigger: "blur" }
        ],
        materialVtweg: [
          { required: true, message: "分销渠道不能为空", trigger: "blur" }
        ],
        materialMeins: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        materialSpart: [
          { required: true, message: "产品组不能为空", trigger: "blur" }
        ],
        materialVtext: [
          { required: true, message: "产品组描述不能为空", trigger: "blur" }
        ],
        materialMatkl: [
          { required: true, message: "类别不能为空", trigger: "blur" }
        ],
        materialWgbez: [
          { required: true, message: "类别描述不能为空", trigger: "blur" }
        ],
        materialDwjz: [
          { required: true, message: "单位净重不能为空", trigger: "blur" }
        ],
        materialSfyx: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料管理列表 */
    getList() {
      this.loading = true;
      listSapMaterial(this.queryParams).then(response => {
        this.sapMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        materialMatnr: null,
        materialMaktx: null,
        materialVkorg: null,
        materialVtweg: null,
        materialMeins: null,
        materialSpart: null,
        materialVtext: null,
        materialMatkl: null,
        materialWgbez: null,
        materialDwjz: null,
        materialChangj: null,
        materialXingh: null,
        materialGuig: null,
        materialJiem: null,
        materialDiany: null,
        materialTemp: null,
        materialModel: null,
        materialMrbs: null,
        materialSfyx: 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
      getSapMaterial(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) {
            updateSapMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSapMaterial(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 delSapMaterial(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sapMaterial/sapMaterial/export', {
        ...this.queryParams
      }, `sapMaterial_${new Date().getTime()}.xlsx`)
    },
    /** 序号 */
    rowSapMaterialIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
  }
};
</script>
