<template>
  <div>
    <el-card>
      <el-select v-model="projectNameValue" clearable filterable placeholder="请选择项目名称" size="small" style="margin-left: 10px;width: 200px" @change="searchPackingList();searchPackingListDetails();searchInvoice()">
        <el-option
          v-for="item in projectName"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <!--<el-button type="success" icon="el-icon-search" size="small" style="margin-left: 10px" @click="searchPackingList();searchPackingListDetails();searchInvoice()">查询</el-button>-->
      <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" style="margin-left: 10px" @click="showAddInvoice();flag1 = 1">增加</el-button>
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>总装箱单号：</span></div>{{ packingListNumber }}
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>发货节点：</span></div>{{ consignmentNode }}
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>设备名称：</span></div>{{ deviceName }}
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>收货单位：</span></div>{{ receivingUnit }}
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>联系人：</span></div>{{ contacts }}
      <div style="margin-left: 15px;display: inline-block;color:gray"><span>联系电话：</span></div>{{ contactNumber }}
    </el-card>
    <el-card>
      <el-row>
        <el-col :span="16"><div class="grid-content bg-purple">
          <el-table
            v-loading="tableData2Loading"
            id="tb"
            :row-style="tableRowClassName1"
            :data="tableData5"
            highlight-current-row
            style="width: 100%"
            height="235"
            @row-click="searchCargoBox"
          >
            <el-table-column align="center" label="发货单号" width="150">
              <template slot-scope="scope">
                {{ scope.row.发货单号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="收货地址" width="230">
              <template slot-scope="scope">
                {{ scope.row.省份 + scope.row.市名 + scope.row.详细地址 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="收货人" width="80">
              <template slot-scope="scope">
                {{ scope.row.联系人 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="联系电话" width="150">
              <template slot-scope="scope">
                {{ scope.row.联系方式 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="车辆牌号" width="150">
              <template slot-scope="scope">
                {{ scope.row.车辆牌号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="发货日期" width="150">
              <template slot-scope="scope">
                {{ scope.row.发货日期 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="运输状态" width="80">
              <template slot-scope="scope">
                {{ scope.row.运输状态 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="追溯码" width="150">
              <template slot-scope="scope">
                <canvas :id="canvas[scope.$index]"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="300">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-edit"
                  @click.stop="editWithdrawing(scope.row);flag1 = 4">导出</el-button>
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click.stop="showEditInvoice(scope.row);flag1 = 2">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click.stop="delInvoice(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div></el-col>
        <el-col :span="8"><div class="grid-content bg-purple-light">
          <div style="width: 550px;height:236px; border: 2px solid rgb(228, 231, 237);margin-left: 5px">
            <!--<el-upload-->
            <!--style="margin-left: 10px"-->
            <!--action="https://jsonplaceholder.typicode.com/posts/"-->
            <!--list-type="picture-card"-->
            <!--:on-preview="handlePictureCardPreview"-->
            <!--:on-remove="handleRemove">-->
            <!--<i class="el-icon-plus"></i>-->
            <!--</el-upload>-->
            <!--<el-dialog :visible.sync="dialogVisible">-->
            <!--<img width="100%" :src="dialogImageUrl" alt="">-->
            <!--</el-dialog>-->
          </div>
        </div></el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-row>
        <el-col :span="11"><div class="grid-content bg-purple">
          <el-tabs v-model="activeName">
            <el-tab-pane label="主机" name="first">
              <el-table
                v-loading="tableData1Loading"
                :row-key="getRowKeys"
                :expand-row-keys="expands"
                :data="tableData1"
                highlight-current-row
                border
                style="width: 100%"
                height="500"
                @expand-change="selectMachine"
              >
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-table
                      :data="tableData4"
                      border
                      style="width: 600px; text-align: center"
                      height="300"
                      highlight-current-row
                      @row-click="choiceParts"
                    >
                      <el-table-column
                        align="center"
                        label="序号"
                        type="index"
                        width="100"/>
                      <el-table-column label="代号" align="center">
                        <template slot-scope="scope">
                          {{ scope.row.代号 }}
                        </template>
                      </el-table-column>
                      <el-table-column label="名称" align="center">
                        <template slot-scope="scope">
                          {{ scope.row.名称 }}
                        </template>
                      </el-table-column>
                      <el-table-column label="规格" align="center">
                        <template slot-scope="scope">
                          {{ scope.row.规格 }}
                        </template>
                      </el-table-column>
                      <el-table-column label="总数量" align="center">
                        <template slot-scope="scope">
                          {{ scope.row.数量 }}
                        </template>
                      </el-table-column>
                      <el-table-column label="未发数量" align="center">
                        <template slot-scope="scope">
                          <el-tag>{{ scope.row.未发数量 }}</el-tag>
                        </template>
                      </el-table-column>
                      <el-table-column label="备注" align="center">
                        <template slot-scope="scope">
                          {{ scope.row.备注 }}
                        </template>
                      </el-table-column>
                    </el-table>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="序号" type="index" width="50"/>
                <el-table-column align="center" label="代号">
                  <template slot-scope="scope">
                    {{ scope.row.代号 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="名称">
                  <template slot-scope="scope">
                    {{ scope.row.名称 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="数量">
                  <template slot-scope="scope">
                    {{ scope.row.数量 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="备注">
                  <template slot-scope="scope">
                    {{ scope.row.备注 }}
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="随机供应备件" name="second">
              <el-table
                v-loading="tableData2Loading"
                :data="tableData2"
                highlight-current-row
                border
                style="width: 100%"
                height="500"
                @row-click="choiceSpare"
              >
                <el-table-column align="center" label="序号" type="index" width="50"/>
                <el-table-column align="center" label="名称">
                  <template slot-scope="scope">
                    {{ scope.row.名称 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="规格">
                  <template slot-scope="scope">
                    {{ scope.row.规格 }}
                  </template>
                </el-table-column>
                <el-table-column label="总数量" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.数量 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="未发数量">
                  <template slot-scope="scope">
                    <el-tag>{{ scope.row.未发数量 }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="单位">
                  <template slot-scope="scope">
                    {{ scope.row.单位 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="备注">
                  <template slot-scope="scope">
                    {{ scope.row.备注 }}
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="随即技术文件" name="third">
              <el-table
                v-loading="tableData3Loading"
                :data="tableData3"
                highlight-current-row
                border
                style="width: 100%"
                height="500"
                @row-click="choiceFile"
              >
                <el-table-column align="center" label="序号" type="index" width="50"/>
                <el-table-column align="center" label="名称">
                  <template slot-scope="scope">
                    {{ scope.row.名称 }}
                  </template>
                </el-table-column>
                <el-table-column label="总数量" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.数量 }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="未发数量">
                  <template slot-scope="scope">
                    <el-tag>{{ scope.row.未发数量 }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="备注">
                  <template slot-scope="scope">
                    {{ scope.row.备注 }}
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </div></el-col>
        <el-col :span="2">
          <div class="grid-content bg-purple">
            <div style="height:554px">
              <div>
                <el-button :disabled="disabledmoveInto" round type="primary" size="small" style="margin-top: 200px;margin-left: 45px" @click="moveInto()">➡</el-button>
              </div>
              <div>
                <el-button round type="danger" size="small" style="margin-top: 50px;margin-left: 45px" @click="remove()">⬅</el-button>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="11"><div class="grid-content bg-purple-light">
          <span style="margin-left: 20px">
            货箱号：
          </span>
          <el-select v-model="cargoBoxValue" clearable filterable placeholder="请选择货箱号" size="small" style="margin-left: 2px;width: 200px" @change="searchCargoBoxDetailed" @clear="clear">
            <el-option
              v-for="item in cargoBox"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" style="margin-left: 10px" @click="addCargoBox()">增加</el-button>
          <el-button type="danger" icon="el-icon-circle-plus-outline" size="small" style="margin-left: 10px;margin-right: 50px" @click="delCargoBox()">删除</el-button>
          <el-popover
            placement="right"
            width="80"
            trigger="hover"
          >
            <canvas v-show="show" id="canvas"/>
            <el-button slot="reference">追溯码</el-button>
          </el-popover>
          <hr style="border:none;border-top:2px solid rgb(228, 231, 237)" >
          <el-table
            v-loading="tableData6Loading"
            :data="tableData6"
            border
            style="width: 100%;margin-top: 3px"
            height="500"
            highlight-current-row
            @row-click="choiceGoods"
          >
            <el-table-column align="center" label="序号" type="index" width="100"/>
            <el-table-column align="center" label="代号">
              <template slot-scope="scope">
                {{ scope.row.代号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="名称">
              <template slot-scope="scope">
                {{ scope.row.名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="规格">
              <template slot-scope="scope">
                {{ scope.row.规格 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="数量">
              <template slot-scope="scope">
                {{ scope.row.数量 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="备注">
              <template slot-scope="scope">
                {{ scope.row.备注 }}
              </template>
            </el-table-column>
          </el-table>
        </div></el-col>
      </el-row>
    </el-card>

    <!--发货单新增或编辑-->
    <el-dialog :title="title1" :visible.sync="dialogFormVisible1" width="40%" center>
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货地址" prop="收货地址" label-width="110px">
              <el-input v-model="form1.收货地址" clearable size="small" placeholder="请选择收货地址" style="width: 200px" readonly autosize @dblclick.native="dialogFormVisible2 = true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆牌号" prop="车辆牌号" label-width="110px">
              <el-input v-model="form1.车辆牌号" size="small" placeholder="请输入车辆牌号" style="width: 200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="联系人" label-width="110px">
              <el-input v-model="form1.联系人" size="small" placeholder="请输入联系人" style="width: 200px" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="联系方式" label-width="110px">
              <el-input v-model="form1.联系方式" size="small" placeholder="请输入联系电话" style="width: 200px" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发货日期" prop="发货日期" label-width="110px">
              <el-date-picker
                v-model="form1.发货日期"
                style="width: 200px"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="请选择发货日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运输状态" prop="entryNameValue" label-width="110px">
              <el-select v-model="form1.运输状态" clearable filterable placeholder="请选择运输状态" style="width: 200px" size="small">
                <el-option
                  v-for="item in Transport"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOffInvoice('form1')">取消</el-button>
        <el-button type="primary" @click="submitInvoice('form1')">确 定</el-button>
      </div>
    </el-dialog>

    <!--地址选择-->
    <el-dialog :visible.sync="dialogFormVisible2" title="地址选择" center width="50%">
      <el-card>
        <el-select v-model="provinceValue" placeholder="请选择省份" size="mini" clearable style="width: 120px" @change="provinceChange">
          <el-option
            v-for="item in province"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-select v-model="cityValue" placeholder="请选择市名" size="mini" clearable style="width: 120px">
          <el-option
            v-for="item in city"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-input v-model="detailedAddress" clearable size="mini" style="width:200px" placeholder="请输入详细地址"/>
        <el-input v-model="people" clearable size="mini" style="width:100px" placeholder="联系人"/>
        <el-input v-model="phoneNumber" clearable size="mini" style="width:150px" placeholder="请输入联系方式"/>
        <el-button type="primary" size="mini" style="margin-left: 15px" @click="submitAdress">新增</el-button>
        <el-button type="warning" size="mini" @click="editAdress">编辑</el-button>
      </el-card>
      <el-table :data="addressData" highlight-current-row height="300" show-overflow-tooltip style="height: 330px; margin-top: 20px" @row-click="handleAdressChange">
        <el-table-column label="省份" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.省份 }}
          </template>
        </el-table-column>
        <el-table-column label="市" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.市名 }}
          </template>
        </el-table-column>
        <el-table-column label="详细地址" align="center">
          <template slot-scope="scope">
            {{ scope.row.详细地址 }}
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.联系人 }}
          </template>
        </el-table-column>
        <el-table-column label="联系方式" align="center">
          <template slot-scope="scope">
            {{ scope.row.联系方式 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="deleteAdress(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageCurrent1"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize1"
        :total="total1"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange1"
        @current-change="handleCurrentChange1"/>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible2 = false">取消</el-button>
        <el-button size="small" type="primary" @click="dialogFormVisible2 = false">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible3" title="移入" width="20%" center>
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left">
        <el-form-item label="名称" prop="名称" label-width="110px">
          <el-input v-model="form2.名称" size="small" style="width: 200px" disabled/>
        </el-form-item>
        <el-form-item label="数量" prop="数量" label-width="110px">
          <el-input-number v-model="form2.数量" :min="0" :max="maxNum" :precision="2" :step="1" controls-position="right" size="small" style="width: 200px" placeholder="请输入本次付款金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3 = false">取消</el-button>
        <el-button type="primary" @click="submitMoveInto('form2')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible4" title="移出" width="20%" center>
      <el-form ref="form3" :model="form3" :rules="rules2" label-position="left">
        <el-form-item label="名称" prop="名称" label-width="110px">
          <el-input v-model="form3.名称" size="small" style="width: 200px" disabled/>
        </el-form-item>
        <el-form-item label="数量" prop="数量" label-width="110px">
          <el-input-number v-model="form3.数量" :min="0" :max="maxNum" :precision="2" :step="1" controls-position="right" size="small" style="width: 200px" placeholder="请输入本次付款金额" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible4 = false">取消</el-button>
        <el-button type="primary" @click="submitRremove('form3')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elInput from 'element-ui/packages/input'
import { submitRremove, searchCargoBoxDetailed, submitMoveInto, delCargoBox, addCargoBox, searchCargoBox, editInvoice, delInvoice, addInvoice, editAdress, addAdress, deleteAdress, citySearch, provinceSearch, addressSearch, searchParts, searchHost, searchSparePart, searchProjectName, searchPackingList, searchFile, searchInvoice } from '@/api//MarketingCenter/DeliveryManagement'
import QRCode from 'qrcode'
export default {
  components: {
    elInput
  },
  data() {
    return {
      canvas: [],
      show: false,
      num: '',
      a: 1, // 嵌套表格交换变量
      b: 1, // 嵌套表格交换变量
      flag1: '', // 发货单旗帜变量
      flag2: '', // 表格选择旗帜变量
      title1: '', // 发货单对话框标题
      projectNameValue: '', // 项目名称
      activeName: 'first', // 标签页索引
      packingListNumber: '', // 装箱单号
      deviceName: '', // 设备名称
      consignmentNode: '', // 发货节点
      receivingUnit: '', // 收货单位
      contacts: '', // 联系人
      contactNumber: '', // 联系方式
      provinceValue: '', // 省份
      adressNum: '', // 收货地址流水号
      invoiceNum: '', // 发货单编号
      detailedAddress: '',
      disabledmoveInto: false,
      people: '',
      phoneNumber: '',
      cityValue: '', // 市名
      pageCurrent1: 1,
      code: '',
      pageSize1: 10,
      total1: null,
      cargoBoxValue: '',
      DetailOfGoodsCode: '',
      maxNum: 10, // 移入的最大数量
      goodsCodeName: '', // 货物代号
      goodsName: '', // 货物名称
      goodsSpecifications: '', // 货物规格
      goodsNumber: '', // 货物数量
      goodsRemark: '', // 货物备注
      partsCode: '', // 部件编号
      sparePartCode: '', // 备件编号
      fileCode: '', // 文件编号
      File: [],
      Parts: [],
      Spare: [],
      cargoBox: [],
      addressData: [],
      tableData1: [], // 主机表格
      tableData2: [], // 备件表格
      tableData3: [], // 随机文件表格
      tableData4: [], // 机床部件表格
      tableData5: [], // 发货单表格
      tableData6: [], // 货箱明细表格
      projectName: [], // 项目名称下拉框数组
      city: [], // 市名下拉框数组
      province: [], // 省份下拉框数组
      expands: [], // 嵌套表格
      tableData2Loading: false, // 备件加载动画
      tableData3Loading: false, // 随机文件加载动画
      tableData1Loading: false, // 主机加载动画
      tableData4Loading: false, // 机床所属部件加载动画
      tableData5Loading: false, // 发货单加载动画
      tableData6Loading: false, // 货箱明细加载动画
      dialogFormVisible1: false, // 发货单对话框标题显示隐藏
      dialogFormVisible2: false, // 地址选择对话框显示隐藏
      dialogFormVisible3: false, // 填充货箱内货物对话框显示隐藏
      dialogFormVisible4: false, // 移除货箱内货物对话框显示隐藏
      QueryDetail: '',
      Transport: [
        {
          value: 0,
          label: '运输中'
        },
        {
          value: 1,
          label: '已签收'
        }
      ],
      form1: {
        收货地址: '',
        车辆牌号: '',
        发货日期: '',
        运输状态: 0,
        联系人: '',
        联系方式: ''
      },
      form2: {
        名称: '',
        数量: ''
      },
      form3: {
        名称: '',
        数量: ''
      },
      rules1: {
        收货地址: [{ required: true, message: '请选择收货地址', trigger: 'change' }],
        车辆牌号: [{ required: true, message: '请输入车辆牌号', trigger: 'blur' }],
        联系人: [{ required: true, message: '请选择联系人', trigger: 'blur' }],
        联系方式: [{ required: true, message: '请选择联系方式', trigger: 'blur' }],
        发货日期: [{ required: true, message: '请选择发货日期', trigger: 'change' }],
        运输状态: [{ required: true, message: '请选择运输状态' }]
      },
      rules2: {}
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化函数
    init() {
      this.getSelect(searchProjectName, ['项目名称', '装箱单编号'], 'projectName')
      this.getSelect(provinceSearch, ['省份', '省份流水号'], 'province')
      this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
    },
    // 查询总单据信息
    searchPackingList() {
      searchPackingList(this.projectNameValue).then(response => {
        this.packingListNumber = response.data[0].装箱单号
        this.deviceName = response.data[0].名称
        this.consignmentNode = response.data[0].发货节点
        this.receivingUnit = response.data[0].收货单位
        this.contacts = response.data[0].联系人
        this.contactNumber = response.data[0].联系方式
      })
    },
    // 查询总单据明细
    searchPackingListDetails() {
      // 备件查询
      this.tableData2Loading = true
      searchSparePart(this.projectNameValue).then(response => {
        this.tableData2Loading = false
        this.tableData2 = response.data
      })
      // 随机文件查询
      this.tableData3Loading = true
      searchFile(this.projectNameValue).then(response => {
        this.tableData3Loading = false
        this.tableData3 = response.data
      })
      this.tableData1Loading = true
      searchHost(this.projectNameValue).then(response => {
        this.tableData1Loading = false
        this.tableData1 = response.data
      })
    },
    getRowKeys(row) {
      return row.代号
    },
    // 查询机床所属部件
    selectMachine(row) {
      this.code = row.代号
      this.tableData4Loading = true
      this.tableData4 = []
      searchParts(this.code).then(response => {
        this.tableData4Loading = false
        this.tableData4 = response.data
      })
      this.id = row.代号
      if (this.a === 1) {
        this.expands.push(row.代号)
        this.b = row.代号
        this.a = 0
      } else {
        if (this.b === row.代号) {
          this.expands = []
          this.a = 1
        } else {
          this.expands = []
          this.expands.push(row.代号)
          this.a = 0
          this.b = row.代号
        }
      }
    },
    // 发货单查询
    searchInvoice() {
      this.tableData5Loading = true
      searchInvoice(this.projectNameValue).then(response => {
        const data = response.data
        this.tableData5 = data
        this.canvas = []
        for (let i = 0; i < data.length; i++) {
          this.canvas.push(data[i].发货单编号.toString())
          data[i].发货日期 = data[i].发货日期.substring(0, data[i].发货日期.length - 9)
          data[i].运输状态 === 0 ? data[i].运输状态 = '运输中' : data[i].运输状态 = '已签收'
          // QRCode.toCanvas(document.getElementById(this.canvas[i]), data[i].发货单编号.toString(), function(error) {
          //   if (error) console.error(error)
          //   console.log('success!')
          // })
        }
        this.tableData5Loading = false
        this.tableData5 = data
        return data
      }).then(res => {
        for (let i = 0; i < res.length; i++) {
          QRCode.toCanvas(document.getElementById(this.canvas[i]), 'http://192.168.1.132:8080/#/DeliveryManagement/index?id=' + res[i].发货单编号.toString(), function(error) {
            if (error) console.error(error)
            console.log('success!')
          })
        }
        this.tableData5 = res
      })
    },
    // 显示新增发货单对话框
    showAddInvoice() {
      if (this.projectNameValue === '') {
        this.$message.warning('请选择装箱单')
      } else {
        this.addForm('form1')
        this.dialogFormVisible1 = true
        this.title1 = '新增'
      }
    },
    // 提交发货单
    submitInvoice(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.flag1 === 1) {
            addInvoice(this.projectNameValue, this.form1.车辆牌号, this.form1.运输状态, this.adressNum, this.form1.发货日期).then(response => {
              this.dialogFormVisible1 = false
              if (response.data[0].result === '1') {
                this.$message.success('添加成功')
                this.searchInvoice()
                this.flag1 = ''
              } else {
                this.$message.error('添加失败')
              }
            })
          } else {
            editInvoice(this.form1.车辆牌号, this.form1.运输状态, this.adressNum, this.form1.发货日期, this.invoiceNum).then(response => {
              this.dialogFormVisible1 = false
              if (response.data[0].result === '1') {
                this.$message.success('信息更新成功')
                this.searchInvoice()
                this.flag1 = ''
              } else {
                this.$message.error('信息更新失败')
              }
            })
          }
        }
      })
    },
    // 显示编辑发货单对话框
    showEditInvoice(row) {
      console.log(row)
      this.dialogFormVisible1 = true
      this.form1.收货地址 = row.省份 + row.市名 + row.详细地址
      this.form1.车辆牌号 = row.车辆牌号
      this.form1.发货日期 = row.发货日期
      row.运输状态 === '运输中' ? this.form1.运输状态 = 0 : this.form1.运输状态 = 1
      this.form1.联系人 = row.联系人
      this.form1.联系方式 = row.联系方式
      this.adressNum = row.收货地址流水号
      this.invoiceNum = row.发货单编号
    },
    // 删除发货单
    delInvoice(row) {
      this.deleteRow(delInvoice, row.发货单编号, function() { this.searchInvoice() })
    },
    // 收货地址表格分页
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
    },
    // 收货地址表格分页
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
    },
    // 根据省分查市名
    provinceChange() {
      this.city = []
      this.cityValue = ''
      this.getSelect(citySearch, ['市名', '市流水号'], 'city', this.provinceValue)
    },
    // 删除地址
    deleteAdress(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAdress(row.收货地址流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 选中地址
    handleAdressChange(row) {
      this.people = row.联系人
      this.phoneNumber = row.联系方式
      this.detailedAddress = row.详细地址
      this.provinceValue = parseInt(row.省份流水号)
      this.getSelect(citySearch, ['市名', '市流水号'], 'city', this.provinceValue)
      this.cityValue = parseInt(row.市流水号)
      this.adressNum = row.收货地址流水号
      this.form1.收货地址 = row.省份 + row.市名 + row.详细地址
      this.form1.联系人 = row.联系人
      this.form1.联系方式 = row.联系方式
    },
    submitAdress() {
      if (this.people === '' || this.phoneNumber === '' || this.detailedAddress === '' || this.provinceValue === '' || this.cityValue === '') {
        this.$message.warning('请填写全部信息')
      } else {
        this.$confirm('此操作将增加新地址，是否继续', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          addAdress(this.provinceValue, this.cityValue, this.detailedAddress, this.people, this.phoneNumber).then(response => {
            if (response.data[0].result === '1') {
              this.provinceValue = ''
              this.cityValue = ''
              this.detailedAddress = ''
              this.people = ''
              this.phoneNumber = ''
              this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
              this.$message.success('增加成功')
            } else { this.$message.error('增加失败') }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消增加'
          })
        })
      }
    },
    editAdress() {
      if (this.people === '' || this.phoneNumber === '' || this.detailedAddress === '' || this.provinceValue === '' || this.cityValue === '' || this.adressNum === '') {
        this.$message.warning('请选择要修改的地址并填全信息')
      } else {
        this.$confirm('此操作将修改该地址，是否继续', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          editAdress(this.provinceValue, this.cityValue, this.detailedAddress, this.people, this.phoneNumber, this.adressNum).then(response => {
            if (response.data[0].result === '1') {
              this.provinceValue = ''
              this.cityValue = ''
              this.detailedAddress = ''
              this.people = ''
              this.phoneNumber = ''
              this.getTable(addressSearch, [this.pageCurrent1, this.pageSize1], ['addressData', 'total1'])
              this.$message.success('修改成功')
            } else { this.$message.error('修改失败') }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消增加'
          })
        })
      }
    },
    searchCargoBox(row) {
      this.invoiceNum = row.发货单编号
      this.getSelect(searchCargoBox, ['货箱号', '货箱号编号'], 'cargoBox', this.invoiceNum)
    },
    addCargoBox() {
      this.$confirm('此操作将新增货箱, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addCargoBox(this.invoiceNum).then(response => {
          if (response.data[0].result === '1') {
            this.$message.sucess('增加成功')
            this.getSelect(searchCargoBox, ['货箱号', '货箱号编号'], 'cargoBox', this.invoiceNum)
          } else {
            this.$message.error('增加失败')
          }
        })
      })
    },
    delCargoBox() {
      this.$confirm('此操作会永久删除改行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delCargoBox(this.cargoBoxValue).then(response => {
          if (response.data[0].result === '1') {
            this.$message.sucess('删除成功')
            this.getSelect(searchCargoBox, ['货箱号', '货箱号编号'], 'cargoBox', this.invoiceNum)
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    // 选择备件
    choiceSpare(row) {
      if (parseInt(row.未发数量) === 0) {
        this.disabledmoveInto = true
      } else {
        this.disabledmoveInto = false
      }
      this.maxNum = row.未发数量
      this.form2.名称 = row.名称
      this.form2.数量 = row.数量
      this.goodsCodeName = ''
      this.partsCode = ''
      this.sparePartCode = row.备件编号
      this.fileCode = ''
      if (row.名称 === null) { this.goodsName = '' } else { this.goodsName = row.名称 }
      if (row.规格 === null) { this.goodsSpecifications = '' } else { this.goodsSpecifications = row.规格 }
      if (row.数量 === null) { this.goodsNumber = '' } else { this.goodsNumber = row.数量 }
      if (row.备注 === null) { this.goodsRemark = '' } else { this.goodsRemark = row.备注 }
      console.log(this.Spare, this.goodsCodeName, this.goodsName, this.goodsSpecifications, this.goodsNumber, this.goodsRemark)
    },
    // 选择文件
    choiceFile(row) {
      if (parseInt(row.未发数量) === 0) {
        this.disabledmoveInto = true
      } else {
        this.disabledmoveInto = false
      }
      this.maxNum = row.未发数量
      this.form2.名称 = row.名称
      this.form2.数量 = row.数量
      this.goodsCodeName = ''
      this.partsCode = ''
      this.sparePartCode = ''
      this.fileCode = row.文件编号
      if (row.名称 === null) { this.goodsName = '' } else { this.goodsName = row.名称 }
      this.goodsSpecifications = ''
      if (row.数量 === null) { this.goodsNumber = '' } else { this.goodsNumber = row.数量 }
      if (row.备注 === null) { this.goodsRemark = '' } else { this.goodsRemark = row.备注 }
      console.log(this.Spare, this.goodsCodeName, this.goodsName, this.goodsSpecifications, this.goodsNumber, this.goodsRemark)
    },
    // 选择部件
    choiceParts(row) {
      if (parseInt(row.未发数量) === 0) {
        this.disabledmoveInto = true
      } else {
        this.disabledmoveInto = false
      }
      this.maxNum = row.未发数量
      this.form2.名称 = row.名称
      this.form2.数量 = row.数量
      this.partsCode = row.部件编号
      this.sparePartCode = ''
      this.fileCode = ''
      if (row.代号 === null) { this.goodsCodeName = '' } else { this.goodsCodeName = row.代号 }
      if (row.名称 === null) { this.goodsName = '' } else { this.goodsName = row.名称 }
      if (row.规格 === null) { this.goodsSpecifications = '' } else { this.goodsSpecifications = row.规格 }
      if (row.数量 === null) { this.goodsNumber = '' } else { this.goodsNumber = row.数量 }
      if (row.备注 === null) { this.goodsRemark = '' } else { this.goodsRemark = row.备注 }
      console.log(this.Spare, this.goodsCodeName, this.goodsName, this.goodsSpecifications, this.goodsNumber, this.goodsRemark)
    },
    choiceGoods(row) {
      this.maxNum = row.数量
      this.form3.名称 = row.名称
      this.form3.数量 = row.数量
      this.DetailOfGoodsCode = row.货箱明细编号
      this.sparePartCode = row.备件编号
      this.fileCode = row.文件编号
      this.partsCode = row.部件编号
    },
    moveInto() {
      if (this.form2.名称 !== '' && this.form2.数量 !== '' && this.cargoBoxValue !== '') {
        this.dialogFormVisible3 = true
      } else {
        this.$message.warning(`请选择货箱及货物`)
      }
    },
    submitMoveInto() {
      submitMoveInto(this.cargoBoxValue, this.goodsCodeName, this.goodsName, this.goodsSpecifications, this.form2.数量, this.goodsRemark, this.partsCode, this.sparePartCode, this.fileCode).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.form2.名称 = ''
          this.form2.数量 = ''
          this.dialogFormVisible3 = false
          this.searchCargoBoxDetailed()
          this.searchPackingListDetails()
          searchParts(this.code).then(response => {
            this.tableData4 = response.data
          })
        } else { this.$message.error('添加失败') }
      })
    },
    searchCargoBoxDetailed() {
      this.tableData6Loading = true
      searchCargoBoxDetailed(this.cargoBoxValue).then(response => {
        this.tableData6Loading = false
        this.tableData6 = response.data
      })
      this.useqrcode()
    },
    remove() {
      if (this.cargoBoxValue === '' || this.form3.名称 === '' || this.form3.数量 === '') {
        this.$message.warning('请选择货箱并选择货物')
      } else {
        this.dialogFormVisible4 = true
      }
    },
    submitRremove() {
      submitRremove(this.DetailOfGoodsCode, this.form3.数量, this.partsCode, this.sparePartCode, this.fileCode, this.cargoBoxValue).then(response => {
        this.dialogFormVisible4 = false
        this.form3.名称 = ''
        this.form3.数量 = ''
        if (response.data[0].result === '1') {
          this.searchCargoBoxDetailed()
          searchParts(this.code).then(response => {
            this.tableData4 = response.data
          })
          this.$message.sucess('移除成功')
        } else {
          this.$message.error('移除失败')
        }
      })
    },
    tableRowClassName1({ row }) {
      if (row.运输状态 === '已签收') {
        return 'background: #33cc00'
      } else {
        return ''
      }
    },
    useqrcode() {
      this.show = true
      console.log(this.cargoBoxValue)
      // 生成的二维码内容，可以添加变量
      this.QueryDetail = this.cargoBoxValue.toString()
      var canvas = document.getElementById('canvas')
      QRCode.toCanvas(canvas, this.QueryDetail, function(error) {
        if (error) console.error(error)
        console.log('success!')
      })
    },
    callOffInvoice() {
      this.dialogFormVisible1 = false
    },
    clear() {
      this.show = false
    }
    // clear() {
    //   this.cargoBox = []
    //   this.cargoBoxValue = ''
    // }
  }
}
</script>

<style scoped>
  canvas{
    width: 45px !important;
    height: auto!important;
  }
  #canvas{
    width: 120px !important;
    height: auto!important;
  }
</style>
