<template>
  <div class="app-container">
    <div v-loading.fullscreen.lock="loadingFull" />
    <el-select v-model="listQuery.activity_id" style="width:300px;margin-bottom:20px" @change="seclect">
      <el-option v-for="item in activitys" :key="item.id" :label="item.title" :value="item.id" />
    </el-select>
    <el-button type="primary" style="margin-left: 15px" @click="toExport">导出</el-button>
    <el-table
      border
      stripe
      :header-cell-style="{ background: '#f8fbfc' }"
      class="tableBorder"
      :data="tablelist"
    >
      <el-table-column prop="order_num" label="订单编号" width="210">
        <template slot-scope="{ row }">
          {{ row.order_num }}
        </template>
      </el-table-column>
      <el-table-column prop="created_at" align="center" label="下单时间" width="160" :show-overflow-tooltip="true" />
      <el-table-column label="所属政委" width="150" align="center">
        <template slot-scope="{ row: { get_user } }">
          {{ get_user && get_user.warea && get_user.warea.admin_name }}
        </template>
      </el-table-column>
      <el-table-column prop="get_user" label="备注昵称" align="center" width="150" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.get_user && row.get_user.name }}</template>
      </el-table-column>
      <el-table-column prop="get_user" label="下单人姓名" align="center" width="120" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.get_user && row.get_user.realname }}</template>
      </el-table-column>
      <el-table-column prop="status" align="center" label="订单状态" width="110" :show-overflow-tooltip="true">
        <template slot-scope="{ row: { is_deleted, status } }">
          <el-tag v-if="is_deleted === 1" type="info">已作废</el-tag>
          <el-tag v-else :color="orderStatus(status).color" style="color:#ffffff;">{{ orderStatus(status).name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="pay_status" align="center" label="支付状态" width="90" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">
          <span :style="`color: ${payStatus(row.pay_status).color}`">{{ payStatus(row.pay_status).name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="total" label="数量合计" align="center" width="80" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">
          {{ row.total }}
        </template>
      </el-table-column>
      <el-table-column prop="money" label="订单总额" align="center" width="120" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ `￥${row.money}` }}</template>
      </el-table-column>
      <el-table-column prop="belong_user" label="所属者昵称" align="center" width="120" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.belong_user && row.belong_user.nickname }}</template>
      </el-table-column>
      <el-table-column prop="belong_user" label="所属者姓名" align="center" width="120" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.belong_user && row.belong_user.realname }}</template>
      </el-table-column>
      <el-table-column prop="belong_user" label="所属者手机号" align="center" width="120" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.belong_user && row.belong_user.mobile }}</template>
      </el-table-column>
      <el-table-column prop="or_status" label="原因" align="center" width="160" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">{{ row.or_status? '所属者与下单人不是一个团队' : '所属者已被删除' }}</template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination :total="tableTotal" :limit="listQuery.page_size" :page="listQuery.page_index" @pagination="changePagination" @update:limit="changePagination" />
  </div>
</template>
<script>
// api
import { GetExceptionOrder, ExportExceptionOrder } from '@/api/order/examine'
import { activityList } from '@/api/giftB'
import { parseMoney } from '@/utils/index'
import { orderStatus, payStatus } from '@/filters/common'
import { formatter } from '@/filters/index'
export default {
  data() {
    return {
      loadingFull: false,
      listQuery: {
        page_index: 1,
        page_size: 10,
        activity_id: ''
      },
      tablelist: [],
      tableTotal: 0,
      activitys: []
    }
  },
  created() {
    this.orderStatus = orderStatus
    this.payStatus = payStatus
    this.parseMoney = parseMoney
    this.activityList(() => {
      this.getList()
    })
  },
  methods: {
    seclect() {
      this.getList()
    },
    async getList() {
      this.loadingFull = true
      await GetExceptionOrder(this.listQuery).then(res => {
        this.loadingFull = false
        if (res.code === 200) {
          this.tablelist = res.data.list
          this.tableTotal = res.data.total
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.loadingFull = false
      })
    },
    index(i) {
      const { page_index: a, page_size: b } = this.listQuery
      return (a - 1) * b + i + 1
    },
    // 分页页码变化
    changePagination(data) {
      const { page, limit } = data
      this.listQuery.page_size = limit
      this.listQuery.page_index = page
      this.getList()
    },
    // 导出
    async toExport() {
      this.loadingFull = true
      this.$request(ExportExceptionOrder({ activity_id: this.listQuery.activity_id }), (res) => {
        const exportData = res
        console.log(exportData)
        import('@/vendor/Export2Excel').then(excel => {
          const header = ['序号', '下单时间', '所属政委', '备注昵称', '下单人姓名', '订单状态', '支付状态', '数量合计', '订单总额', '所属者昵称', '所属者姓名', '所属者手机号', '原因']
          const exportK = ['index', 'updated_at', 'admin_name', 'name', 'realname', 'status', 'pay_status', 'total', 'money', 'belong_nickname', 'belong_realname', 'belong_mobile', 'or_status']
          const exportV = exportData.map((item, i) => {
            const obj = {
              index: i + 1,
              updated_at: item.updated_at || '',
              admin_name: item.get_user && item.get_user.warea && item.get_user.warea.admin_name,
              name: item.get_user && item.get_user.name || '',
              realname: item.get_user && item.get_user.realname || '',
              status: orderStatus(item.status).name || '',
              pay_status: payStatus(item.pay_status).name,
              total: item.total || '',
              money: item.money || '',
              belong_realname: item.belong_user && item.belong_user.realname,
              belong_nickname: item.belong_user && item.belong_user.nickname,
              belong_mobile: item.belong_user && item.belong_user.mobile,
              or_status: item.or_status ? '所属者与下单人不是一个团队' : '所属者已被删除'
            }
            return obj
          })
          const exportD = exportV.map(item => exportK.map(k => item[k]))
          excel.export_json_to_excel({
            header,
            data: exportD,
            filename: `${formatter(new Date(), 'yyyy-MM-dd')}异常订单`,
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
        this.loadingFull = false
      })
    },
    async activityList(cb) {
      this.loadingFull = true
      await activityList().then(res => {
        this.loadingFull = false
        if (res.code === 200) {
          this.activitys = res.data
          res.data.map(item => {
            if (item.is_ing === 1) {
              this.listQuery.activity_id = item.id
            }
          })
        }
        cb && cb()
      }).catch(e => {
        this.loadingFull = false
        cb && cb()
      })
    }
  }
}
</script>
