<template>
  <div class="app-container">



    <el-form ref="form" :model="form" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="采购订单编号" prop="poNo">
            <el-input v-model="form.poNo" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单计划数量" prop="orderAmount">
            <el-input v-model="form.orderAmount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>



      <el-row>
        <el-col :span="12">
          <el-form-item label="物料编码" prop="materialCode">
            <el-input v-model="form.materialCode" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料名称" prop="materialName">
            <el-input v-model="form.materialName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="审核日期" prop="approveDate">
            <el-input v-model="form.approveDate" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划交货日期" prop="planDeliveryDate">
            <el-input v-model="form.planDeliveryDate" disabled/>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col :span="12">
          <el-form-item label="订单状态" prop="orderStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.order_status" :value="form.orderStatus"/>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="完成日期" prop="completeDate">
            <el-input v-model="form.completeDate" disabled/>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col :span="12">
          <el-form-item label="源单编号" prop="srcBillNo">
            <el-input v-model="form.srcBillNo" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="京源项目" prop="tondBase">
            <el-input v-model="form.tondBase" disabled/>
          </el-form-item>
        </el-col>
      </el-row>


      <el-divider content-position="center">采购订单绑定信息</el-divider>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMesOrderBind">添加</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMesOrderBind">删除</el-button>
        </el-col>
      </el-row>
      <el-table :data="mesOrderBindList" :row-class-name="rowMesOrderBindIndex"
                @selection-change="handleMesOrderBindSelectionChange" ref="mesOrderBind">
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="序号" align="center" prop="index" width="50"/>
        <el-table-column label="安全库存标识" prop="safeFlag" width="150">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.safeFlag" @input="changeSafeFlag(scope.row)">
              <el-radio
                v-for="dict in dict.type.mes_safe_flag"
                :key="dict.value"
                :label="dict.value"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column label="销售订单" prop="saleOrderCode">
          <template slot-scope="scope">
            <el-input v-model="scope.row.saleOrderCode" placeholder="请点击右侧选择销售订单" readonly>
              <el-button slot="append" icon="el-icon-search" @click="handleSaleOrderAdd(scope.row)"></el-button>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="成品编码" prop="productCode" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.productCode" readonly/>
          </template>
        </el-table-column>
        <el-table-column label="成品名称" prop="productName" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.productName" readonly/>
          </template>
        </el-table-column>
        <el-table-column label="绑定数量" prop="bindAmount" width="260">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.bindAmount" placeholder="请输入绑定数量">
            </el-input-number>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">关 闭</el-button>
    </div>


    <!-- 添加物料信息对话框 -->
    <el-dialog title="选择销售订单" :visible.sync="saleOrderOpen" append-to-body>
      <select-saleOrder ref="saleOrderRef"></select-saleOrder>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSaleOrderForm">确 定</el-button>
        <el-button @click="saleOrderOpen = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {getPurchaseOrder, bindOrder} from "@/api/mes/purchaseOrder";

import selectSaleOrder from '@//views/mes/purchaseOrder/selectSaleOrder.vue';


export default {
  name: "PurchaseOrder",
  components: {
    'select-saleOrder': selectSaleOrder,
  },
  dicts: ['active_flag', 'mes_purchase_order_status', 'document_status', 'mes_safe_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMesOrderBind: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单信息表格数据
      purchaseOrderList: [],
      // 采购销售订单绑定信息;销售订单绑定采购订单明细信息表格数据
      mesOrderBindList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        erpId: null,
        fentryId: null,
        poNo: null,
        documentStatus: null,
        materialId: null,
        materialCode: null,
        materialName: null,
        orderAmount: null,
        completeAmount: null,
        approveDate: null,
        erpModifyDate: null,
        planDeliveryDate: null,
        beginDate: null,
        endDate: null,
        orderStatus: null,
        completeDate: null,
        isFlag: null,
        unitId: null,
        stockUnitId: null,
        priceUnitId: null,
        auxPropId: null,
        srcBillNo: null,
        purchaseOrgId: null,
        tondBase: null,
        supplierId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialCode: [
          {required: true, message: "物料编码;对应FMaterialId.FNumber不能为空", trigger: "blur"}
        ],
        orderAmount: [
          {required: true, message: "订单计划数量;对应FQty不能为空", trigger: "blur"}
        ],
        isFlag: [
          {required: true, message: "是否标识：1-是；0-否不能为空", trigger: "change"}
        ],
      },

      saleOrderOpen: false
    };
  },
  created() {
    const purchaseOrderId = this.$route.params && this.$route.params.purchaseOrderId;
    getPurchaseOrder(purchaseOrderId).then(response => {
      this.form = response.data;
      this.mesOrderBindList = response.data.mesOrderBindList;
    });

  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.loading = true;
      this.form.mesOrderBindList = this.mesOrderBindList;
      if (this.form.purchaseOrderId != null) {
        bindOrder(this.form).then(response => {
          this.$modal.msgSuccess("绑定成功");
          this.close();
        });
      }

    },

    /** 采购销售订单绑定信息;销售订单绑定采购订单明细信息序号 */
    rowMesOrderBindIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 采购销售订单绑定信息;销售订单绑定采购订单明细信息添加按钮操作 */
    handleAddMesOrderBind() {
      // this.open=true;
      let obj = {};
      obj.safeFlag = "";
      obj.saleOrderId = "";
      obj.saleOrderCode = "";
      obj.productId = "";
      obj.productCode = "";
      obj.productName = "";
      obj.bindAmount = "";
      obj.remark = "";
      this.mesOrderBindList.push(obj);
    },
    /** 采购销售订单绑定信息;销售订单绑定采购订单明细信息删除按钮操作 */
    handleDeleteMesOrderBind() {
      if (this.checkedMesOrderBind.length == 0) {
        this.$modal.msgError("请先选择要删除的采购订单绑定信息");
      } else {
        const mesOrderBindList = this.mesOrderBindList;
        const checkedMesOrderBind = this.checkedMesOrderBind;
        this.mesOrderBindList = mesOrderBindList.filter(function (item) {
          return checkedMesOrderBind.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMesOrderBindSelectionChange(selection) {
      this.checkedMesOrderBind = selection.map(item => item.index)
    },


    handleSaleOrderAdd(row) {
      this.editedRow = row;
      this.saleOrderOpen = true;
    },


    /** 提交选择销售订单信息按钮 */
    submitSaleOrderForm() {
      let selectedRow = this.$refs.saleOrderRef.selectedRow;
      this.editedRow.saleOrderId = selectedRow.saleOrderId;
      this.editedRow.saleOrderCode = selectedRow.saleorderCode;
      this.editedRow.productCode = selectedRow.materialCode;
      this.editedRow.productName = selectedRow.materialName;
      this.editedRow.productId = selectedRow.materialId;
      this.saleOrderOpen = false;

    },

    changeSafeFlag(row) {
      if (row.safeFlag == "1") {
        row.saleOrderId = "";
        row.saleOrderCode = "";
        row.productCode = "";
        row.productName = "";
        row.productId = "";
      }
    },

    /** 关闭按钮 */
    close() {
      const obj = {path: "/mes/plan/purchaseOrder", query: {t: Date.now(), pageNum: this.$route.query.pageNum}};
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
