<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="productionOrderNo">
        <el-input
          v-model="queryParams.productionOrderNo"
          placeholder="请输入生产令号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产图号" prop="drawingNo">
        <el-input
          v-model="queryParams.drawingNo"
          placeholder="请输入生产图号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产名称" prop="drawingName">
        <el-input
          v-model="queryParams.drawingName"
          placeholder="请输入生产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入生产数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产材料" prop="material">
        <el-input
          v-model="queryParams.material"
          placeholder="请输入生产材料"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单重" prop="singleWeight">
        <el-input
          v-model="queryParams.singleWeight"
          placeholder="请输入单重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总重" prop="totalWeight">
        <el-input
          v-model="queryParams.totalWeight"
          placeholder="请输入总重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部件名称" prop="parentPart">
        <el-input
          v-model="queryParams.parentPart"
          placeholder="请输入部件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部件图号" prop="parentDrawingNo">
        <el-input
          v-model="queryParams.parentDrawingNo"
          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="['system:order: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="['system:order: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="['system:order: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="['system:order:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:details:import']"
        >导入
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  v-loading="loading" :data="orderList" border  :span-method="spanMethod" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="生产令号" align="center" prop="productionOrderNo" />
      <el-table-column label="生产图号" align="center" prop="drawingNo" >
      <template slot-scope="scope">
        <el-button v-if="scope.row.material == '组焊件'" @click="openProductionInfo(scope.row)" type="primary">
          {{ scope.row.drawingNo }}
        </el-button>
        <div v-else>
          {{ scope.row.drawingNo }}
        </div>
      </template>
      </el-table-column>
      <el-table-column label="生产名称" align="center" prop="drawingName" />
      <el-table-column label="生产数量" align="center" prop="quantity" />
      <el-table-column label="生产材料" align="center" prop="material" >
      </el-table-column>
      <el-table-column label="单重" align="center" prop="singleWeight" />
      <el-table-column label="总重" align="center" prop="totalWeight" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="部件名称" align="center" prop="parentPart" />
      <el-table-column label="部件图号" align="center" prop="parentDrawingNo" />
      <el-table-column label="操作" fixed="right" 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="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order: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-drawer title="计划详情" :visible.sync="drawer" :with-header="false" direction="rtl" size="50%">

      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span class="card-title">采购</span>
        </div>

        <el-table style="width: 100%;" max-height="300" v-loading="figureSaveLoading" :data="figureSaveList">
          <el-table-column label="生产名称" align="center" prop="drawingName" />
          <el-table-column label="生产数量" align="center" prop="quantity" />
          <el-table-column label="生产材料" align="center" prop="material" />
          <el-table-column label="单重" align="center" prop="singleWeight" />
          <el-table-column label="总重" align="center" prop="totalWeight" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="部件名称" align="center" prop="parentPart" />
          <el-table-column label="部件图号" align="center" prop="parentDrawingNo" />
          <!-- 操作列 -->
          <!-- <el-table-column prop="operation" label="操作" width="200px"> </el-table-column> -->
          <!-- 进度列 -->
          <!-- <el-table-column prop="operation" label="进度" width="300px"> </el-table-column> -->
        </el-table>
      </el-card>
    </el-drawer>

    <!-- 添加或修改生产订单对话框 -->
    <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="productionOrderNo">
          <el-input v-model="form.productionOrderNo" placeholder="请输入生产令号" />
        </el-form-item>
        <el-form-item label="生产图号" prop="drawingNo">
          <el-input v-model="form.drawingNo" placeholder="请输入生产图号" />
        </el-form-item>
        <el-form-item label="生产名称" prop="drawingName">
          <el-input v-model="form.drawingName" placeholder="请输入生产名称" />
        </el-form-item>
        <el-form-item label="生产数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入生产数量" />
        </el-form-item>
        <el-form-item label="生产材料" prop="material">
          <el-input v-model="form.material" placeholder="请输入生产材料" />
        </el-form-item>
        <el-form-item label="单重" prop="singleWeight">
          <el-input v-model="form.singleWeight" placeholder="请输入单重" />
        </el-form-item>
        <el-form-item label="总重" prop="totalWeight">
          <el-input v-model="form.totalWeight" placeholder="请输入总重" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="部件名称" prop="parentPart">
          <el-input v-model="form.parentPart" placeholder="请输入部件名称" />
        </el-form-item>
        <el-form-item label="部件图号" prop="parentDrawingNo">
          <el-input v-model="form.parentDrawingNo" placeholder="请输入部件图号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的物料数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!--          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                             @click="importTemplate">下载模板
                    </el-link>-->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order";
import {getToken} from "../../../utils/auth";

export default {
  name: "Order",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //抽屉
      drawer: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        productionOrderNo: undefined,
        drawingNo: undefined,
        drawingName: undefined,
        quantity: undefined,
        material: undefined,
        singleWeight: undefined,
        totalWeight: undefined,
        parentPart: undefined,
        parentDrawingNo: undefined,
      },
      // 表单参数
      form: {},
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入物料BOM",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/order/importData"
      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        productionOrderNo: [
          { required: true, message: "生产令号不能为空", trigger: "blur" }
        ],
        drawingNo: [
          { required: true, message: "生产图号不能为空", trigger: "blur" }
        ],
        drawingName: [
          { required: true, message: "生产名称不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "生产数量不能为空", trigger: "blur" }
        ],
        material: [
          { required: true, message: "生产材料不能为空", trigger: "blur" }
        ],
        singleWeight: [
          { required: true, message: "单重不能为空", trigger: "blur" }
        ],
        totalWeight: [
          { required: true, message: "总重不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        parentPart: [
          { required: true, message: "部件名称不能为空", trigger: "blur" }
        ],
        parentDrawingNo: [
          { required: true, message: "部件图号不能为空", trigger: "blur" }
        ],
      },
      spanArr:[],
      tableHeader:['productionOrderNo','drawingNo','drawingName','quantity','material','singleWeight','totalWeight','remark','parentPart','parentDrawingNo']
    };

  },
  created() {
    this.getList();
//this.queryParams.productionOrderNo = ''

  },
  mounted() {


  },
  methods: {

    getSpanArr() {

      const hLen = this.tableHeader.length;
c
      // i表示行，j表示列
      for (let i = 0; i < this.orderList.length; i++) {
        if (i === 0) {
          for (let j = 0; j < hLen; j++) {
            this.spanArr[i * hLen + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          // 当前和上一次的一样，合并所有列的相同数据单元格
          for (let j = 0; j < hLen; j++) {
            // 指定合并哪些列
            if (this.tableHeader[j] === "parentPart") {


              // 哪些不合并，country不一样的，不合并
              if (
                this.orderList[i]["parentPart"] !==
                this.orderList[i - 1]["parentPart"]
              ) {
                this.spanArr[i * hLen + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              } else if (
                this.orderList[i][this.tableHeader[j]] ===
                this.orderList[i - 1][this.tableHeader[j]]
              ) {

                let beforeItem = this.spanArr[(i - 1) * hLen + j];

                this.spanArr[i * hLen + j ] = {
                  rowspan: 1 + beforeItem.rowspan, // 合并几列
                  colspan: 1, // 合并几行
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {

                this.spanArr[i * hLen + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }
          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < hLen; i++) {
        for (let j = 0; j < this.orderList.length; j++) {
          const spanItem = this.spanArr[j * hLen + i];
          if (spanItem) {
            if (spanItem.rowspan === 0) {
              stack.push(spanItem);
            }
            if (j !== 0 && spanItem.rowspan !== 0) {
              stack.push(spanItem);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * hLen + i] = pop;
              }
            }
          }
        }
      }
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(this.spanArr[rowIndex * this.colFields.length + columnIndex]);
      return this.spanArr[rowIndex * this.tableHeader.length + columnIndex]
    },
    openProductionInfo(item) {
      // 是否打开,设置为打开
      this.drawer = true
      this.designProgress = 0
      this.productionProgress = 0
      this.fileList = []
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "物料BOM导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/order/importTemplate', {}, `order_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>"
        + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询生产订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        //this.getSpanArr();
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productionOrderNo: undefined,
        drawingNo: undefined,
        drawingName: undefined,
        quantity: undefined,
        material: undefined,
        singleWeight: undefined,
        totalWeight: undefined,
        remark: undefined,
        parentPart: undefined,
        parentDrawingNo: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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.loading = true;
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改生产订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产订单编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delOrder(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>

</style>
