<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="唛头" prop="marks">
        <el-input v-model="queryParams.marks" placeholder="唛头" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="发货人" prop="consignorId">
        <el-input v-model="queryParams.consignorId" placeholder="发货人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="收货人" prop="consigneeId">
        <el-input v-model="queryParams.consigneeId" placeholder="收货人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="始发地" prop="startWarehouseId">
        <el-select v-model="queryParams.startWarehouseId" placeholder="请选择始发地">
          <el-option v-for="item in expoerCityList" :label="item.titleZh" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="destWarehouseId">
        <el-select v-model="queryParams.destWarehouseId" placeholder="请选择目的地">
          <el-option v-for="item in importCityList" :label="item.titleZh" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="运输方式" prop="transportId">
        <dict-selector :type="DICT_TYPE.ECW_TRANSPORT_TYPE" v-model="queryParams.transportId" formatter="number" />
      </el-form-item>

      <el-form-item label="商品分类" prop="goodsType">
        <selector v-model="queryParams.goodsType" :options="productAttrList" label-field="attrName" value-field="id">
        </selector>
      </el-form-item>
      <el-form-item label="品名" prop="prodId">
        <product-selector v-model="queryParams.prodId" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <dict-selector v-model="queryParams.brand" :type="DICT_TYPE.ECW_IS_BRAND" formatter="bool" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提单号" prop="tidanNo">
        <el-input v-model="queryParams.tidanNo" placeholder="提单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="控货" prop="isCargoControl">
        <dict-selector v-model="queryParams.isCargoControl" :type="DICT_TYPE.INFRA_BOOLEAN_STRING" fomatter="bool" />
      </el-form-item>
      <el-form-item label="入仓类型" prop="warehouseType">
        <dict-selector :type="DICT_TYPE.ECW_WAREHOUSING_TYPE" v-model="queryParams.warehousingType" />
      </el-form-item>
      <!-- <el-form-item label="跟进客服" prop="">
        <customer-selector v-model="" />
      </el-form-item> -->
      <el-form-item label="订单状态" prop="status">
        <dict-selector :type="DICT_TYPE.PAY_ORDER_STATUS" v-model="queryParams.status"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报关方式" prop="customsType">
        <dict-selector :type="DICT_TYPE.ECW_CUSTOMS_TYPE" v-model="queryParams.customsType" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="是否齐货" prop="customsType">
        <dict-selector v-model="queryParams.isCargoControl" :type="DICT_TYPE.INFRA_BOOLEAN_STRING" fomatter="bool" />
      </el-form-item> -->
      <el-form-item label="重货比" prop="weightRatio">
        <el-input style="width: 100px;" v-model="molecule" placeholder="" clearable @change="moleculeChange"
          @keyup.enter.native="handleQuery" />
        -
        <el-input style="width: 100px;" v-model="denominator" placeholder="" clearable @change="denominatorChange"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备案属性" prop="productRecord">
        <dict-selector :type="DICT_TYPE.PRODUCT_RECORD_ATTRIBUTE" v-model="queryParams.productRecord" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递单号" prop="number">
        <el-input v-model="queryParams.number" placeholder="快递单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecw:order:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-setting" size="mini" @click="handleEdit" :disabled="multiple">批量调仓
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['ecw:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="唛头" align="center" prop="marks" />
      <el-table-column label="入仓箱数" align="center" prop="sumNum" />
      <el-table-column label="入仓重量" align="center" prop="sumWeight" />

      <el-table-column label="运输方式" align="center" prop="transportId">
        <template slot-scope="scope">
          <span>{{ scope.row.transportId | transportFilter(transportList) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status | orderStatusFilter(orderStatusList) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" 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="['ecw:order:update']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ecw:order:update']">退仓</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ecw:order:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="唛头" prop="marks">
          <el-input v-model="form.marks" placeholder="请输入唛头" />
        </el-form-item>
        <el-form-item label="产品备案属性" prop="productRecord">
          <dict-selector :type="DICT_TYPE.PRODUCT_RECORD_ATTRIBUTE" v-model="form.productRecord" clearable />
        </el-form-item>
        <el-form-item label="控货状态" prop="cargoControlStatus">
          <dict-selector :type="DICT_TYPE.CONTROL_GOODS_STATUS" v-model="form.cargoControlStatus" clearable />
        </el-form-item>
        <el-form-item label="报关类别" prop="customsType">
          <dict-selector :type="DICT_TYPE.ECW_CUSTOMS_TYPE" v-model="form.customsType" clearable />
        </el-form-item>

        <el-form-item label="入仓箱数" prop="sumNum">
          <el-input v-model="form.sumNum" placeholder="请输入入仓箱数" />
        </el-form-item>
        <el-form-item label="入仓方数" prop="sumVolume">
          <el-input v-model="form.sumVolume" placeholder="请输入入仓方数" />
        </el-form-item>
        <el-form-item label="入仓重量" prop="sumWeight">
          <el-input v-model="form.sumWeight" placeholder="请输入入仓重量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Selector from '@/components/Selector'
import ProductSelector from '@/components/ProductSelector'
import { getProductAttrList } from '@/api/ecw/productAttr'
import CustomerSelector from '@/components/CustomerSelector'
import { getTradeCityList } from '@/api/ecw/region'
import { createOrder, updateOrder, deleteOrder, getOrder, getOrderPage, exportOrderExcel } from "@/api/ecw/order";
import { getDictDatas, DICT_TYPE } from '@/utils/dict';
export default {
  name: "Order",
  components: {
    CustomerSelector, ProductSelector, Selector
  },
  filters: {
    cargoControlStatusFilter(e, cargoControlStatusList) {
      if (e) return cargoControlStatusList.find(item => item.value == e).label
    },
    recordFilter(e, productRecordList) {
      if (e) return productRecordList.find(item => item.value == e).label
    },
    customsTypeFilter(e, customsTypeList) {
      if (e) return customsTypeList.find(item => item.value == e).label
    },
    orderStatusFilter(e, orderStatusList) {
      if (e) return orderStatusList.find(item => item.value == e) ? orderStatusList.find(item => item.value == e).label : ''
    },
    transportFilter(e, transportList) {
      if (e) return transportList.find(item => item.value == e).label
    }
  },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeRucangtime: [],
      dateRangeDaogangtime: [],
      dateRangeQingguantime: [],
      dateRangePreLoadTime: [],
      dateRangeLoadTime: [],
      dateRangeUnloadTime: [],
      dateRangeTakeTime: [],
      dateRangeSplitTime: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        number: null,
        tidanNo: null,
        parentNumber: null,
        oldNumbers: null,
        transportId: null,
        lineId: null,
        channelId: null,
        orderType: null,
        userId: null,
        marks: null,
        productRecord: null,
        isCargoControl: null,
        cargoControlStatus: null,
        customsType: null,
        upFile: null,
        payType: null,
        status: null,
        isDel: null,
        collectionProxy: null,
        isCollection: null,
        zhongPao: null,
        wVolume: null,
        vWeight: null,
        weightUnit: null,
        warehouseType: null,
        countryAbbreviation: null,
        countryCode: null,
        sendsms: null,
        commissionId: null,
        documentsFile: null,
        remarks: null,
        isTodoOrder: null,
        advanceType: null,
        transFee: null,
        transCurrency: null,
        plateFee: null,
        boxFee: null,
        todoDetail: null,
        advanceWho: null,
        toDepartureId: null,
        sumNum: null,
        sumVolume: null,
        sumWeight: null,
        weightRatio: null,
        customerId: null,
        wareId: null,
        areaId: null,
        locationId: null,
        locationName: null,
        sumValue: null,
        valuationFee: null,
        cost: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [{ required: true, message: "订单编号不能为空", trigger: "blur" }],
        sumVolume: [{ required: true, message: "入仓方数不能为空", trigger: "blur" }],
        sumWeight: [{ required: true, message: "入仓重量不能为空", trigger: "blur" }],
        weightRatio: [{ required: true, message: "重货比不能为空", trigger: "blur" }],
      },
      productRecordList: getDictDatas(DICT_TYPE.PRODUCT_RECORD_ATTRIBUTE),
      cargoControlStatusList: getDictDatas(DICT_TYPE.CONTROL_GOODS_STATUS),
      customsTypeList: getDictDatas(DICT_TYPE.ECW_CUSTOMS_TYPE),
      orderStatusList: getDictDatas(DICT_TYPE.PAY_ORDER_STATUS),
      transportList: getDictDatas(DICT_TYPE.ECW_TRANSPORT_TYPE),
      tradeCityList: [],
      productAttrList: [],  // 商品属性
      molecule: '', //重货比分子
      denominator: '' //重货比分母
    };
  },
  computed: {
    expoerCityList() {
      return this.tradeCityList.filter(item => item.type == 2)
    },
    importCityList() {
      return this.tradeCityList.filter(item => item.type == 1)
    }
  },
  created() {
    this.getList();
    getProductAttrList().then(res => this.productAttrList = res.data)
    getTradeCityList().then(res => this.tradeCityList = res.data)
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = { ...this.queryParams };
      console.log('params', params)
      this.addBeginAndEndTime(params, this.dateRangeRucangtime, 'rucangtime');
      this.addBeginAndEndTime(params, this.dateRangeDaogangtime, 'daogangtime');
      this.addBeginAndEndTime(params, this.dateRangeQingguantime, 'qingguantime');
      this.addBeginAndEndTime(params, this.dateRangePreLoadTime, 'preLoadTime');
      this.addBeginAndEndTime(params, this.dateRangeLoadTime, 'loadTime');
      this.addBeginAndEndTime(params, this.dateRangeUnloadTime, 'unloadTime');
      this.addBeginAndEndTime(params, this.dateRangeTakeTime, 'takeTime');
      this.addBeginAndEndTime(params, this.dateRangeSplitTime, 'splitTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getOrderPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        orderId: undefined,
        orderNo: undefined,
        number: undefined,
        tidanNo: undefined,
        parentNumber: undefined,
        oldNumbers: undefined,
        transportId: undefined,
        lineId: undefined,
        channelId: undefined,
        orderType: undefined,
        userId: undefined,
        marks: undefined,
        productRecord: undefined,
        isCargoControl: undefined,
        cargoControlStatus: undefined,
        customsType: undefined,
        upFile: undefined,
        payType: undefined,
        status: undefined,
        isDel: undefined,
        collectionProxy: undefined,
        isCollection: undefined,
        zhongPao: undefined,
        wVolume: undefined,
        vWeight: undefined,
        weightUnit: undefined,
        warehouseType: undefined,
        countryAbbreviation: undefined,
        countryCode: undefined,
        sendsms: undefined,
        commissionId: undefined,
        documentsFile: undefined,
        remarks: undefined,
        isTodoOrder: undefined,
        advanceType: undefined,
        transFee: undefined,
        transCurrency: undefined,
        plateFee: undefined,
        boxFee: undefined,
        todoDetail: undefined,
        advanceWho: undefined,
        toDepartureId: undefined,
        sumNum: undefined,
        sumVolume: undefined,
        sumWeight: undefined,
        weightRatio: undefined,
        customerId: undefined,
        wareId: undefined,
        areaId: undefined,
        locationId: undefined,
        locationName: undefined,
        sumValue: undefined,
        valuationFee: undefined,
        rucangtime: undefined,
        daogangtime: undefined,
        cost: undefined,
        qingguantime: undefined,
        preLoadTime: undefined,
        loadTime: undefined,
        unloadTime: undefined,
        takeTime: undefined,
        splitTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeRucangtime = [];
      this.dateRangeDaogangtime = [];
      this.dateRangeQingguantime = [];
      this.dateRangePreLoadTime = [];
      this.dateRangeLoadTime = [];
      this.dateRangeUnloadTime = [];
      this.dateRangeTakeTime = [];
      this.dateRangeSplitTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId;
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.orderId != null) {
          updateOrder(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createOrder(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderId = row.orderId;
      this.$modal.confirm('是否确认删除订单编号为"' + orderId + '"的数据项?').then(function () {
        return deleteOrder(orderId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeRucangtime, 'rucangtime');
      this.addBeginAndEndTime(params, this.dateRangeDaogangtime, 'daogangtime');
      this.addBeginAndEndTime(params, this.dateRangeQingguantime, 'qingguantime');
      this.addBeginAndEndTime(params, this.dateRangePreLoadTime, 'preLoadTime');
      this.addBeginAndEndTime(params, this.dateRangeLoadTime, 'loadTime');
      this.addBeginAndEndTime(params, this.dateRangeUnloadTime, 'unloadTime');
      this.addBeginAndEndTime(params, this.dateRangeTakeTime, 'takeTime');
      this.addBeginAndEndTime(params, this.dateRangeSplitTime, 'splitTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有订单数据项?').then(() => {
        this.exportLoading = true;
        return exportOrderExcel(params);
      }).then(response => {
        this.$download.excel(response, '${table.classComment}.xls');
        this.exportLoading = false;
      }).catch(() => { });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleEdit() { },
    moleculeChange() { },
    denominatorChange() { }
  }
};
</script>
