<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="采购单号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入采购单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in purchaseTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收货仓库" prop="warehouseName">
        <el-select v-model="queryParams.warehouseNeme" placeholder="请选择" clearable size="small">
          <el-option v-for="item in warehouseOptions" 
                    :label="item.warehouseName"
                    :value="item.warehouseName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="采购单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库状态" prop="stockinStatus">
        <el-select v-model="queryParams.stockinStatus" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in stockinStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small" style="width: 215px"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计到货时间" prop="expectedTime">
        <el-date-picker clearable size="small" style="width: 215px"
          v-model="queryParams.expectedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择预计到货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成日期" prop="finishTime">
        <el-date-picker clearable size="small" style="width: 215px"
          v-model="queryParams.finishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="采购员" prop="purchaseUserId">
        <el-input
          v-model="queryParams.purchaseUserId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 30px;">
        <el-button type="cyan" 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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oms:purchaseOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oms:purchaseOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oms:purchaseOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oms:purchaseOrder:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="purchaseOrderList" @selection-change="handleSelectionChange">
      <el-table-column label="序号"  width="55" type="index" align="center" />
      <el-table-column type="selection" width="55" align="center" />     
      <el-table-column label="id" prop="id" v-if="false"></el-table-column>
      <el-table-column label="采购单号" align="center" prop="code" />
      <el-table-column label="采购类型" align="center" prop="type" >
          <template slot-scope="scope">
              <el-tag prop="type" v-for="(item, index) in purchaseTypeOptions"    v-if="scope.row.type==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="供应商id" align="center" prop="supplierId" v-if="false" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="要求收货仓库id" align="center" prop="warehouseId" v-if="false"/>
      <el-table-column label="收货仓库" align="center" prop="warehouseName" />
      <el-table-column label="采购单状态" align="center" prop="status" >
          <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="stockinStatus" >
          <template slot-scope="scope">
              <el-tag prop="stockinStatus" v-for="(item, index) in stockinStatusOptions"    v-if="scope.row.stockinStatus==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计到货时间" align="center" prop="expectedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算方式" align="center" prop="paymentType" />
      <el-table-column label="采购数量" align="center" prop="planNum" />
      <el-table-column label="总金额" align="center" prop="amount" />
      <el-table-column label="入库数量" align="center" prop="alreadyNum" />
      <el-table-column label="未入库数量" align="center" prop="waitNum" />
      <el-table-column label="停止等待数量" align="center" prop="cancelNum" />
      <el-table-column label="是否含包装" width="100" align="center" prop="packagingFlag" >
          <template slot-scope="scope">
              <el-tag prop="packagingFlag" v-for="(item, index) in packagingFlagOptions"    v-if="scope.row.packagingFlag==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="是否包邮" width="100" align="center" prop="shippingFlag" >
          <template slot-scope="scope">
              <el-tag prop="shippingFlag" v-for="(item, index) in shippingFlagOptions"    v-if="scope.row.shippingFlag==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="物流id" align="center" prop="logisticsId" v-if="false"/>
      <el-table-column label="物流公司" align="center" prop="logisticsBn" />
      <el-table-column label="运单号" align="center" prop="logisticsNo" />
      <el-table-column label="运费" align="center" prop="logisticsAmount" />
      <el-table-column label="溢装比例" align="center" prop="overfillRate" >
        <template v-slot="scope">
          {{ scope.row.overfillRate }} %
        </template>
      </el-table-column>
      <el-table-column label="采购员ID" align="center" prop="purchaseUserId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购申请单id" align="center" prop="applyId" v-if="false"/>
      <el-table-column label="来源单号" align="center" prop="applyCode" />
      <el-table-column label="操作" align="center" fixed="right" 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="['oms:purchaseOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oms:purchaseOrder: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"
    />

    <!-- 添加或修改oms采购单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <div style="margin:10px 0 10px 10px">
        <span class="font-small" >采购单基本信息</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="采购单号" prop="code">
              <el-input v-model="form.code" placeholder="自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商" prop="supplierName">
              <el-select v-model="form.supplierName" @change="selectSupplier">
                <el-option v-for="item in supplierOptions" 
                    :label="item.name"
                    :value="item.name"/>            
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                v-for="dict in purchaseTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购员" prop="purchaseUserId">
              <el-input v-model="form.purchaseUserId" placeholder="请输入采购员ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="收货仓库" prop="warehouseName" >
              <el-select v-model="form.warehouseName"  @change="selectWarehouse">
                <el-option v-for="item in warehouseOptions" 
                    :label="item.warehouseName"
                    :value="item.warehouseName"/>            
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="结算方式" prop="paymentType">
              <el-input v-model="form.paymentType" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预计到货时间" prop="expectedTime">
              <el-date-picker clearable size="small" style="width: 185px"
                v-model="form.expectedTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择预计到货时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="溢装比例" prop="overfillRate">
              <el-input v-model="form.overfillRate" placeholder="请输入" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="发票类型" prop="invoiceType">
              <el-select v-model="form.invoiceType" placeholder="请选择">
                <el-option
                  v-for="dict in invoiceTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否含包装" prop="packagingFlag">
              <el-radio-group v-model="form.packagingFlag">
                <el-radio :label="item.dictValue" v-for="item in packagingFlagOptions" >{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否包邮" prop="shippingFlag">
              <el-radio-group v-model="form.shippingFlag">
                <el-radio :label="item.dictValue" v-for="item in shippingFlagOptions" >{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="6">            
            <el-form-item label="运费" prop="logisticsAmount">
              <el-input v-model="form.logisticsAmount" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="物流公司" prop="name" >
              <el-select v-model="form.logisticsBn">
                <el-option v-for="item in expressOptions" 
                    :label="item.name"
                    :value="item.name"/>            
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物流单号" prop="logisticsNo">
              <el-input v-model="form.logisticsNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="收货人" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="收货手机" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        
            <el-form-item  label-width="100px" label="所属省市区" prop="cityIds">
              <el-cascader
              ref="citySelect"
              v-model="cityIds"
              :options="citys"
              @change="handleCityChange"></el-cascader>
            </el-form-item>
          
          
            <el-form-item  prop="address">
              <el-input v-model="form.address" placeholder="请输入详情地址" />
            </el-form-item>
        
        
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入" />
            </el-form-item>
          
        <el-form-item label="物流id" prop="logisticsId" v-if="false">
          <el-input v-model="form.logisticsId" placeholder="请输入物流id" />
        </el-form-item>
        <el-form-item label="采购申请单id" prop="applyId" v-if="false">
          <el-input v-model="form.applyId" placeholder="请输入采购申请单id" />
        </el-form-item>
        <el-form-item label="供应商id" prop="supplierId" v-if="false">
          <el-input v-model="form.supplierId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="要求收货仓库id" prop="warehouseId" v-if="false">
          <el-input v-model="form.warehouseId" placeholder="请输入要求收货仓库id" />
        </el-form-item>
        
        <el-form-item label="采购商品信息" prop="products" v-if="false">
          <el-input v-model="form.products" placeholder="请输入采购商品信息" />
        </el-form-item>
        <el-form-item label="总金额" prop="amount" v-if="false">
          <el-input v-model="form.amount" placeholder="请输入总金额" />
        </el-form-item>
        
        <el-form-item label="已下采购数量/入库数量" prop="alreadyNum" v-if="false">
          <el-input v-model="form.alreadyNum" placeholder="请输入已下采购数量/入库数量" />
        </el-form-item>
        <el-form-item label="未下采购数量/未入库数量" prop="waitNum" v-if="false">
          <el-input v-model="form.waitNum" placeholder="请输入未下采购数量/未入库数量" />
        </el-form-item>
        <el-form-item label="采购数量" prop="planNum" v-if="false">
          <el-input v-model="form.planNum" placeholder="请输入采购数量" />
        </el-form-item>
      </el-form>
      <div style="margin:10px 0 10px 10px">
        <span class="font-small" >商品信息</span>
        <el-button style="margin-left: 20px;" type="primary" plain @click="showDialog">添加商品</el-button>
      </div>  
      <el-table border :data="selectProducts" v-model="form.products">
        <el-table-column label="序号"  width="55" type="index" align="center" /> 
        <el-table-column label="商品图片" align="center" width="160">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.image" width="100">
            <img :src="scope.row.image" style="max-width: 150px;">
            </el-popover>
          </template>
        </el-table-column>       
        <el-table-column label="商品编码"  align="center" prop="spuCode" />
        <el-table-column label="商品名称"  align="center" prop="spuName" />
        <el-table-column label="SKU编码"  align="center" prop="skuCode" />
        <el-table-column label="SKU名称" align="center" prop="skuName" />
        <el-table-column label="下单数量" align="center">
          <template slot-scope="scope">
            <el-input v-model.number="scope.row.planNum" type="number"></el-input>
          </template>          
        </el-table-column>
        <el-table-column label="含税下单价"  align="center" prop="taxunitPrice" />
        <el-table-column label="税率"  align="center" prop="taxRate" >
          <template v-slot="scope">
            {{ scope.row.taxRate }} %
          </template>
        </el-table-column>
        <el-table-column label="含税金额"  align="center" prop="taxPrice" />
        <el-table-column label="供应商商品编码"  align="center" prop="supplierSpucode" />
        <el-table-column label="供应商SKU编码"  align="center" prop="supplierSkucode" />
        <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="danger" icon="el-icon-delete" @click="removeSelected(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible"  title="选择商品" width="1200px" @selection-change="handleSelectionChange">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuerys"
        />
      </el-form-item>
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuerys"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuerys"
        />
      </el-form-item>
       <el-form-item label="商品编号" prop="spuCode">
        <el-input
          v-model="queryParams.spuCode"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuerys"
        />
      </el-form-item>
      <el-form-item label="SKU编号" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder="请输入SKU编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuerys"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuerys">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuerys">重置</el-button>
      </el-form-item>
    </el-form>
      <el-table border ref="dataList" v-loading="loading" :data="productOptions">
        <el-table-column label="序号"  width="55" type="index" align="center" />
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="供应商编码"  align="center" prop="supplierCode" />
        <el-table-column label="商品图片" align="center" width="160" v-if="false">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.image" width="100">
            <img :src="scope.row.image" style="max-width: 150px;">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称"  align="center" prop="supplierName" />
        <el-table-column label="商品编码"  align="center" prop="spuCode" />
        <el-table-column label="商品名称"  align="center" prop="spuName" />
        <el-table-column label="SKU编码"  align="center" prop="skuCode" />
        <el-table-column label="SKU名称" align="center" prop="skuName" />
        <el-table-column label="供应商商品编码"  align="center" prop="supplierSpucode" />
        <el-table-column label="含税价"  align="center" prop="taxunitPrice" />
        <el-table-column label="税率"  align="center" prop="taxRate" >
        <template v-slot="scope">
          {{ scope.row.taxRate }} %
        </template>
      </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="productAdd">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listPurchaseOrder, getPurchaseOrder, delPurchaseOrder, addPurchaseOrder, updatePurchaseOrder, exportPurchaseOrder } from "@/api/oms/purchaseOrder";
import {listWarehouseName} from "@/api/oms/warehouse";
import { listExpress } from "@/api/store/express";
import {listSupplierByStatus} from "@/api/oms/supplier";
import {listProduct} from "@/api/oms/apply";
import {getCitys} from "@/api/store/city";
export default {
  name: "PurchaseOrder",
  data() {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oms采购单表格数据
      purchaseOrderList: [],
      warehouseOptions:[],
      productOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发票类型 0无 1电子发票 2增值税普通发票 3增值税专用发票字典
      invoiceTypeOptions: [],
      purchaseTypeOptions:[],
      // 是否含包装 0否  1是字典
      packagingFlagOptions: [],
      // 是否包邮 0否  1是字典
      shippingFlagOptions: [],
      // 状态 0待审核  1已审核待入库 2已完成 3已取消 4待通知WMS 5通知WMS失败字典
      statusOptions: [],
      // 入库状态 0未入库  1部分入库 2整单入库 3超收入库字典
      stockinStatusOptions: [],
      supplierOptions:[],
      expressOptions:[],
      cityIds:[],
        citys:[],
      selectProducts:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        applyId: null,
        supplierId: null,
        warehouseId: null,
        expectedTime: null,
        type: null,
        purchaseUserId: null,
        overfillRate: null,
        products: null,
        amount: null,
        invoiceType: null,
        packagingFlag: null,
        shippingFlag: null,
        alreadyNum: null,
        waitNum: null,
        planNum: null,
        auditUserId: null,
        auditTime: null,
        finishTime: null,
        status: null,
        stockinStatus: null,
        cancelNum: null,
        logisticsId: null,
        paymentType: null,
        createUserId: null,
        updateUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("oms_invoice_type").then(response => {
      this.invoiceTypeOptions = response.data;
    });
    this.getDicts("oms_packaging_flag").then(response => {
      this.packagingFlagOptions = response.data;
    });
    this.getDicts("oms_shipping_flag").then(response => {
      this.shippingFlagOptions = response.data;
    });
    this.getDicts("oms_purchase_order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("oms_stockin_status").then(response => {
      this.stockinStatusOptions = response.data;
    });
    this.getDicts("oms_purchase_type").then(response => {
      this.purchaseTypeOptions = response.data;
    });
  },
  methods: {
    removeSelected(index) {
      this.selectProducts.splice(index, 1);
    },
    productAdd(){
     // 获取 dataList 中选中的数据
     const selectedRows = this.$refs.dataList.selection;
      // 遍历选中的数据，添加到 selectedList 列表
      selectedRows.forEach(row => {
        // 判断 selectedData 中是否已存在该行数据
        const index = this.selectProducts.findIndex(item => item.spuCode === row.spuCode && item.supplierCode === row.supplierCode)
        if (index === -1) {
          this.selectProducts.push(row)
        }
      })
      this.dialogVisible=false;
    },
    showDialog() {
      this.loading = true;     
      listProduct(this.queryParams).then(response =>{
        this.productOptions = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.dialogVisible = true;
    },
    handleCityChange(value) {
          console.log(value);
          var nodes=this.$refs.citySelect.getCheckedNodes();
          this.form.cityIds=nodes[0].pathLabels[0]+"-"+nodes[0].pathLabels[1]+"-"+nodes[0].pathLabels[2];
          this.form.cityIdsStr=value.toString();
          console.log(this.form.cityIdsStr);
    },
    selectWarehouse(){   
      console.log('warehouseOptions',this.warehouseOptions)
      const item = this.warehouseOptions.find(item1=> item1.warehouseName === this.form.warehouseName)
      console.log(item)
      this.form.warehouseId = item.warehouseId
    },
    selectSupplier(){   
      console.log('supplierOptions',this.warehouseOptions)
      const item = this.supplierOptions.find(item1=> item1.name === this.form.supplierName)
      console.log(item)
      this.form.supplierId = item.id
    },
    /** 查询oms采购单列表 */
    getList() {
      this.loading = true;
      listPurchaseOrder(this.queryParams).then(response => {
        this.purchaseOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 发票类型 0无 1电子发票 2增值税普通发票 3增值税专用发票字典翻译
    invoiceTypeFormat(row, column) {
      return this.selectDictLabel(this.invoiceTypeOptions, row.invoiceType);
    },
    // 是否含包装 0否  1是字典翻译
    packagingFlagFormat(row, column) {
      return this.selectDictLabel(this.packagingFlagOptions, row.packagingFlag);
    },
    // 是否包邮 0否  1是字典翻译
    shippingFlagFormat(row, column) {
      return this.selectDictLabel(this.shippingFlagOptions, row.shippingFlag);
    },
    // 状态 0待审核  1已审核待入库 2已完成 3已取消 4待通知WMS 5通知WMS失败字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 入库状态 0未入库  1部分入库 2整单入库 3超收入库字典翻译
    stockinStatusFormat(row, column) {
      return this.selectDictLabel(this.stockinStatusOptions, row.stockinStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.selectProducts = [];
      this.form = {
        id: null,
        code: null,
        applyId: null,
        supplierId: null,
        warehouseId: null,
        expectedTime: null,
        type: null,
        purchaseUserId: null,
        overfillRate: null,
        products: null,
        amount: null,
        invoiceType: null,
        packagingFlag: null,
        shippingFlag: null,
        alreadyNum: null,
        waitNum: null,
        planNum: null,
        auditUserId: null,
        auditTime: null,
        finishTime: null,
        status: null,
        stockinStatus: null,
        cancelNum: null,
        logisticsId: null,
        paymentType: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null,
        remark: null,
        supplierCode:null,
        supplierName:null,
        warehouseName:null,
        logisticsAmount:null,
        logisticsBn:null,
        logisticsNo:null,
        cityIds:null,
        userName:null,
        userPhone:null,
        cityIds: null,
        address: null
      };
      this.resetForm("form");
    },
    handleQuerys() {
      this.queryParams.pageNum = 1;
      listProduct(this.queryParams).then(response =>{
        this.productOptions = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    resetQuerys(){
      this.resetForm("queryForm");
      this.handleQuerys;
    },
    /** 搜索按钮操作 */
    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();
      listWarehouseName(this.queryParams).then(response =>{
        this.warehouseOptions = response.data;
      });
      listExpress(this.queryParams).then(response =>{
        this.expressOptions = response.rows;
      });
      listSupplierByStatus(this.queryParams).then(response =>{
        this.supplierOptions = response.data;
      });
      getCitys().then(res => {
            this.loading = false;
            this.citys=res.data;
        });
      this.open = true;
      this.title = "添加oms采购单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPurchaseOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改oms采购单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const data = this.selectProducts;
      const jsonData = JSON.stringify(data);
      this.form.products = jsonData;
      this.form.planNum = this.selectProducts.reduce((total, item) => total + item.planNum, 0);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePurchaseOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPurchaseOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除oms采购单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPurchaseOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有oms采购单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPurchaseOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>

<style>
.font-small{
  font-size: 20px;
  font-weight: bold;
}


</style>