<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="searchData.searchTime"
        :picker-options="pickerOptions"
        class="filter-item"
        type="daterange"
        format="yyyy年MM月dd日"
        value-format="yyyy-MM-dd"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-input v-model="searchData.adFrom" placeholder="请输入消息/广告媒体来源" class="filter-item" />
      <el-select v-model="searchData.type" class="filter-item" filterable clearable placeholder="请选择订单类型">
        <el-option v-for="item in orderType" :key="item.F_Value" :label="item.F_Value" :value="item.F_Value"/>
      </el-select>
      <el-input v-model="searchData.id" placeholder="请输入订单号" class="filter-item" />
      <el-input v-model="searchData.customer" placeholder="请输入客户姓名" class="filter-item" />
      <el-input v-model="searchData.customerPhone" placeholder="请输入客户手机号" class="filter-item" />
      <el-input v-model="searchData.trackingNo" placeholder="请输入运单号" class="filter-item" />
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
    </div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick" >
      <el-tab-pane label="全部" name="-2"></el-tab-pane>
      <!-- <el-tab-pane label="无效" name="-1"></el-tab-pane> -->
      <el-tab-pane label="暂存" name="0"></el-tab-pane>
      <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-tabs>
    <el-table v-loading="loading" :data="dataLists" border stripe  @selection-change="handleSelectionChange" row-key="F_Id">
      <el-table-column type="selection" :reserve-selection="true"></el-table-column>
      <el-table-column type="index" label="编号" align="center" fixed width="80" />
      <el-table-column prop="F_Id" label="订单编号" align="center" min-width="180" >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="hadndleOrderCode(scope.row.F_Id)">{{ scope.row.F_Id }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="F_Customer" label="客户姓名" align="center" min-width />
      <el-table-column prop="F_CustomerPhone" label="手机号码" align="center" min-width="140" />
      <el-table-column label="订购商品" align="center" min-width>
        <template slot-scope="scope">
          {{ scope.row.OrderDetailList | judgmentOrderGoods}}
        </template>
      </el-table-column>
      <el-table-column prop="F_AddTime" label="下单日期" align="center" />
      <el-table-column label="订单状态" align="center" >
        <template slot-scope="scope">
          {{ scope.row.F_State | judgmentStateName }}
        </template>
      </el-table-column>
      <el-table-column prop="F_AddUserName" label="下单员工" align="center" />
      <el-table-column prop="F_BelongName" label="归属员工" align="center" />
      <el-table-column prop="F_Type" label="订单类型" align="center" />
      <el-table-column prop="F_Express" label="快递公司" align="center" />
      <el-table-column label="物流状态" align="center" >
        <template slot-scope="scope">
          {{ scope.row.F_Status | judgmentStatusName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'HY_edit'" type="text" @click="btn_edit(scope.row.F_Id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getDictionaryValueList } from '@/api/commonAPI'
import { getAfterSaleOrderList } from "@/api/afterSaleManagement/afterSaleList";
import { pickerOptions, formatterContent } from "@/utils";
import edit from "./edit";
import detail from "@/views/orderManagement/orderList/detail"
import Pagination from "@/components/Pagination"; // 对el-pagination 二次封装

export default {
  name: "orderList",
  components: {
    Pagination
  },
  filters: {
    judgmentStateName(status) {
      const statusMap = {
        '-1': '无效',
        '0': '暂存',
        '1': '提交',
        '2': '退回',
        '3': '通过',
        '4': '无货',
        '5': '已分拣',
        '6': '已发货'
      }
      return statusMap[status]
    },
    judgmentStatusName(status) {
      const statusMap = {
        '0': '未发出',
        '1': '未签收',
        '2': '签收',
        '3': '改代收',
        '4': '拒收',
      }
      return statusMap[status]
    },
    judgmentOrderGoods(status) {
      let orderGoodsNameQuantity = ''
      for (let i = 0; i < status.length; i++) {
        orderGoodsNameQuantity = `${orderGoodsNameQuantity}${status[i].F_ProductName}*${status[i].F_Count},`
      }
      orderGoodsNameQuantity = orderGoodsNameQuantity.substring(0, orderGoodsNameQuantity.length - 1)
      return orderGoodsNameQuantity
    },
  },
  data() {
    return {
      loading: false,
      searchData: {
        adFrom: '', //消息/广告媒体来源
        type: '', //订单类型
        id: '', //订单号
        customer: '', //客户姓名
        customerPhone: '', //客户手机号
        trackingNo: '', //运单号
        searchTime: '', //订单时间
      },
      activeName: "-2", //标签切换首页
      pickerOptions, // 日期数据
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [], // 列表数据
      multipleSelection: [], // 选中的数据
      selectedId: [], // 选中的数据的id
      orderType: [],//订单类型
      mergeOrderId: [], //合并订单id
    };
  },
  created() {
    this.getOrderType()
    this.getList()
    document.onkeyup = e => {
      if (e.keyCode === 13) {
        this.getList();
      }
    };
  },
  methods: {
    getList() {
      this.loading = true;
      return new Promise(resolve => {
        const params = {
          F_State: this.activeName, //订单状态
          pageindex: this.pageParams.pageindex, // int 第几页
          pagesize: this.pageParams.pagesize, // int 每页几条信息
          F_ADFrom: this.searchData.adFrom, //消息/广告来源
          F_Type: this.searchData.type, //订单类型
          F_Id: this.searchData.id, //订单号
          F_Customer: this.searchData.customer, //客户姓名
          F_CustomerPhone: this.searchData.customerPhone, //客户手机号
          F_TrackingNo: this.searchData.trackingNo, //运单号
          SearchStartTime: this.searchData.searchTime[0], //添加开始时间
          SearchEndTime: this.searchData.searchTime[1], //添加结束时间
          F_PayState:'-2',
        };
        getAfterSaleOrderList(params).then(response => {
          this.loading = false;
          if (response.state.toLowerCase() === "success") {
            this.pageParams.total = response.data.Totals;
            this.dataLists = response.data.Rows;
          }
        });
        resolve();
      });
    },
    // 获取订单类型
    getOrderType() {
      return new Promise(resolve=>{
        const params={
          isleaf:true,
          code:'ORDERTYPE'
        }
        getDictionaryValueList(params).then(response=>{
          if(response.state.toLowerCase() === 'success'){
            this.orderType = response.data.Rows
          }
        })
      })
    },
    handleTabClick(tab, event) {
      this.getList()
    },
    btn_search() {
      this.pageParams.pageindex = 1;
      this.getList();
    },
    btn_edit(editId) {
      this.$layer.iframe({
        content: {
          content: edit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: editId } // props
        },
        area: ["80%", "90%"],
        title: "编辑订单"
      });
    },
    hadndleOrderCode(ordercode){
      this.$layer.iframe({
        content: {
          content: detail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': ordercode }// props
        },
        area: ['80%', '90%'],
        title: '订单详情'
      })
    },
    //选择多个
    handleSelectionChange(val) {
      const ids = []
      this.multipleSelection = val
      for (let i=0; i<this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].F_Id)
      }
      this.mergeOrderId = ids
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el{
  cursor: pointer;
}
</style>
