<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="公司名" prop="companyId">
        <el-select filterable v-model="queryParams.companyId" placeholder="请选择公司名" @change="companyChange" clearable size="small">
          <el-option
            v-for="item in companys"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <treeselect style="width: 220px" :clearable="false" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
      </el-form-item>

      <el-form-item label="订单类型" prop="orderTypeFilter">
        <el-select v-model="queryParams.orderTypeFilter" placeholder="请选择订单类型" clearable size="small">
          <el-option label="全部" :value="null" />
          <el-option label="销售订单" :value="1" />
          <el-option label="商城订单" :value="2" />
          <el-option label="直播订单" :value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="销售名称" prop="salesName">
        <el-input
          v-model="queryParams.salesName"
          placeholder="请输入销售名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="运单号" prop="deliveryId">
        <el-input
          v-model="queryParams.deliveryId"
          placeholder="请输入运单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收件人" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入收件人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable size="small">
          <el-option
            v-for="item in payTypeOptions"
            :key="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 deliveryStatusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>

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

      <el-form-item>
        <el-button type="primary" 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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部订单" name="00"></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="-1"></el-tab-pane>
      <el-tab-pane label="已退款" name="-2"></el-tab-pane>
      <el-tab-pane label="已取消" name="-3"></el-tab-pane>
    </el-tabs>

    <el-table
      ref="orderTable"
      border
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
      :default-sort="{prop: 'createTime', order: 'descending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单类型" align="center" prop="orderTypeName" width="100px">
        <template slot-scope="scope">
          <el-tag :type="getOrderTypeTagType(scope.row.orderType)">
            {{ scope.row.orderTypeName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderCode" width="200px" />
      <el-table-column label="银行交易流水号" align="center" prop="bankTransactionId" width="200px" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="销售名称" align="center" prop="salesName" width="120px" />
      <el-table-column label="用户昵称" align="center" prop="nickname" width="150px" />
      <el-table-column label="收件人" align="center" prop="realName" width="120px" />
      <el-table-column label="手机号" align="center" prop="userPhone" width="120px" />
      <el-table-column label="订单金额" align="center" prop="totalPrice" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.totalPrice != null">{{ scope.row.totalPrice.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" prop="payPrice" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.payPrice != null">{{ scope.row.payPrice.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payMoney" width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.payMoney != null">{{ scope.row.payMoney.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="180" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
      <el-table-column label="支付方式" align="center" prop="payType" width="100px">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in payTypeOptions" :key="index" v-if="scope.row.payType == item.dictValue" size="mini">
            {{ item.dictLabel }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100px">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in statusOptions" :key="index" v-if="scope.row.status == item.dictValue" size="mini">
            {{ item.dictLabel }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="物流状态" align="center" prop="deliveryStatus" width="100px">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in deliveryStatusOptions" :key="index" v-if="scope.row.deliveryStatus == item.dictValue" size="mini">
            {{ item.dictLabel }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="运单号" align="center" prop="deliveryId" width="150px" />
      <el-table-column label="操作" fixed="right" width="80px" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >查看</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 { listMergedOrder } from '@/api/live/mergedOrder'
import { getCompanyList } from '@/api/company/company'
import { treeselect } from '@/api/company/companyDept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'MergedOrder',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 公司列表
      companys: [],
      companyId: null,
      // 部门树选项
      deptOptions: [],
      // 字典
      payTypeOptions: [],
      deliveryStatusOptions: [],
      statusOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      createTimeRange: [],
      activeName: '00',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        deptId: null,
        orderTypeFilter: null,
        salesName: null,
        orderCode: null,
        deliveryId: null,
        userPhone: null,
        realName: null,
        productName: null,
        payType: null,
        deliveryStatus: null,
        status: null
      }
    }
  },
  created() {
    getCompanyList().then(response => {
      this.companys = response.data
      if (this.companys != null && this.companys.length > 0) {
        this.companyId = this.companys[0].companyId
        this.getTreeselect()
      }
    })
    this.getDicts('store_pay_type').then((response) => {
      this.payTypeOptions = response.data
    })
    this.getDicts('store_order_status').then((response) => {
      this.statusOptions = response.data
    })
    this.getDicts('store_order_delivery_status').then((response) => {
      this.deliveryStatusOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      if (this.queryParams.status == '00') {
        this.queryParams.status = null
      }

      if (this.createTimeRange != null && this.createTimeRange.length == 2) {
        this.queryParams.createTimeRange = this.createTimeRange[0] + '--' + this.createTimeRange[1]
      } else {
        this.queryParams.createTimeRange = null
      }

      listMergedOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(error => {
        console.error('查询失败:', error)
        this.loading = false
        this.$message.error('查询数据失败')
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id || item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleClick(tab, event) {
      this.activeName = tab.name
      this.queryParams.status = tab.name
      this.getList()
    },
    handleDetails(row) {
      // 根据订单类型跳转到不同的详情页面
      if (row.orderType === 1 || row.orderType === 2) {
        // 销售订单或商城订单
        const orderId = row.id
        this.$router.push({
          path: '/hisStore/storeOrder',
          query: { id: orderId }
        })
      } else if (row.orderType === 3) {
        // 直播订单
        const orderId = row.orderId
        this.$router.push({
          path: '/live/liveOrder',
          query: { id: orderId }
        })
      }
    },
    getOrderTypeTagType(orderType) {
      const typeMap = {
        1: 'success', // 销售订单
        2: 'warning', // 商城订单
        3: 'info' // 直播订单
      }
      return typeMap[orderType] || ''
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      var param = { companyId: this.companyId }
      treeselect(param).then((response) => {
        this.deptOptions = response.data
      })
    },
    companyChange(val) {
      this.companyId = val
      this.getTreeselect()
    }
  }
}
</script>

<style scoped>
</style>

