<template>
  <div>
    <div style="width: 100%;overflow:auto">
      <div style="white-space: nowrap">
        <el-card style="margin-left: 0px;width:740px; display: inline-block;">
          <el-col>
            <el-input v-model="partDrawingNumber" placeholder="机床号或零件图号或零件名称" size="small" clearable style="margin: 3px 0;width: 200px" @keyup.enter.native="searchTable1"/>
            <el-tooltip :open-delay="1200" effect="dark" content="查询备料零件" placement="top">
              <el-button type="primary" size="small" icon="el-icon-search" plain style="margin-left: 10px" @click="pageCurrent1=1;pageSize1=10;total1=0;searchTable1()">查询</el-button>
            </el-tooltip>
            <el-tooltip :open-delay="1200" effect="dark" content="将零件打回重新分配备料计划" placement="top">
              <el-button v-positive="'loading'" :disabled="false" type="danger" size="small" icon="el-icon-download" plain style="margin-left: 10px;float: right;" @click="back">打回重新分配</el-button>
            </el-tooltip>
            <el-table
              v-loading="loading1"
              :data="tableData1"
              :header-cell-style="{fontFamily:'YouYuan',fontSize:'14px',fontWeight:200}"
              border
              stripe
              highlight-current-row
              style="width:720px ;margin-top:3px;"
              height="690"
              size="mini"
              @sort-change="sortChange"
              @selection-change="handleSelectionChange">
              <el-table-column type="selection" align="center" width="45"/>
              <el-table-column label="机床图号" prop="机床图号" width="120" sortable="custom" align="center">
                <template slot-scope="scope">
                  {{ scope.row.机床图号 }}
                </template>
              </el-table-column>
              <el-table-column label="组号" prop="组号" width="70" align="center">
                <template slot-scope="scope">
                  {{ scope.row.组号 }}
                </template>
              </el-table-column>
              <el-table-column label="零件图号" prop="零件图号" min-width="120" width="160" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.零件图号 }}
                </template>
              </el-table-column>
              <el-table-column label="零件名称" prop="零件名称" width="120" sortable="custom" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.零件名称 }}
                </template>
              </el-table-column>
              <el-table-column label="数量" width="70" align="center">
                <template slot-scope="scope">
                  {{ scope.row.批次数量 }}
                </template>
              </el-table-column>
              <el-table-column label="材料" prop="材料" width="120" sortable="custom" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.材料 }}
                </template>
              </el-table-column>
              <el-table-column label="分配时间" prop="备料任务分配时间" width="120" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.备料任务分配时间 }}
                </template>
              </el-table-column>
              <el-table-column label="分配人" prop="分配人姓名" width="90" align="center" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.分配人姓名 }}
                </template>
              </el-table-column>
            </el-table>
            <div v-show="false" class="block">
              <el-pagination
                :current-page="pageCurrent1"
                :page-size="pageSize1"
                :total="total1"
                style="display:inline-block;width:50%"
                layout="total, prev, pager, next"
                @size-change="handleSizeChange1"
                @current-change="handleCurrentChange1"/>
            </div>
          </el-col>
        </el-card>
        <el-card style="margin-left: 1px;width:1120px; display: inline-block;">
          <div> <!--slot="header"-->
            <!--<el-input v-model="purchasingOrder" placeholder="采购计划单号" size="small" style="margin: 3px 0;width: 155px" clearable @keyup.enter.native="pageCurrent2=1;pageSize2=10;total2=0;searchTable2()"/>-->
            <el-button class="button111" size="small" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="addPurchaseOrder">外购备料</el-button>
            <el-tooltip :open-delay="1200" effect="dark" content="向选择的采购计划里添加采购零件" placement="top">
              <el-button v-positive="'loading'" :disabled="Number(采购计划状态)===3 || Number(采购计划状态)===4" type="primary" plain size="small" icon="el-icon-download" style="margin-left: 10px" @click="addMateriel">选入</el-button>
            </el-tooltip>
            <el-tooltip :open-delay="1200" effect="dark" content="保存选中的采购计划单" placement="top">
              <el-button v-positive="'loading'" :disabled="Number(采购计划状态)===3 || Number(采购计划状态)===4" type="success" size="small" icon="el-icon-tickets" plain style="margin-top: 3px;margin-left: 10px" @click="saveMateriel">保存</el-button>
            </el-tooltip>
            <el-select v-model="supplierName0" placeholder="供应商" style="width: 220px;margin-left: 10px" size="small" filterable clearable @change="pageCurrent2=1;pageSize2=10;total2=0;searchTable2()">
              <el-option
                v-for="item in supplierName0s"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
            <el-date-picker
              v-model="dateRange"
              :picker-options="pickerOptions"
              size="small"
              type="daterange"
              align="center"
              style="width: 220px;margin: 3px 10px"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              range-separator="～"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
            <!--<el-input v-model="supplierName0" placeholder="供应商" size="small" clearable/>-->
            <!--<el-tooltip :open-delay="1200" effect="dark" content="查询采购计划单" placement="top">-->
            <!--<el-button type="success" size="small" icon="el-icon-search" style="margin-left: 10px" @click="pageCurrent2=1;pageSize2=10;total2=0;searchTable2()">查询</el-button>-->
            <!--</el-tooltip>-->
            <el-button type="primary" size="small" icon="el-icon-search" plain style="margin-left: 10px" @click="searchTable2">查询</el-button>
          </div>
          <el-table
            v-loading=""
            :data="tableData2"
            :header-cell-style="{fontFamily:'YouYuan',fontSize:'14px',fontWeight:200}"
            border
            stripe
            style="max-height: 460px;width: 1120px"
            height="230px"
            size="mini"
            highlight-current-row
            @row-click="searchTable3">
            <el-table-column label="进程" prop="采购计划状态" align="center" width="80" fixed="left">
              <template slot-scope="scope">
                <span v-if="Number(scope.row.采购计划状态)===0" type="warning" size="mini">编辑中</span>
                <span v-if="Number(scope.row.采购计划状态)===1" type="primary" size="mini">已保存</span>
                <span v-if="Number(scope.row.采购计划状态)===3" type="success" size="mini">已到货</span>
                <span v-if="Number(scope.row.采购计划状态)===4" type="info" size="mini">已结算</span>
              </template>
            </el-table-column>
            <el-table-column label="外购备料单号" prop="采购单号" align="center" width="180">
              <template slot-scope="scope">
                {{ scope.row.采购单号 }}
              </template>
            </el-table-column>
            <el-table-column label="外购备料单名称" prop="采购单名称" align="center" width="180">
              <template slot-scope="scope">
                {{ scope.row.采购单名称 }}
              </template>
            </el-table-column>
            <!--<el-table-column label="供应商" prop="供应商名称" align="center" min-width="200" width="200" show-overflow-tooltip>-->
            <el-table-column label="供应商" prop="供应商名称" align="center" min-width="200" width="250" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.供应商名称 }}
              </template>
            </el-table-column>
            <el-table-column label="采购员" prop="姓名" align="center" width="110" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.姓名 }}
              </template>
            </el-table-column>
            <el-table-column label="计划完成" align="center" width="120">
              <template slot-scope="scope">
                {{ scope.row.计划完成日期.split(' ')[0] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="170px">
              <template slot-scope="scope">
                <el-tooltip :open-delay="1200" effect="dark" content="导出" placement="top">
                  <div style="display: inline-block">
                    <el-button size="mini" style="margin-left: 10px" type="text" icon="el-icon-download" @click="exportExcel2(scope.row)">导出</el-button>
                  </div>
                </el-tooltip>
                <el-tooltip :open-delay="1200" effect="text" content="打印" placement="top">
                  <div style="display: inline-block">
                    <el-button size="mini" type="text" style="margin-left: 10px" icon="el-icon-printer" @click="exportExcel(scope.row)"/>
                  </div>
                </el-tooltip>
                <el-tooltip :open-delay="1200" effect="dark" content="编辑" placement="top">
                  <div style="display: inline-block">
                    <el-button :disabled="parseInt(scope.row.采购计划状态)===3 || parseInt(scope.row.采购计划状态)===4" type="text" style="margin-left: 10px" size="mini" icon="el-icon-edit-outline" @click="editPurchaseOrder(scope.row)"/>
                  </div>
                </el-tooltip>
                <el-tooltip :open-delay="1200" effect="dark" content="删除" placement="top">
                  <div style="display: inline-block">
                    <el-button :disabled="parseInt(scope.row.采购计划状态)===3 || parseInt(scope.row.采购计划状态)===4" type="text" style="margin-left: 10px" size="mini" icon="el-icon-delete" @click="deletePurchaseOrder(scope.row)"/>
                  </div>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
          <!--<div class="block">-->
          <!--<el-pagination-->
          <!--:current-page="pageCurrent2"-->
          <!--:page-sizes="[10, 20, 30, 40]"-->
          <!--:page-size="pageSize2"-->
          <!--:total="total2"-->
          <!--style="display:inline-block;width:40%;float: right;margin-right: 60px"-->
          <!--layout="total, sizes, prev, pager, next, jumper"-->
          <!--@size-change="handleSizeChange2"-->
          <!--@current-change="handleCurrentChange2"/>-->
          <!--</div>-->
          <div class="block">
            <el-pagination
              v-show="false"
              :current-page="pageCurrent2"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize2"
              :total="total2"
              style="display:inline-block;width:40%;float: right;margin-right: 60px"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange2"
              @current-change="handleCurrentChange2"/>
          </div>
          <el-table v-loading="" :data="tableData3" :header-cell-style="{fontFamily:'YouYuan',fontSize:'14px',fontWeight:200}" border highlight-current-row style="margin: 3px 0;width: 1100px" height="455px" width="1100px" size="mini">
            <el-table-column label="进程" align="center" width="60">
              <template slot-scope="scope">
                <span v-if="Number(scope.row.是否到货)===0" type="warning" size="mini">未到货</span>
                <span v-if="Number(scope.row.是否到货)===1" type="success" size="mini">已到货</span>
                <span v-if="Number(scope.row.是否到货)===2" type="info" size="mini">已结算</span>
              </template>
            </el-table-column>
            <el-table-column label="物料编码" prop="物料零件编码" align="center" width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.物料零件编码 }}
              </template>
            </el-table-column>
            <el-table-column label="零件名称" align="center" width="120">
              <template slot-scope="scope">
                {{ scope.row.零件名称 }}
              </template>
            </el-table-column>
            <el-table-column label="材料" prop="材料" align="center" width="140">
              <template slot-scope="scope">
                {{ scope.row.材料 }}
              </template>
            </el-table-column>
            <el-table-column label="数量" align="center" width="135">
              <template slot-scope="scope">
                <el-input-number :disabled="parseInt(scope.row.是否到货)===1 || parseInt(scope.row.是否到货)===2" v-model="scope.row.采购数量" :min="0" size="mini" style="width: 100%" @change="saveWeight(scope.$index, scope.row)"/>
              </template>
            </el-table-column>
            <el-table-column label="成品尺寸（长、宽、高）" align="center" width="180px">
              <template slot-scope="scope">
                <el-input :disabled="parseInt(scope.row.是否到货)===1 || parseInt(scope.row.是否到货)===2" v-model="scope.row.成品尺寸" size="mini" style="width:100%" @change="saveWeight(scope.$index, scope.row)"/>
              </template>
            </el-table-column>
            <el-table-column label="单位" align="center" width="80px">
              <template slot-scope="scope">
                <el-input :disabled="parseInt(scope.row.是否到货)===1 || parseInt(scope.row.是否到货)===2" v-model="scope.row.单位" size="mini" style="width:100%" @change="saveMoney(scope.$index, scope.row)"/>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="备注" width="130px">
              <template slot-scope="scope">
                <el-input :disabled="parseInt(scope.row.是否到货)===1 || parseInt(scope.row.是否到货)===2" v-model="scope.row.备注1" size="mini" style="width:100%" @change="saveRemarks(scope.$index, scope.row)"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="90px" fixed="right">
              <template slot-scope="scope">
                <el-tooltip :open-delay="1000" effect="dark" content="移出" placement="top">
                  <div style="display: inline-block">
                    <el-button :disabled="parseInt(scope.row.是否到货)===1 || parseInt(scope.row.是否到货)===2" type="text" size="mini" icon="el-icon-right" @click="deleteMateriel(scope.row)">移出</el-button>
                  </div>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </div>
    </div>
    <div>
      <el-card style="width: 1100px;margin: 10px auto">
        <div id="WGBLD" style="width: 1000px;margin: 0 auto">
          <table cellspacing="0px" align="center" border="1 solid black" width="100%">
            <tr style="height: 60px">
              <th colspan="14"><h3>江苏高精机电装备有限公司</h3></th>
            </tr>
            <tr style="height: 40px">
              <th colspan="14"><h4>{{ 采购单名称 }}</h4></th>
            </tr>
            <tr style="height: 40px">
              <td colspan="2" style="text-align: center;width: 15%">下单单位</td>
              <td colspan="5" style="text-align: center;width: 35%">{{ 下单单位 }}</td>
              <td colspan="2" style="text-align: center;width: 15%">单号</td>
              <td colspan="5" style="text-align: center;width: 35%">{{ 单号 }}</td>
            </tr>
            <tr style="height: 40px">
              <td colspan="2" style="text-align: center;width: 15%">下单日期</td>
              <td colspan="5" style="text-align: center;width: 35%">{{ 下单日期 }}</td>
              <td colspan="2" style="text-align: center;width: 15%">计划完成日期</td>
              <td colspan="5" style="text-align: center;width: 35%">{{ 计划完成日期 }}</td>
            </tr>
          </table>
          <table cellspacing="0px" align="center" border="1 solid black" width="100%" style="margin-top: 0px">
            <tr id="tablehead" style="height: 35px">
              <td style="text-align: center;width: 7%">序号</td>
              <td colspan="2" style="text-align: center;width: 20%">物料编码</td>
              <td colspan="2" style="text-align: center;width: 10%">名称</td>
              <td colspan="2" style="text-align: center;width: 10%">材料</td>
              <td colspan="2" style="text-align: center;width: 8%">单位</td>
              <td colspan="2" style="text-align: center;width: 8%">数量</td>
              <td colspan="2" style="text-align: center;width: 20%">成品尺寸（长、宽、高）</td>
              <td style="text-align: center;width: 17%">备注</td>
            </tr>
            <tr v-for="(list, index) in tableData4" :key="index" style="height: 35px">
              <td style="text-align: center;width: 7%">{{ index + 1 }}</td>
              <td colspan="2" style="text-align: center;width: 20%">{{ list.物料零件编码 }}</td>
              <td colspan="2" style="text-align: center;width: 10%">{{ list.零件名称 }}</td>
              <td colspan="2" style="text-align: center;width: 10%">{{ list.材料 }}</td>
              <td colspan="2" style="text-align: center;width: 8%">{{ list.单位 }}</td>
              <td colspan="2" style="text-align: center;width: 8%">{{ list.总数量 }}</td>
              <td colspan="2" style="text-align: center;width: 20%">{{ list.成品尺寸 }}</td>
              <td style="text-align: center;width: 17%">{{ list.备注1 }}</td>
            </tr>
          </table>
          <div style="margin-top: 20px">联系人:___________ 联系电话:___________ </div>
        </div>
      </el-card>
    </div>

    <el-dialog v-el-drag-dialog :title="title2" :visible.sync="dialogVisible2" center style="min-height: 300px" width="600px" @close="closedialog()">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="right" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="外购备料单名称" prop="采购计划单名称">
              <el-input v-model="form2.采购单名称" size="small" style="width: 150px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购员" prop="采购员">
              <el-input v-model="name" readonly size="small" style="width: 150px;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplierNameValue">
              <el-select v-model="form2.supplierNameValue" placeholder="供应商" size="small" filterable style="width: 150px;">
                <el-option
                  v-for="item in supplierNames"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划完成日期" prop="计划完成日期">
              <el-date-picker
                v-model="form2.计划完成日期"
                size="small"
                type="date"
                style="width: 150px;"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-show="title2==='创建外购备料单'"
          :disabled="addPurchaseOrder_disable"
          type="primary"
          size="small"
          icon="el-icon-check"
          @click="submitAddPurchaseOrder">确定</el-button>
        <el-button
          v-show="title2==='编辑外购备料单'"
          :disabled="addPurchaseOrder_disable"
          type="primary"
          size="small"
          icon="el-icon-check"
          @click="submitEditPurchaseOrder">确定</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { initPartDrawing, searchPurchasedMaterials, searchPurchaseOrder, addPurchaseOrder, searchSupplier, editPurchaseOrder, deletePurchaseOrder, searchMateriel, searchMateriel1,
  addMateriel, deleteMateriel, saveMateriel, saveMateriel1, searchRawMaterial, addMateriel2, saveWeight, edit2 } from '@/api/WorkshopProcurement/CreatingPurchasingMaterials'
import QRCode from 'qrcode'
import $ from 'jquery'
import { mapGetters } from 'vuex'
export default {
  name: '', // 采购计划
  data() {
    return {
      addPurchaseOrder_disable: false,
      dateRange: '',
      checktime: '',
      state: '',
      pickerOptions: {
        shortcuts: [{
          text: '上季度',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 366 / 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      RawMaterialGroup: [],
      RawMaterialName: '',
      tableData: [],
      supplierNames: [],
      pageCurrent: 1,
      pageSize: 10,
      total: null,
      partName: '',
      order: '',
      partDrawingNumber: '',
      Buyer: '',
      loading1: false,
      title2: '',
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      tableData1: [],
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      purchasingOrder: '', // 采购单号
      purchasingOrderNumber: '', // 采购计划流水号
      supplierName0: '', // 供应商名称（外面的）
      supplierName0s: [], // 供应商名称（外面的）
      supplierName: '', // 供应商名称
      tableData2: [],
      total2: 0,
      pageCurrent2: 1,
      pageSize2: 10,
      dialogVisible1: false,
      form1: {},
      supplierCheck1: 0,
      supplierCheck2: 0,
      supplierCheck3: 0,
      supplierCheck4: 0,
      supplierCheck5: 0,
      supplierCheck6: 0,
      supplierCheck7: 0,
      supplierCheck8: 0,
      supplierCheck9: 0,
      supplierCheck10: 0,
      supplierCheck11: 0,
      supplierCheck12: 0,
      supplierValue: '', // 供应商流水号
      goodTime: '', // 货期
      enterpriseNature: '', // 企业性质
      createDate: '', // 创立日期
      registeredCapital: '', // 注册资本
      taxNum: '', // 税号
      EMail: '', // 电子邮箱
      address: '', // 地址
      account: '', // 账号
      openingBank: '', // 开户行
      phone: '', // 电话号码
      fax: '', // 传真
      tableData01: [],
      dialogVisible2: false,
      下单单位: '',
      下单日期: '',
      单号: '',
      计划完成日期: '',
      采购单名称: '',
      form2: {
        采购计划流水号: '',
        采购单名称: '',
        supplierNameValue: '',
        计划完成日期: ''
      },
      rules2: { // 采购表单验证规则
        采购单名称: [{ required: true, message: '请填写采购单名称' }],
        supplierNameValue: [{ required: true, message: '请选择供应商' }],
        计划完成日期: [{ required: true, message: '请选择计划完成日期' }]
      },
      tableData3: [],
      materielGroup: [], // 工艺计划流水号组
      partNum: [], // 默认采购数量
      partGroup: [], // 采购计划明细流水号组
      unit: [], // 单位组
      Remarks: [], // 备注组
      tableData4: [], // 合同模板
      采购计划状态: '',
      采购计划状态2: '',
      paramRow: '' // 参数row
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.searchRawMaterial()
    this.getCurrentTime()
    this.searchTable2()
    // this.searchTable1()
    // this.searchTable2()
  },
  methods: {
    getCurrentTime() {
      this.dateRange = [this.getTime1(70), this.getTime0(1)]
    },
    getTime0(num) {
      var date0 = new Date()
      var date = new Date(date0.getTime() + num * 24 * 60 * 60 * 1000)
      var month = this.zeroFill(date.getMonth() + 1)
      var day = this.zeroFill(date.getDate())
      var hour = this.zeroFill(date.getHours())
      var minute = this.zeroFill(date.getMinutes())
      var second = this.zeroFill(date.getSeconds())
      return date.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    },
    getTime1(num) {
      var date0 = new Date()
      var date = new Date(date0.getTime() - num * 24 * 60 * 60 * 1000)
      var month = this.zeroFill(date.getMonth() + 1)
      var day = this.zeroFill(date.getDate())
      var hour = this.zeroFill(date.getHours())
      var minute = this.zeroFill(date.getMinutes())
      var second = this.zeroFill(date.getSeconds())
      return date.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    },
    zeroFill(i) {
      if (i >= 0 && i <= 9) {
        return '0' + i
      } else {
        return i
      }
    },
    // 获取原材料
    searchRawMaterial() {
      searchSupplier(0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '').then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.supplierNames.push({
            label: response.data[i].供应商名称,
            value: response.data[i].供应商流水号
          })
          this.supplierName0s.push({
            label: response.data[i].供应商名称,
            value: response.data[i].供应商流水号
          })
        }
      })
      searchRawMaterial(this.RawMaterialName, this.pageCurrent, this.pageSize).then(response => {
        this.tableData = response.data.rows
        this.total = parseInt(response.data.total)
      })
    },
    addMateriel2() { // 将材料选入请购单
      if (!this.purchasingOrderNumber) {
        this.$message.warning('请选择采购单')
      } else if (this.RawMaterialGroup.length === 0) {
        this.$message.warning('请勾选原材料')
      } else {
        addMateriel2(this.purchasingOrderNumber, this.RawMaterialGroup).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('选入成功')
            this.searchTable1()
            this.searchRawMaterial()
            searchMateriel(this.purchasingOrderNumber).then(response => {
              this.tableData4 = response.data
            })
            searchMateriel1(this.purchasingOrderNumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('选入失败') }
        })
      }
    },
    handleSelectionChange2(val) {
      this.RawMaterialGroup = []
      for (let i = 0; i < val.length; i++) {
        this.RawMaterialGroup.push(val[i].原材料流水号)
      }
    },
    saveWeight(index, row) {
      saveWeight(row.采购计划明细流水号, row.成品尺寸, row.备注1, row.单位, row.采购数量)
    },
    saveMoney(index, row) {
      saveWeight(row.采购计划明细流水号, row.成品尺寸, row.备注1, row.单位, row.采购数量)
      // var reg = /^[0-9]*$/
      // if (!reg.test(row.单价)) {
      //   this.$message.error('请输入正确的单价')
      //   row.单价 = ''
      // } else {
      // }
    },
    saveRemarks(index, row) {
      saveWeight(row.采购计划明细流水号, row.成品尺寸, row.备注1, row.单位, row.采购数量)
    },
    // ///////////////////////////////////////////////////////////////////////////////////////0529新加↑
    useqrcode(contractNum) {
      const opts = {
        errorCorrectionLevel: 'H',
        type: 'image/jpeg',
        rendererOpts: {
          quality: 0.3
        }
      }
      QRCode.toDataURL(contractNum, opts, function(err, url) {
        if (err) throw err
        const img = document.getElementById('img')
        img.src = url
      })
    },
    sortChange(column, prop, order) {
      console.log(column + '-' + column.prop + '-' + column.order, 11111)
      if (column.prop === '零件名称') {
        this.partName = 1
      } else if (column.prop === '机床图号') { this.partName = 2 } else if (column.prop === '材料') {
        this.partName = 3
      } else {
        this.partName = ''
      }
      console.log(this.ordername, 9990999)
      if (column.order === 'ascending') {
        this.order = 1
      } else if (column.order === 'descending') { this.order = 2 } else {
        this.order = ''
      }
      this.searchTable1()
    },
    fetchData() {
      initPartDrawing(0, this.partDrawingNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.partDrawingNumber.push({
            label: response.data[i].零件图号,
            value: response.data[i].零件图号
          })
        }
      })
    },
    searchTable1() { // 查询外购备料
      this.loading1 = true
      this.partDrawingNumber ? this.check1 = 1 : this.check1 = 0
      // this.pageCurrent1, this.pageSize1,
      searchPurchasedMaterials(this.check1, this.partDrawingNumber, this.order, this.partName).then(response => {
        this.tableData1 = response.data
        this.total1 = response.data.total
        this.loading1 = false
      })
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    handleSelectionChange(val) {
      this.materielGroup = []
      this.partNum = []
      for (let i = 0; i < val.length; i++) {
        this.materielGroup.push(val[i].工艺计划流水号)
        this.partNum.push(val[i].批次数量)
      }
    },
    searchTable2() { // 查询采购单列表
      this.purchasingOrder ? this.check3 = 1 : this.check3 = 0
      this.supplierName0 ? this.check4 = 1 : this.check4 = 0
      this.dateRange ? this.checktime = 1 : (this.checktime = 0, this.dateRange = '')
      searchPurchaseOrder(this.check3, this.purchasingOrder, 0, 0, this.check4, this.supplierName0, this.checktime, this.dateRange[0], this.dateRange[1]).then(response => {
        this.tableData2 = response.data
        // this.total2 = response.data.total
      })
    },
    handleSizeChange2(val) { // 采购单列表分页
      this.pageSize2 = val
      this.searchTable2()
    },
    handleCurrentChange2(val) { // 采购单列表分页
      this.pageCurrent2 = val
      this.searchTable2()
    },
    addPurchaseOrder() { // 创建采购单
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.title2 = '创建外购备料单'
      this.form2.supplierNameValue = ''
      this.form2.供应商名称 = ''
      this.form2.计划完成日期 = ''
      this.addPurchaseOrder_disable = false
      this.dialogVisible2 = true
    },
    closedialog() {
      this.form2.采购计划流水号 = ''
      this.form2.采购单名称 = ''
      this.form2.supplierNameValue = ''
      this.form2.计划完成日期 = ''
      this.form2.供应商名称 = ''
      this.form2.计划完成日期 = ''
    },
    submitAddPurchaseOrder() { // 新建采购单
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          this.addPurchaseOrder_disable = true
          addPurchaseOrder(this.form2.采购单名称, this.form2.supplierNameValue, this.id, this.form2.计划完成日期).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('新建采购单成功')
              this.searchTable2()
              this.dialogVisible2 = false
            } else { this.$message.error('新建采购单失败') }
          })
        } else {
          return false
        }
      })
    },
    editPurchaseOrder(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.title2 = '编辑外购备料单'
      this.form2.采购计划流水号 = row.采购计划流水号
      this.form2.采购单名称 = row.采购单名称
      this.form2.supplierNameValue = row.供应商
      this.form2.计划完成日期 = row.计划完成日期
      this.addPurchaseOrder_disable = false
      this.dialogVisible2 = true
    },
    submitEditPurchaseOrder() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          this.addPurchaseOrder_disable = true
          editPurchaseOrder(this.form2.采购计划流水号, this.form2.采购单名称, this.form2.supplierNameValue, this.form2.计划完成日期).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('修改采购单成功')
              this.searchTable2()
              this.dialogVisible2 = false
            } else { this.$message.error('修改采购单失败') }
          })
        } else {
          return false
        }
      })
    },
    deletePurchaseOrder(row) { // 删除采购单
      this.$confirm('确定删除该备料计划?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePurchaseOrder(row.采购计划流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable2()
            searchMateriel(this.purchasingOrderNumber).then(response => {
              this.tableData4 = response.data
            })
            searchMateriel1(this.purchasingOrderNumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('数据占用，删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel2(row) {
      if (this.tableData4.length !== 0) {
        var param = []
        param[0] = ['采购计划流水号', row.采购计划流水号]
        param[1] = ['供应商', row.供应商名称]
        param[2] = ['采购单号', row.采购单号]
        var Data = this.CreateData('2001', '报表_备料管理_采购计划明细_查询数据', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    searchTable3(row) { // 查询采购计划明细
      this.purchasingOrderNumber = row.采购计划流水号
      this.采购单名称 = row.采购单名称
      this.采购计划状态 = row.采购计划状态
      this.下单单位 = row.供应商名称
      this.单号 = row.采购单号
      this.计划完成日期 = row.计划完成日期.split(' ')[0]
      this.下单日期 = row.下单日期.split(' ')[0]
      searchMateriel(this.purchasingOrderNumber).then(response => {
        this.tableData4 = response.data
      })
      searchMateriel1(this.purchasingOrderNumber).then(response => {
        this.tableData3 = response.data
      })
      return Promise.resolve(/* 这里是需要返回的数据*/)
    },
    addMateriel() { // 将材料选入请购单
      if (!this.purchasingOrderNumber) {
        this.$message.warning('请选择采购单')
      } else if (this.materielGroup.length === 0) {
        this.$message.warning('请勾选物料')
      } else {
        addMateriel(this.purchasingOrderNumber, this.materielGroup, this.partNum).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('选入成功')
            this.searchTable1()
            searchMateriel(this.purchasingOrderNumber).then(response => {
              this.tableData4 = response.data
            })
            searchMateriel1(this.purchasingOrderNumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('选入失败') }
        })
      }
    },
    back() {
      if (this.materielGroup.length === 0) {
        this.$message.warning('请勾选物料')
      } else {
        edit2(this.materielGroup).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('打回成功')
            this.searchTable1()
          } else { this.$message.error('打回失败') }
        })
      }
    },
    saveMateriel() { // 保存数据
      this.partGroup = []
      this.unit = []
      this.Remarks = []
      if (!this.purchasingOrderNumber) {
        this.$message.warning('请选择采购单')
      } else {
        for (let i = 0; i < this.tableData3.length; i++) {
          this.partGroup.push(this.tableData3[i].采购计划明细流水号)
          if (this.tableData3[i].单位 === '' || this.tableData3[i].单位 === null) {
            this.tableData3[i].单位 = '无'
          }
          this.unit.push(this.tableData3[i].单位)
          if (this.tableData3[i].备注1 === '' || this.tableData3[i].备注1 === null) {
            this.tableData3[i].备注1 = '无'
          }
          this.Remarks.push(this.tableData3[i].备注1)
        }
        saveMateriel(this.purchasingOrderNumber, this.partGroup, this.unit, this.Remarks).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('保存成功')
            this.searchTable2()
            searchMateriel(this.purchasingOrderNumber).then(response => {
              this.tableData4 = response.data
            })
            searchMateriel1(this.purchasingOrderNumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('保存失败') }
        })
      }
    },
    deleteMateriel(row) { // 移除物料
      this.$confirm('确定移除该物料?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMateriel(row.采购计划流水号, row.零件图号, row.材料, row.零件名称).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('移除成功')
            this.searchTable1()
            searchMateriel(this.purchasingOrderNumber).then(response => {
              this.tableData4 = response.data
            })
            searchMateriel1(this.purchasingOrderNumber).then(response => {
              this.tableData3 = response.data
            })
          } else { this.$message.error('移除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消移除'
        })
      })
    },
    exportExcel(row) {
      if (row.采购计划状态 === '0') {
        this.$confirm('该采购计划没有保存,是否确定保存并打印?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          saveMateriel1(row.采购计划流水号).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('保存成功并打印')
              this.searchTable2()
            } else {
              this.$message.success('保存失败')
            }
          })
          const htmlNode = $('#WGBLD').html()
          const body = $('body')
          body.children().hide()
          const printNode = $(htmlNode)
          body.append(printNode)
          window.print()
          body.removeAttr('style')
          body.children().not('script').show()
          body.children().not('#app').hide()
          printNode.remove()
        }).catch(res => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      } else {
        this.$confirm('确定打印备料计划?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const htmlNode = $('#WGBLD').html()
          const body = $('body')
          body.children().hide()
          const printNode = $(htmlNode)
          body.append(printNode)
          window.print()
          body.removeAttr('style')
          body.children().not('script').show()
          body.children().not('#app').hide()
          printNode.remove()
        }).catch(res => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      }
    }
  }
}
</script>

<style scoped>
  .offlineContract td{
    height: 40px;
    text-align: center;
  }
  /*.el-card{*/
  /*  margin-bottom: 15px;*/
  /*}*/
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    /*margin: 10px 0 10px 0px;*/
  }
  .el-tag{
    margin: 0
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 5px;
  }
  .el-form--label-left >>> .el-form-item__label {
    text-align: right;
  }
  >>>.el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 20px 30px 20px
  }
  >>>.el-dialog__header {
    padding: 20px 20px 0px;
  }
  >>>.el-dialog__footer {
    padding: 0px 20px 20px;
    /*text-align: right;*/
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  >>>.el-form-item {
    margin-bottom: 13px;
  }
</style>
<style lang="scss">
  .button111{
    background:rgb(253,246,236);
    border-color: #ff9759;
    color: #ff9759;
    &:hover{
      background: #ff9759;
      border-color: #ff9759;
      color: white;
    }
    &:focus{
      background: #ff9759;
      border-color: #ff9759;
      color: white;
    }
  }
</style>

