<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="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="cargoControlStatus">
        <dict-selector :type="DICT_TYPE.CONTROL_GOODS_STATUS" v-model="queryParams.cargoControlStatus" clearable @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>
        <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="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">
      <el-table-column label="主键ID" align="center" prop="orderId" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="父编号" align="center" prop="parentNumber" />
      <el-table-column label="下单会员" align="center" prop="userId" />
      <el-table-column label="唛头" align="center" prop="marks" />
 
      <el-table-column label="产品备案属性" align="center" prop="productRecord">
        <template slot-scope="scope">
          <span>{{ scope.row.productRecord | recordFilter(productRecordList) }}</span>
        </template>
      </el-table-column>

       <el-table-column label="控货状态" align="center" prop="cargoControlStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.cargoControlStatus | cargoControlStatusFilter(cargoControlStatusList) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="报关类别" align="center" prop="customsType">
        <template slot-scope="scope">
          <span>{{ scope.row.customsType | customsTypeFilter(customsTypeList) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="入仓箱数" align="center" prop="sumNum" />
      <el-table-column label="入仓方数" align="center" prop="sumVolume" />
      <el-table-column label="入仓重量" align="center" prop="sumWeight" />
      <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" 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-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 { createOrder, updateOrder, deleteOrder, getOrder, getOrderPage, exportOrderExcel } from "@/api/ecw/order";
import { getDictDatas, DICT_TYPE } from '@/utils/dict';
export default {
  name: "Order",
  components: {
    
  },
  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
    }
  },
  data() {
    return {
      // 遮罩层
      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)
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      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(() => {});
    }
  }
};
</script>
