<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="innerOrderNo">
        <el-input
          v-model="queryParams.innerOrderNo"
          placeholder="三方订单号/内部订单号,多个逗号隔开"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label-width="110px" label="商品名称/编码" prop="barCodes">
        <el-input
          v-model="queryParams.barCodes"
          placeholder="请输入商品名称/编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label-width="110px" label="SKU编码" prop="skuCodes">
        <el-input
          v-model="queryParams.skuCodes"
          placeholder="请输入预占编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
       <el-form-item  label="销售渠道" prop="channelType">
        <el-select  @change="channelChange" v-model="queryParams.channelType" placeholder="请选择销售渠道" clearable size="small">
           <el-option
                v-for="item in channelTypeOptions"
                :key="'channelType'+item.dictValue"  
                :label="item.dictLabel"
                :value="item.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="销售店铺" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择销售店铺" clearable size="small">
           <el-option
                v-for="item in stores"
                :key="'omsStore'+item.storeId"  
                :label="item.storeName"
                :value="item.storeId"/>
        </el-select>
      </el-form-item>
       <el-form-item label="订单来源" prop="innerOrderSource">
        <el-select v-model="queryParams.innerOrderSource" placeholder="请选择订单来源" clearable size="small">
           <el-option
                v-for="item in sourceOptions"
                :key="'omsSource'+item.dictValue"  
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="买家昵称" prop="buyerNick">
        <el-input
          v-model="queryParams.buyerNick"
          placeholder="请输入买家昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="110px" label="收货人姓名" prop="logisticsUserName">
        <el-input
          v-model="queryParams.logisticsUserName"
          placeholder="请输入收货人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
     <el-form-item label-width="110px" label="收货人手机" prop="logisticsUserPhone">
        <el-input
          v-model="queryParams.logisticsUserPhone"
          placeholder="请输入收货人手机"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买家备注" prop="buyerRemark">
        <el-input
          v-model="queryParams.buyerRemark"
          placeholder="请输入买家备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家备注" prop="orderRemark">
        <el-input
          v-model="queryParams.orderRemark"
          placeholder="请输入商家备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有无赠品" prop="isGift">
        <el-select v-model="queryParams.isGift" placeholder="请选择" clearable size="small">
           <el-option key="0" label="有" value="0"/>
           <el-option key="1" label="无" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="110px" label="三方下单时间" prop="orderTimeRange">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="orderTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="支付时间" prop="payTimeRange">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="payTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
           start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>      

      <el-form-item label="创建时间" prop="createTimeRange">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="createTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
           start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

     <el-form-item label="发货时间" prop="predictDeliveryTimeRange">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="predictDeliveryTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
           start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
    </el-form-item>

     <el-form-item label="标签" prop="tags">
        <el-select v-model="queryParams.tags" placeholder="请选择标签" clearable size="small">
           <el-option
                v-for="item in omsTagsOptions"
                :key="'omsTags'+item.dictValue" 
                :label="item.dictLabel"
                :value="item.dictLabel"
              />
        </el-select>
      </el-form-item>

     <el-form-item label="挂起状态" prop="suspendStatus">
        <el-select v-model="queryParams.suspendStatus" placeholder="请选择挂起状态" clearable size="small">
           <el-option
                v-for="item in omsSuspendStatusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>

      <el-form-item label="寻源状态" prop="sourceStatus">
        <el-select v-model="queryParams.sourceStatus" placeholder="请选择寻源状态" clearable size="small">
           <el-option
                v-for="item in omsSourceStatusOptions"
                :key="'omsSourceStatus'+item.dictValue" 
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>

      <el-form-item label="发货状态" prop="deliveryStatus">
        <el-select v-model="queryParams.deliveryStatus" placeholder="请选择发货状态" clearable size="small">
           <el-option
                v-for="item in omsDeliveryStatusOptions"
                :key="'omsDeliveryStatus'+item.dictValue" 
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>

      <el-form-item>
        <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="['system:order: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="['system:order: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="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="三方订单号" align="center" prop="outerOrderNo" />
      <el-table-column label="内部订单号" align="center" prop="innerOrderNo" />
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />

      <el-table-column  label="销售渠道" align="center" prop="channelType">
        <template slot-scope="scope">
          {{getChanelType(scope.row.chanelType)}}
        </template>
      </el-table-column>

      <el-table-column  label="订单来源" align="center" prop="innerOrderSource">
        <template slot-scope="scope" v-for="(item, index) in sourceOptions" >
            <el-tag prop="innerOrderSource"   :key="'innerOrderSource'+index"   :type="scope.row.innerOrderSource==0?'danger':'success'" v-if="scope.row.innerOrderSource==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="商品行数量" align="center" prop="skuNum" />
      <el-table-column label="买家昵称" align="center" prop="buyerNick" />
      <el-table-column label="买家备注" align="center" prop="buyerRemark" />
      <el-table-column label="商家备注" align="center" prop="orderRemark" />

      <el-table-column label="收货人姓名" align="center" prop="receiverNameMask" />
      <el-table-column label="收货人手机号" align="center" prop="receiverPhoneMask" />
      <el-table-column label="收货人地址" align="center" prop="receiverAddressMask" />
      <el-table-column  label="发货状态" align="center" prop="deliveryStatus">
        <template slot-scope="scope"  v-for="(item, index) in omsDeliveryStatusOptions" >
            <el-tag prop="deliveryStatus" :key="'deliveryStatus'+index"   :type="scope.row.deliveryStatus==0?'danger':'success'" v-if="scope.row.deliveryStatus==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="物流单号" align="center" prop="logisticsNo" />
      <el-table-column  label="收货状态" align="center" prop="receivingStatus">
        <template slot-scope="scope"  v-for="(item, index) in omsReceivingStatusOptions" >
            <el-tag prop="receivingStatus" :key="'receivingStatus'+index"   :type="scope.row.receivingStatus==0?'danger':'success'" v-if="scope.row.receivingStatus==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="商品总金额" align="center" prop="totalAmount" />
      <el-table-column label="买家实付" align="center" prop="buyerPayAmount" />
      <el-table-column label="商品总成本" align="center" prop="costAmount" />
      <el-table-column  label="三方下单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

     <el-table-column label="创单时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="预计发货时间" align="center" prop="predictDeliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.predictDeliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column  label="寻源状态" align="center" prop="sourceStatus">
        <template slot-scope="scope"  v-for="(item, index) in omsSourceStatusOptions" >
            <el-tag prop="sourceStatus" :key="'sourceStatus'+index"   :type="scope.row.sourceStatus==0?'danger':'success'" v-if="scope.row.sourceStatus==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

       <el-table-column  label="挂起状态" align="center" prop="suspendStatus">
        <template slot-scope="scope"  v-for="(item, index) in omsSuspendStatusOptions" >
            <el-tag prop="suspendStatus" :key="'suspendStatus'+index"   :type="scope.row.suspendStatus==0?'danger':'success'" v-if="scope.row.suspendStatus==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column  label="有无赠品" align="center" prop="isGift">
        <template slot-scope="scope">
            <el-tag prop="syncDeliveryStatus"  :type="scope.row.isGift==0?'warn':'success'" >{{scope.row.isGift==0?'无':'有'}}</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="创建人ID" align="center" prop="createUserId" />
      <el-table-column label="修改人ID" align="center" prop="updateUserId" /> -->
      <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="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order: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"
    />

  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/oms/orderInternal";
import { listStore } from "@/api/oms/store";
import Editor from '@/components/Editor';

export default {
  name: "Order",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oms销售订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelType:null,
        logisticsUserName:null,
        storeId: null,
        outerOrderNo: null,
        innerOrderNo: null,
        orderType: null,
        innerOrderSource: null,
        isGift: null,
        buyerNick: null,
        buyerRemark: null,
        skuCodes:null,
        barCodes:null,
        productName:null,
        
        innerOrderStatus: null,  
        orderStatus: null,
        syncStatus: null,
        refundStatus: null,
        payType: null,
        payStatus: null,
        auditType: null,
        auditStatus: null,
        orderStage: null,
        tags: null,
        goodsNum: null,
        skuNum: null,
        thirdDiscountAmount: null,
        businessDiscountAmount: null,
        totalDiscount: null,
        totalAmount: null,
        amountReceivable: null,
        buyerPayAmount: null,
        buyerRealPrice: null,
        shippingType: null,
        costAmount: null,
        postage: null,
        logisticsIds: null,
        orderRemark: null,
        serviceRemark: null,
        payNo: null,
        invoiceStatus: null,
        invoiceTopic: null,
        invoiceContent: null,
        deliveryStatus: null,
        receivingStatus: null,
        preemptionStatus: null,
        sourceStatus: null,
        suspendStatus: null,
        syncPreempStatus: null,
        syncDeliveryStatus: null,
        createUserId: null,
        updateUserId: null,

      },
      sourceOptions:[],
      channelTypeOptions:[],
      stores:[],
      omsTagsOptions:[],
      omsDeliveryStatusOptions:[],//发货状态
      omsSuspendStatusOptions:[],//挂起状态
      omsSourceStatusOptions:[],//寻源状态
      omsReceivingStatusOptions:[],//发货状态
      omsSyncPreempStatusOptions:[],//预占回写状态
      omsInnerOrderStatusOptitons:[],//内部订单状态
      orderTimeRange:[],
      payTimeRange:[],
      createTimeRange:[],
      predictDeliveryTimeRange:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("oms_channel_type").then((response) => {
        this.channelTypeOptions = response.data;
    });
    this.getDicts("oms_order_source").then((response) => {
      this.sourceOptions = response.data;
    });
    this.getDicts("oms_suspend_status").then((response) => {
      this.omsSuspendStatusOptions = response.data;
    });
    this.getDicts("oms_source_status").then((response) => {
      this.omsSourceStatusOptions = response.data;
    });
    this.getDicts("oms_delivery_status").then((response) => {
      this.omsDeliveryStatusOptions = response.data;
    });
    this.getDicts("oms_receiving_status").then((response) => {
       this.omsReceivingStatusOptions = response.data;
    });
    this.getDicts("oms_innerorder_status").then((response) => {
       this.omsInnerOrderStatusOptitons = response.data;
    });

    this.getDicts("oms_order_tags").then((response) => {
       this.omsTagsOptions = response.data;
    });
    
    this.getList();
  },
  methods: {
    /** 查询oms销售订单列表 */
    getList() {
      if(this.orderTimeRange!=null&&this.orderTimeRange.length==2){
        this.queryParams.orderTime=this.orderTimeRange[0]+"--"+this.orderTimeRange[1]
      }
      else{
        this.queryParams.orderTime=null;
      }
      if(this.payTimeRange!=null&&this.payTimeRange.length==2){
        this.queryParams.payTime=this.payTimeRange[0]+"--"+this.payTimeRange[1]
      }
      else{
        this.queryParams.payTime=null;
      }
      if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTime=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTime=null;
      }
      if(this.predictDeliveryTimeRange!=null&&this.predictDeliveryTimeRange.length==2){
        this.queryParams.predictDeliveryTime=this.predictDeliveryTimeRange[0]+"--"+this.predictDeliveryTimeRange[1]
      }
      else{
        this.queryParams.predictDeliveryTime=null;
      }
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        outerOrderNo: null,
        innerOrderNo: null,
        preemptCode: null,
        circulationStatus: 0,
        failureReason: null,
        orderType: null,
        orderSource: null,
        innerOrderSource: null,
        orderStatus: 0,
        syncStatus: 0,
        refundStatus: 0,
        payType: null,
        payStatus: 0,
        auditType: null,
        auditStatus: 0,
        orderStage: null,
        isGift: null,
        goodsNum: null,
        skuNum: null,
        thirdDiscountAmount: null,
        businessDiscountAmount: null,
        totalDiscount: null,
        totalAmount: null,
        amountReceivable: null,
        buyerPayAmount: null,
        buyerRealPrice: null,
        shippingType: null,
        costAmount: null,
        postage: null,
        logisticsIds: null,
        tags: null,
        buyerNick: null,
        buyerRemark: null,
        orderRemark: null,
        serviceRemark: null,
        payNo: null,
        invoiceStatus: 0,
        invoiceTopic: null,
        invoiceContent: null,
        deliveryStatus: 0,
        receivingStatus: 0,
        preemptionStatus: 0,
        sourceStatus: 0,
        suspendStatus: 0,
        syncPreempStatus: 0,
        syncDeliveryStatus: 0,
        orderTime: null,
        payTime: null,
        pullTime: null,
        lastShipTime: null,
        predictDeliveryTime: null,
        receiveTime: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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();
      // this.open = true;
      // this.title = "添加oms销售订单";
       this.$router.push({path:'/oms/order/internal/add',query:{"orderId":"1"}});

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改oms销售订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOrder(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('是否确认删除销售订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有oms销售订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    getChanelType(type){
      var chanelTypeName="";
      const chanelType = this.channelTypeOptions.filter((obj) => {
            return obj[dictValue] === type
      });
      if(chanelType!=null){
          chanelTypeName=chanelType.dictLable;
      }
      return chanelTypeName;
    },
    channelChange(e){
        var item=this.channelTypeOptions.find(val => val.dictValue === e);
        var channelType=item.dictValue;
        this.queryParams.channelType=channelType;
        listStore({"channelType":channelType}).then(response => {
            this.stores = response.rows;
        });
    },
  }
};
</script>
