<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="saleorderCode">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.saleorderCode"-->
    <!--          placeholder="请输入销售订单编号"-->
    <!--          clearable-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="物料ID" prop="materialId">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.materialId"-->
    <!--          placeholder="请输入物料ID"-->
    <!--          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="outstockAmount">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.outstockAmount"-->
    <!--          placeholder="请输入出库数量"-->
    <!--          clearable-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="分摊使用的数量" prop="usedAmount">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.usedAmount"-->
    <!--          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:saleOrderMaterial: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:saleOrderMaterial: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:saleOrderMaterial:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :loading="submitLoading"
          @click="submitForm"
          v-hasPermi="['mes:saleOrder:used']"
        >保存
        </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:saleOrder:used']"
        >导出
        </el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="saleOrderMaterialList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      <el-table-column label="主键标识" align="center" prop="saleOrderMaterialId" />-->
      <!--      <el-table-column label="销售订单编号" align="center" prop="saleorderCode" />-->
      <!--      <el-table-column label="物料ID" align="center" prop="materialId" />-->
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="物料编码" align="center" prop="materialCode"/>
      <el-table-column label="物料名称" align="center" prop="materialName"/>
      <el-table-column label="物料规格" align="center" prop="materialSpec"/>
      <el-table-column label="出库数量" align="center" prop="realOutstockAmount"/>
      <el-table-column label="其他订单分摊数量" align="center" prop="totalUsedAmount"/>
      <el-table-column label="此订单分摊数量" align="center" prop="usedAmount">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.usedAmount" placeholder="请输入使用数量">
          </el-input-number>
        </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="['mes:saleOrderMaterial:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['mes:saleOrderMaterial: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="销售订单编号" prop="saleorderCode">
          <el-input v-model="form.saleorderCode" placeholder="请输入销售订单编号"/>
        </el-form-item>
        <el-form-item label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID"/>
        </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="materialSpec">
          <el-input v-model="form.materialSpec" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="出库数量" prop="outstockAmount">
          <el-input v-model="form.outstockAmount" placeholder="请输入出库数量"/>
        </el-form-item>
        <el-form-item label="分摊使用的数量" prop="usedAmount">
          <el-input v-model="form.usedAmount" 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 {selectMaterialOutstockUsedInfos,updateMesSaleOrderMaterials} from "@/api/mes/saleOrder";

export default {
  name: "SaleOrderMaterial",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单使用物料情况表格数据
      saleOrderMaterialList: [],
      submitLoading:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleorderCode: null,
        materialId: null,
        materialCode: null,
        materialName: null,
        materialSpec: null,
        outstockAmount: null,
        usedAmount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        saleorderCode: [
          {required: true, message: "销售订单编号不能为空", trigger: "blur"}
        ],
        materialId: [
          {required: true, message: "物料ID不能为空", trigger: "blur"}
        ],
        materialCode: [
          {required: true, message: "物料编码不能为空", trigger: "blur"}
        ],
        updateBy: [
          {required: true, message: "更新人不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售订单使用物料情况列表 */
    getList() {
      this.loading = true;
      let saleorderCode = this.$route.query && this.$route.query.saleorderCode;
      this.queryParams.saleorderCode = saleorderCode;
      selectMaterialOutstockUsedInfos(this.queryParams).then(response => {
        response.rows.forEach(e => {
          e.oriUsedAmount = e.usedAmount
          e.totalUsedAmount = e.totalUsedAmount == null ? 0:e.totalUsedAmount;
          e.usedAmount = e.usedAmount == null ? 0:e.usedAmount;
          e.totalUsedAmount = e.totalUsedAmount - e.usedAmount;//其他销售订单分摊的，不包括当前销售订单
          e.saleorderCode = saleorderCode

        });

        this.saleOrderMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        saleOrderMaterialId: null,
        saleorderCode: null,
        materialId: null,
        materialCode: null,
        materialName: null,
        materialSpec: null,
        outstockAmount: null,
        usedAmount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.saleOrderMaterialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      this.submitLoading = true;
      let errorMsg="";
      this.saleOrderMaterialList.forEach((e,index) => {
          if(e.usedAmount && e.usedAmount!==null){
            let realOutstockAmount = e.realOutstockAmount == null ? 0 :e.realOutstockAmount;
            let totalUsedAmount = e.totalUsedAmount == null ? 0 :e.totalUsedAmount;
            let usedAmount = e.usedAmount == null ? 0 :e.usedAmount;
            let newTotalUsedAmount = totalUsedAmount+usedAmount;
            if(realOutstockAmount < newTotalUsedAmount){
               errorMsg+="第"+(parseInt(index)+1)+"行，总共分摊数量为"+newTotalUsedAmount+",大于出库数量"+realOutstockAmount+";\n";
            }
          }
      });

      if(errorMsg!==""){
        this.$modal.msgWarning(errorMsg);
        this.submitLoading=false;
        return;
      }

      updateMesSaleOrderMaterials(this.saleOrderMaterialList).then(response => {
        this.$modal.msgSuccess("保存成功");
        this.submitLoading=false;
        this.getList();
      });

    },

    /** 导出按钮操作 */
    handleExport() {
      let saleorderCode = this.$route.query && this.$route.query.saleorderCode;
      this.download('mes/saleOrder/exportSaleOrderMaterials', {
        ...this.queryParams
      }, `物料情况使用_`+saleorderCode+`_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
