<template>
  <ContainerQuery>
    <div slot="left">
      <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="selTopType"
      >
        <el-tab-pane label="昨日" name="1"></el-tab-pane>
        <el-tab-pane label="今日" name="2"></el-tab-pane>
        <el-tab-pane label="本周" name="3"></el-tab-pane>
        <el-tab-pane label="本月" name="4"></el-tab-pane>
        <el-tab-pane label="上月" name="5"></el-tab-pane>
        <el-tab-pane label="近三月" name="6"></el-tab-pane>
        <el-tab-pane label="本年" name="7"></el-tab-pane>
      </el-tabs>
    </div>
    <div slot="more">
      <el-form inline size="small">
        <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-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-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>
          <SelectCustomer
            v-model="customer_name"
            placeholder="经手人"
            @clear="customerClear"
            @change="customerSel"
          />
        </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-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-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-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="time1"
            type="date"
            style="width: 220px"
            placeholder="预计发货日期"
          ></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-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"
      auto-resize
      :data="tableData"
      @checkbox-all="checkboxChangeEvent"
      @checkbox-change="checkboxChangeEvent"
    >
      <vxe-table-column
        type="checkbox"
        width="60"
        fixed="left"
      ></vxe-table-column>
      <vxe-table-column
        fixed="left"
        field="documentDate"
        title="单据日期"
        width="150"
      >
        <template #default="{ row }">
          {{ $_common.formatDate(row.documentDate) }}
        </template>
      </vxe-table-column>
      <vxe-table-column
        fixed="left"
        field="documentNo"
        title="单据编号"
        width="200"
      ></vxe-table-column>
      <vxe-table-column
        field="sourceNo"
        title="来源订单"
        width="160"
      ></vxe-table-column>
      <vxe-table-column field="deliveryState" title="配送状态" width="100">
        <template #default="{ row }">
          <span v-if="row.deliveryState === 1">待分配</span>
          <span v-if="row.deliveryState === 2">已分配</span>
        </template>
      </vxe-table-column>
      <vxe-table-column
        field="documentType"
        title="单据类型"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="customerName"
        title="客户"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="deliveryWarehouse"
        title="发货仓库"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="deliveryQuantity"
        title="发货数量"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="deliveryAmount"
        title="发货金额"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="returnWarehouse"
        title="退货仓库"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="returnAmount"
        title="退货金额"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="containerLocation"
        title="集货拉"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="binningNum"
        title="装箱数量"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="packingNum"
        title="包装数量1"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="packingNum"
        title="包装数量2"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="packingNum"
        title="包装数量3"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="manager"
        title="经手人"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="producer"
        title="制单人"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="receiver"
        title="收货人"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="mobile"
        title="联系电话"
        width="150"
      ></vxe-table-column>
      <vxe-table-column
        field="receivingAddress"
        title="收货地址"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="distributionRoute"
        title="配送线路"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="expectedDelivery"
        title="预计发货"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="modeOfProduction"
        title="产生方式"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="distributionDriver"
        title="配送司机"
        width="100"
      ></vxe-table-column>
      <vxe-table-column
        field="remarks"
        title="备注"
        width="100"
      ></vxe-table-column>
      <vxe-table-column fixed="right" field="name" title="操作" width="100">
        <template #default>
          <el-button type="text" size="mini">终止配送</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" @click="distribution()">创建配送任务</el-button>
        <el-button size="mini">终止配送</el-button>
      </div>
    </FooterPage>
    <vxe-modal
      v-model="distribution_task_show"
      title="创建配送任务"
      width="1120"
      min-height="400"
      resize
      remember
    >
      <template #default>
        <el-form ref="form" :model="logisticsData" inline>
          <el-form-item>
            <el-input
              v-model="logisticsData.driverName"
              placeholder="选择司机"
              style="width: 200px"
              clearable
              @clear="clearDriver"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="driver_show = true"
              ></i>
            </el-input>
          </el-form-item>
          <el-form-item>
            <SelectCustomer
              v-model="logisticsData.customer_name"
              placeholder="送货人"
              :modal="false"
              @clear="logisticsDataCustomerClear"
              @change="logisticsDataCustomerSel"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="logisticsData.truckName"
              style="width: 200px"
              readonly
              placeholder="车辆号码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="logisticsData.time"
              type="date"
              placeholder="指定配送日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="logisticsData.remark"
              style="width: 200px"
              readonly
              placeholder="备注"
            ></el-input>
          </el-form-item>
        </el-form>
        <vxe-table :data="records">
          <vxe-table-column
            fixed="left"
            field="documentDate"
            title="单据日期"
            width="120"
          >
            <template #default="{ row }">
              {{ $_common.formatDate(row.documentDate) }}
            </template>
          </vxe-table-column>
          <vxe-table-column
            fixed="left"
            field="documentNo"
            title="单据编号"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="sourceNo"
            title="来源订单"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="customerName"
            title="客户"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="manager"
            title="经手人"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="receiver"
            title="收货人"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="mobile"
            title="联系电话"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="receivingAddress"
            title="收货地址"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="distributionRoute"
            title="配送线路"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="goodsName"
            title="配送顺序"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="deliveryWarehouse"
            title="发货仓库"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="binningNum"
            title="装箱数量"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="deliveryQuantity"
            title="发货数量"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="returnWarehouse"
            title="退货仓库"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="goodsName"
            title="退货数量"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="containerLocation"
            title="集货拉"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="goodsName"
            title="预计发货日期"
            width="120"
          ></vxe-table-column>
          <vxe-table-column
            field="remarks"
            title="备注"
            width="120"
          ></vxe-table-column>
        </vxe-table>
        <div class="float_right" style="margin-top: 20px">
          <el-button @click="distribution_task_show = false">取 消</el-button>
          <el-button type="primary">生成拣货单</el-button>
        </div>
      </template>
    </vxe-modal>
    <!--    选择司机弹窗-->
    <DriverModel
      v-if="driver_show"
      :is-show="driver_show"
      :is-check="false"
      @confirm="selDriver"
      @cancel="driver_show = false"
    ></DriverModel>
  </ContainerQuery>
</template>
<script>
  import SelectCustomer from "@/component/common/SelectCustomer.vue";
  import WarehouseModel from "@/component/common/WarehouseModel.vue";
  import { getAllDistribution } from "@/api/Delivery";
  import DriverModel from "@/component/common/DriverModel";

  export default {
    components: {
      SelectCustomer,
      WarehouseModel,
      DriverModel,
    },
    data() {
      return {
        keyword: "",
        top_time: "1",
        customer_name: "",
        customerId: "",
        time: "",
        start: "",
        end: "",
        Return: "",
        Return_status: [],
        warehouseName: "",
        warehouse_show: false,
        warehouseId: "",
        tableData: [],
        page: 1,
        pageSize: 10,
        total: 0,
        checkedAll: false,
        records: [],
        time1: "",
        distribution_task_show: false,
        isIndeterminate: false,
        driver_show: false,
        logisticsData: {
          driverName: "",
          driverId: "",
          customer_name: "",
          customerId: "",
          truckName: "",
          time: "",
          remark: "",
        },
      };
    },
    created() {
      this.getAllDistribution();
    },
    methods: {
      pageChange(val) {
        this.page = val;
        this.getAllDistribution();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      selTopType() {},
      customerClear() {
        this.customerId = "";
        this.customer_name = "";
        this.pageChange(1);
      },
      logisticsDataCustomerClear() {
        this.logisticsData.customerId = "";
        this.logisticsData.customer_name = "";
      },
      // 选择客户
      customerSel(val, list) {
        this.customerId = list[0].id;
        this.pageChange(1);
      },
      //选择送货人
      logisticsDataCustomerSel(val, list) {
        this.logisticsData.customerId = list[0].id;
      },
      //  时间
      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 getAllDistribution() {
        const { data } = await getAllDistribution({
          page: this.page,
          pageSize: this.pageSize,
        });
        this.tableData = data;
      },
      distribution() {
        if (!this.records.length) {
          this.$message.warning("请先选中要处理的销售出库单!");
          return;
        }
        this.distribution_task_show = true;
      },
      selDriver(row) {
        this.logisticsData.driverId = row[0].id;
        this.logisticsData.driverName = row[0].driverName;
        this.logisticsData.truckName = row[0].plateNumber;
      },
      clearDriver() {
        this.logisticsData.driverId = "";
        this.logisticsData.truckName = "";
      },
    },
  };
</script>
<style></style>
