<template>
  <ContainerQuery>
    <div slot="left">
      <el-button
        size="small"
        type="primary"
        @click="$router.push('/stock/OrdersForShipment/PickingDetail')"
      >
        待拣货明细
      </el-button>
      <el-button size="small" type="primary">打印</el-button>
      <el-button size="small" type="primary">导出</el-button>
    </div>
    <div slot="more">
      <el-tabs
        v-model="top_time"
        style="position: absolute; top: 70px; left: 180px"
        @tab-click="pageChange(1)"
      >
        <el-tab-pane label="昨日" name="yesterday"></el-tab-pane>
        <el-tab-pane label="今日" name="today"></el-tab-pane>
        <el-tab-pane label="本周" name="beforweek"></el-tab-pane>
        <el-tab-pane label="本月" name="nowmonth"></el-tab-pane>
        <el-tab-pane label="上月" name="permonth"></el-tab-pane>
        <el-tab-pane label="近三月" name="preday"></el-tab-pane>
        <el-tab-pane label="本年" name="nowyear"></el-tab-pane>
      </el-tabs>
    </div>
    <div slot="more">
      <el-form inline size="small">
        <el-form-item>
          <el-input
            v-model="keyword"
            style="width: 220px"
            placeholder="单据编号"
            clearable
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button slot="append" @click="pageChange(1)">
              <i class="el-icon-search"></i>
            </el-button>
          </el-input>
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <SelectCustomer-->
        <!--            v-model="customer_name"-->
        <!--            placeholder="收货人"-->
        <!--            @clear="customerClear"-->
        <!--            @change="customerSel"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            v-model="keyword"-->
        <!--            style="width: 220px"-->
        <!--            placeholder="联系电话"-->
        <!--            clearable-->
        <!--            @keyup.enter.native="pageChange(1)"-->
        <!--            @clear="pageChange(1)"-->
        <!--          >-->
        <!--            <el-button slot="append" @click="pageChange(1)">-->
        <!--              <i class="el-icon-search"></i>-->
        <!--            </el-button>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            v-model="keyword"-->
        <!--            style="width: 220px"-->
        <!--            placeholder="提货地址"-->
        <!--            clearable-->
        <!--            @keyup.enter.native="pageChange(1)"-->
        <!--            @clear="pageChange(1)"-->
        <!--          >-->
        <!--            <el-button slot="append" @click="pageChange(1)">-->
        <!--              <i class="el-icon-search"></i>-->
        <!--            </el-button>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-date-picker
            v-model="time"
            clearable
            type="daterange"
            value-format="timestamp"
            range-separator="-"
            start-placeholder="下单开始日期"
            end-placeholder="下单结束日期"
            @change="orderDate"
          ></el-date-picker>
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <el-select-->
        <!--            v-model="Return"-->
        <!--            style="width: 150px"-->
        <!--            multiple-->
        <!--            placeholder="产生方式"-->
        <!--            @change="pageChange(1)"-->
        <!--          >-->
        <!--            <el-option-->
        <!--              v-for="item in Return_status"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            v-model="keyword"-->
        <!--            style="width: 220px"-->
        <!--            placeholder="配送线路"-->
        <!--            clearable-->
        <!--            @keyup.enter.native="pageChange(1)"-->
        <!--            @clear="pageChange(1)"-->
        <!--          >-->
        <!--            <el-button slot="append" @click="pageChange(1)">-->
        <!--              <i class="el-icon-search"></i>-->
        <!--            </el-button>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            v-model="warehouseName"-->
        <!--            placeholder="发货仓库"-->
        <!--            style="width: 150px"-->
        <!--          >-->
        <!--            <i-->
        <!--              slot="suffix"-->
        <!--              class="el-input__icon el-icon-search"-->
        <!--              @click="warehouse_show = true"-->
        <!--            ></i>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-select-->
        <!--            v-model="Return"-->
        <!--            style="width: 150px"-->
        <!--            multiple-->
        <!--            placeholder="配送方式"-->
        <!--            @change="pageChange(1)"-->
        <!--          >-->
        <!--            <el-option-->
        <!--              v-for="item in Return_status"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
      </el-form>
      <!--    选择仓库-->
      <WarehouseModel
        v-if="warehouse_show"
        :is-check="false"
        :is-show="warehouse_show"
        @confirm="selWarehouse"
        @cancel="warehouse_show = false"
      />
    </div>
    <vxe-table
      ref="xTable"
      :data="tableData"
      auto-resize
      @checkbox-all="checkboxChangeEvent"
      @checkbox-change="checkboxChangeEvent"
    >
      <vxe-table-column
        fixed="left"
        type="checkbox"
        width="60"
      ></vxe-table-column>
      <vxe-table-column
        fixed="left"
        field="no"
        title="单据编号"
        width="160"
      ></vxe-table-column>
      <vxe-table-column fixed="left" field="age" title="单据日期" width="150">
        <template #default="{ row }">
          {{ $_common.formatDate(row.createTime) }}
        </template>
      </vxe-table-column>
      <vxe-table-column
        show-overflow
        field="customerName"
        title="往来单位"
        width="160"
      ></vxe-table-column>
      <vxe-table-column field="outNum" title="已发货数量" width="100">
        <template #default="{ row }">
          {{ $_common.formatNub(row.outNum) }}
        </template>
      </vxe-table-column>
      <vxe-table-column
        field="sort"
        title="排序"
        width="100"
      ></vxe-table-column>
      <vxe-table-column field="orderNum" title="订单数量" width="100">
        <template #default="{ row }">
          {{ $_common.formatNub(row.orderNum) }}
        </template>
      </vxe-table-column>
      <vxe-table-column field="payAmount" title="销售金额" width="100">
        <template #default="{ row }">
          {{ $_common.formattedNumber(row.payAmount) }}
        </template>
      </vxe-table-column>
      <vxe-table-column field="notPickingNum" title="未拣货数量" width="100">
        <template #default="{ row }">
          {{ $_common.formatNub(row.notPickingNum) }}
        </template>
      </vxe-table-column>
      <vxe-table-column field="pickingNum" title="已拣货数量" width="100">
        <template #default="{ row }">
          {{ $_common.formatNub(row.pickingNum) }}
        </template>
      </vxe-table-column>
      <vxe-table-column field="notOutNum" title="未发货数量" width="100">
        <template #default="{ row }">
          {{ $_common.formatNub(row.notOutNum) }}
        </template>
      </vxe-table-column>
      <vxe-table-column field="outTime" title="预计发货时间" width="130">
        <template #default="{ row }">
          <span v-if="row.outTime">
            {{ $_common.formatDate(row.outTime) }}
          </span>
          <span v-else>0</span>
        </template>
      </vxe-table-column>
      <vxe-table-column
        field="mangeName"
        title="经手人"
        width="100"
      ></vxe-table-column>
      <vxe-table-column show-overflow title="提货地址" width="160">
        <!--        <template #default="{ row }">-->
        <!--          {{ row.orderReceiveData.area.provinceName }}-{{-->
        <!--            row.orderReceiveData.area.cityName-->
        <!--          }}-{{ row.orderReceiveData.area.districtName }}-{{-->
        <!--            row.orderReceiveData.address-->
        <!--          }}-->
        <!--        </template>-->
      </vxe-table-column>
      <vxe-table-column
        field="deliveryMsg"
        title="配送方式"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="remark"
        title="单据备注"
        width="100"
      ></vxe-table-column>
      <vxe-table-column title="提交时间" width="150">
        <template #default="{ row }">
          {{ $_common.formatDate(row.createTime) }}
        </template>
      </vxe-table-column>
      <vxe-table-column fixed="right" title="操作" width="100">
        <template #default="{ row }">
          <el-button type="text" size="mini" @click="getOrderPickingInfo(row)">
            生成拣货单
          </el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    >
      <div slot="btn-div" class="foot-btn-div">
        <span class="batch-checkbox">
          <vxe-checkbox
            v-model="checkedAll"
            :indeterminate="isIndeterminate"
            @change="checkAllChange"
          ></vxe-checkbox>
        </span>
        <el-button size="mini">整单拣货</el-button>
        <el-button size="mini">拣货汇总</el-button>
        <el-button size="mini">商品汇总</el-button>
      </div>
    </FooterPage>
    <vxe-modal
      v-model="add_picking_show"
      title="按单拣货"
      width="700"
      min-height="400"
      resize
      remember
    >
      <template #default>
        <vxe-table :data="picking_list.goodsDate">
          <vxe-table-column
            field="goodsName"
            title="商品名称"
            width="120"
          ></vxe-table-column>
          <!--          <vxe-table-column-->
          <!--            field="num"-->
          <!--            title="仓库"-->
          <!--            width="80"-->
          <!--          ></vxe-table-column>-->
          <vxe-table-column
            field="barCode"
            title="条码"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="specGroup"
            title="规格"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="unitName"
            title="单位名称"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="num"
            title="生产日期"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="num"
            title="保质期"
            width="80"
          ></vxe-table-column>
          <!--          <vxe-table-column-->
          <!--            field="num"-->
          <!--            title="账面库存"-->
          <!--            width="80"-->
          <!--          ></vxe-table-column>-->
          <vxe-table-column
            field="buyNum"
            title="商品数量"
            width="80"
          ></vxe-table-column>
          <vxe-table-column
            field="buyNum"
            title="未拣货数量"
            width="90"
          ></vxe-table-column>
          <vxe-table-column
            fixed="right"
            field="num"
            title="本次拣货数量"
            width="110"
          >
            <template #default="{ row }">
              <el-input-number
                v-model="row.numCopy"
                size="mini"
                style="width: 80px"
                :controls="false"
                :min="1"
                :mix="row.buyNum"
                type="text"
                placeholder="数量"
              />
            </template>
          </vxe-table-column>
        </vxe-table>
        <div class="float_right" style="margin-top: 20px">
          <el-button @click="time_show = false">取 消</el-button>
          <el-button type="primary" @click="addPicking">生成拣货单</el-button>
        </div>
      </template>
    </vxe-modal>
  </ContainerQuery>
</template>
<script>
  import SelectCustomer from "@/component/common/SelectCustomer.vue";
  import WarehouseModel from "@/component/common/WarehouseModel.vue";
  import { getAllOrderPicking, getOrderPickingInfo } from "@/api/Order";
  import { addPicking } from "@/api/Stock";
  import { mapGetters } from "vuex";
  export default {
    components: {
      // SelectCustomer,
      WarehouseModel,
    },
    data() {
      return {
        keyword: "",
        top_time: "today",
        customer_name: "",
        customerId: "",
        time: "",
        start: "",
        end: "",
        Return: "",
        Return_status: [],
        warehouseName: "",
        warehouse_show: false,
        warehouseId: "",
        tableData: [],
        page: 1,
        pageSize: 10,
        total: 0,
        checkedAll: false,
        records: [],
        add_picking_show: false,
        picking_list: {},
        isIndeterminate: false,
      };
    },
    computed: {
      ...mapGetters({
        enterprise: "MUser/enterprise",
      }),
    },
    created() {
      this.getAllOrderPicking();
    },
    methods: {
      pageChange(val) {
        this.page = val;
        this.getAllOrderPicking();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      selTopType() {},
      customerClear() {
        this.customerId = "";
        this.customer_name = "";
        this.pageChange(1);
      },
      // 选择客户
      customerSel(val, list) {
        this.customerId = list[0].id;
        this.pageChange(1);
      },
      //  时间
      orderDate(val) {
        if (val && val.length) {
          this.start = val[0] / 1000;
          this.end = val[1] / 1000 + 86399;
        } else {
          this.start = "";
          this.end = "";
        }
        this.pageChange(1);
      },
      selWarehouse(row) {
        this.warehouseName = row[0].warehouseName;
        this.warehouseId = row[0].id;
        this.pageChange(1);
      },
      checkboxChangeEvent({ records }) {
        this.checkedAll = this.$refs.xTable.isAllCheckboxChecked();
        this.isIndeterminate = this.$refs.xTable.isAllCheckboxChecked();
        this.records = records;
      },
      checkAllChange() {
        this.$refs.xTable.setAllCheckboxRow(this.checkedAll);
        this.records = this.$refs.xTable.getCheckboxRecords();
      },
      async getAllOrderPicking() {
        const { data, pageTotal } = await getAllOrderPicking({
          page: this.page,
          pageSize: this.pageSize,
          no: this.keyword,
          startTime: this.start,
          endTime: this.end,
          timeName: this.top_time,
        });
        this.tableData = data;
        this.total = pageTotal;
      },
      async getOrderPickingInfo(row) {
        this.add_picking_show = true;
        const { data } = await getOrderPickingInfo(row.id);
        data.goodsDate.forEach((item) => {
          item.numCopy = item.buyNum;
        });
        this.picking_list = data;
      },
      async addPicking() {
        let arr = [];
        this.picking_list.goodsDate.forEach((item) => {
          arr.push({
            goodsCode: item.goodsCode,
            goodsId: item.goodsId,
            goodsName: item.goodsName,
            price: item.price,
            originPrice: item.originPrice,
            pickingNum: item.numCopy,
            totalMoney: item.totalMoney,
            orderNum: item.numCopy,
            goodsBasicId: item.goodsBasicId,
            unitName: item.unitName,
            conversion: item.conversion,
            outCostPrice: item.outCostPrice || 0,
            isEq: item.isEq,
            categoryPath: item.categoryPath,
            pickingedNum: 0,
            InspectionNum: item.numCopy,
            InspectionedNum: 0,
            goodsImages: item.goodsImages,
          });
        });
        let address = [];
        if (this.picking_list.orderReceive.length !== 0) {
          address =
            this.picking_list.orderReceive.area.provinceName +
            "-" +
            this.picking_list.orderReceive.area.cityName +
            "-" +
            this.picking_list.orderReceive.area.districtName;
        }
        const parser = {
          shopId: this.picking_list.shopId,
          shopName: this.picking_list.shopName,
          merchantId: this.picking_list.merchantId || 0,
          originId: this.picking_list.id,
          originNo: this.picking_list.no,
          warehouseId: 0,
          warehouseName: "",
          personnel: this.enterprise.userCenterId,
          personnelName: this.userName,
          currentUnitId: this.picking_list.customerId,
          currentUnit: this.picking_list.customerName,
          sort: 0,
          number: 0,
          goodsLine: arr.length,
          totalMoney: 0,
          notPickingNum: 0,
          pickingNum: 0,
          outNum: 0,
          notOutNum: 0,
          outTime: 0,
          InspectionNum: 0,
          InspectionedNum: 0,
          deliveryType: this.picking_list.deliveryMsg,
          address: address,
          managerId: this.enterprise.userCenterId,
          managerName: this.userName,
          pickingType: 1,
          remark: this.picking_list.remark,
          goodsData: arr,
        };
        this.$confirm("确定生成拣货单?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const { data } = await addPicking(parser);
          this.$message({
            type: "success",
            message: "生成拣货单成功!",
          });
          this.add_picking_show = false;
        });
      },
    },
  };
</script>
<style></style>
