<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-select
        v-model="searchData.payState"
        class="filter-item"
        filterable
        clearable
        placeholder="请选择交易状态"
      >
        <el-option label="待回款" value="0"></el-option>
        <el-option label="已支付" value="1"></el-option>
        <el-option label="待退款" value="2"></el-option>
        <el-option label="已退款" value="3"></el-option>
      </el-select>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button
        v-permission="'HY_export'"
        type="primary"
        class="filter-item"
        icon="el-icon-upload2"
        @click="btn_export"
      >导出</el-button>
      <el-button
        v-permission="'HY_waybill_number_import'"
        type="primary"
        class="filter-item"
        @click="btn_waybill_number_import"
      >运单号导入</el-button>
      <!-- <el-button v-permission="'HY_merge'" type="primary" class="filter-item" @click="btn_merge">合并</el-button> -->
      <el-button v-permission="'HY_split'" type="primary" class="filter-item" @click="btn_split">拆分</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'"
            v-if="authority_edit(scope.row.F_State)"
            type="text"
            @click="btn_edit(scope.row.F_Id)"
          >编辑</el-button>
          <el-button
            v-permission="'HY_goback'"
            v-if="authority_goback(scope.row.F_State)"
            type="text"
            @click="btn_goback(scope.row.F_Id)"
          >退回</el-button>
          <el-button
            v-permission="'HY_through'"
            v-if="authority_through(scope.row.F_State)"
            type="text"
            @click="btn_through(scope.row.F_Id)"
          >通过</el-button>
          <el-button v-permission="'HY_label'" type="text" @click="btn_label(scope.row.F_Id)">标签</el-button>
          <el-button
            v-permission="'HY_logistics_status'"
            v-if="authority_logistics_status(scope.row.F_Status,scope.row.F_State)"
            type="text"
            @click="btn_logistics_status(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 { getOrderListCustomerService } from "@/api/customerServiceManagement/orderListCustomerService";
import { pickerOptions, formatterContent } from "@/utils";
import goback from "./goback";
import through from "./through";
import label from "./label";
import logistics from "./logistics";
import edit from "./edit";
import merge from './merge';
import detail from "@/views/orderManagement/orderList/detail"
import importWaybillNumber from './importWaybillNumber';
import Pagination from "@/components/Pagination"; // 对el-pagination 二次封装

export default {
  name: "OrderListCustomerService",
  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: '', //订单时间
        payState: '', //交易状态
      },
      activeName: "-2", //标签切换首页
      pickerOptions, // 日期数据
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [], // 列表数据
      multipleSelection: [], // 选中的数据
      selectedId: [], // 选中的数据的id
      orderType: [],//订单类型
      mergeOrderId: [], //合并订单id
    };
  },
  created () {
    if (this.$route.params.id) {
      this.activeName = this.$route.params.id
    }
    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: 'this.searchData.payState', //交易状态
          F_IsExpress: -2, //-2全部 0客服未分配快递 1已分配
        };
        getOrderListCustomerService(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: "编辑订单"
      });
    },
    //导出
    btn_export () { },
    //导入
    btn_waybill_number_import () {
      this.$layer.iframe({
        content: {
          content: importWaybillNumber, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': '' }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['40%', '380px'],
        shadeClose: false,
        title: '导入订单信息'
      })
    },
    //退回订单
    btn_goback (orderid) {
      this.$layer.iframe({
        content: {
          content: goback, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: orderid } // props
        },
        area: ["30%", "30%"],
        title: "退回订单"
      });
    },
    //通过订单
    btn_through (orderid) {
      this.$layer.iframe({
        content: {
          content: through, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: orderid } // props
        },
        area: ["30%", "30%"],
        title: "通过订单"
      });
    },
    //标签订单
    btn_label (orderid) {
      this.$layer.iframe({
        content: {
          content: label, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: orderid } // props
        },
        area: ["30%", "30%"],
        title: "标签"
      });
    },
    //标签订单
    btn_logistics_status (orderid) {
      this.$layer.iframe({
        content: {
          content: logistics, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: orderid } // props
        },
        area: ["30%", "30%"],
        title: "物流状态"
      });
    },
    //合并
    // btn_merge() {
    //   if (this.mergeOrderId.length < 2) {
    //     this.$message({
    //       message: '最少选择两条订单！',
    //       type: 'warning'
    //     });
    //     return
    //   }
    //   this.$layer.iframe({
    //     content: {
    //       content: merge, // 传递的组件对象
    //       parent: this, // 当前的vue对象
    //       data: { rowid: this.mergeOrderId } // props
    //     },
    //     area: ["80%", "90%"],
    //     title: "合并订单"
    //   });
    // },
    //拆分
    btn_split () { },
    //订单详情
    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
    },
    //编辑权限
    authority_edit (state) {
      if (state == 6 || state == 3) {
        return false
      } else {
        return true
      }
    },
    //退回权限
    authority_goback (state) {
      if (state == 6) {
        return false
      } else {
        return true
      }
    },
    //通过权限
    authority_through (state) {
      if (state == 6 || state == 3) {
        return false
      } else {
        return true
      }
    },
    //物流状态权限
    authority_logistics_status (status, state) {
      if (status == 0 || status == 1) {
        if (state == 6) {
          return true
        } else {
          return false
        }
      } else {
        return false
      }
    }
  }
};
</script>

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