/* eslint-disable no-dupe-keys */
<template>
  <div v-loading="loading">
    <div class="heior_box">
      <el-button
        size="small"
        type="primary"
        icon="el-icon-sold-out"
        @click="dataExport"
        v-if="$local.isRoot()"
      >导出</el-button>
      <div class="search-box">
        <!-- <el-cascader
         placeholder="选择产品/套餐"
         size="small"
        v-model="searchModel.pModel"
          :options="options"
          :props="{ checkStrictly: true}"
          clearable
        ></el-cascader> -->
        <el-select
          v-model="searchModel.ProductIds"
          multiple
          collapse-tags
          style="margin-left: 10px;"
          size=small
          placeholder="请选择产品">
          <el-option
            v-for="item in products"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-select
          v-model="searchModel.PackageNames"
          multiple
          collapse-tags
          style="margin-left: 10px;"
          size=small
          placeholder="请选择套餐">
          <el-option
            v-for="item in packages"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-select v-model="searchModel.ProductId" placeholder="选择产品" clearable size="small">
          <el-option v-for="item in products" :key="item.Name" :label="item.Name" :value="item.Id"></el-option>
        </el-select> -->
        <el-select
          v-model="searchModel.OrderTypes"
          multiple
          collapse-tags
          clearable
          size="small"
          style="margin-left: 10px;"
          placeholder="选择类型"
        >
          <el-option v-for="item in types" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
        <el-date-picker
          size="small"
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="margin-left: 10px;"
        ></el-date-picker>
        <el-input
          v-model="searchModel.keyWord"
          placeholder="请输入订单号、订单名称、用户名"
          clearable
          style="margin-left: 10px;"
          size="small"
        >
          <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
        </el-input>
      </div>
    </div>
    <el-table :data="retData.Data" row-key="Id" border size="medium" :height="$getHeight(100)">
      <!-- <el-table-column prop="CreateTime" label="创建日期" width="180"></el-table-column> -->
      <el-table-column prop="UpdateTime" label="支付日期" ></el-table-column>
      <el-table-column prop="OrderNo" label="订单号"></el-table-column>
      <el-table-column prop="OrderType" label="类型" :formatter="typeFormat"></el-table-column>
      <el-table-column prop="UserName" label="用户">
        <template slot-scope="scope">
          <phoneHide :text="scope.row.UserName"></phoneHide>
        </template>
      </el-table-column>
      <el-table-column prop="ProductName" label="产品"></el-table-column>
      <el-table-column prop="PackageName" label="套餐"></el-table-column>
      <el-table-column prop="DayPrice" label="单价"></el-table-column>
      <el-table-column prop="ConnectCount" label="连接数">
        <template slot-scope="scope">{{scope.row.ConnectCount*scope.row.AccountCount}}</template>
      </el-table-column>
      <el-table-column prop="Accounts" label="账号">
        <template slot-scope="scope">
          <cutTip :text="scope.row.Accounts"></cutTip>
        </template>
      </el-table-column>
      <el-table-column prop="OrderAmount" label="订单金额"></el-table-column>
      <el-table-column prop="CouponAmount" label="优惠金额"></el-table-column>
      <el-table-column prop="AccountPayAmount" label="余额支付"></el-table-column>
      <el-table-column prop="OtherPayAmount" label="在线支付"></el-table-column>
      <el-table-column prop="PaymentAmount" label="实付"></el-table-column>
      <!-- <el-table-column prop="RefundAmount" label="退款"></el-table-column> -->
      <el-table-column prop="PayType" label="付款方式" :formatter="payTypeFormat"></el-table-column>
      <el-table-column prop="TradeNo" label="支付流水号">
        <template slot-scope="scope">
          <cutTip :text="scope.row.TradeNo"></cutTip>
        </template>
      </el-table-column>
      <el-table-column prop="Remark" label="备注" >
        <template slot-scope="scope">
          <cutTip :text="scope.row.Remark"></cutTip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        v-if="retData.Data.length>0"
        @current-change="get"
        :current-page.sync="searchModel.PageIndex"
        :page-size="50"
        layout="total,prev, pager, next"
        :total="retData.TotalCount"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: 'treeTable',
  components: {
    cutTip: () => import('@/components/common/cutTip'),
    phoneHide: () => import('@/components/common/phonehide')
  },
  data () {
    return {
      loading: false,
      options: [],
      types: [
        { text: '新开', value: '1' },
        { text: '批量新开', value: '2' },
        { text: '续费', value: '3' },
        { text: '批量续费', value: '4' }
      ],
      retData: { TotalCount: 0, Data: [] },
      dateRange: [],
      products: [],
      packages: [
        {
          label: '年卡',
          value: '年卡'
        }, {
          label: '季卡',
          value: '季卡'
        }, {
          label: '月卡',
          value: '月卡'
        }, {
          label: '周卡',
          value: '周卡'
        }, {
          label: '天卡',
          value: '天卡'
        }, {
          label: '测试卡',
          value: '测试卡'
        }, {
          label: '双月卡',
          value: '双月卡'
        }, {
          label: '双月卡(活动)',
          value: '双月卡(活动)'
        }, {
          label: '月卡(活动)',
          value: '月卡(活动)'
        }, {
          label: '季卡(活动)',
          value: '季卡(活动)'
        }
      ],
      searchModel: {
        PageIndex: 1,
        ProductIds: [],
        PackageNames: [],
        keyWord: '',
        Status: 0,
        PayType: 0,
        OrderTypes: [],
        Btime: '',
        Etime: '',
        pModel: []
      },
      types: [
        { text: '新开', value: 1 },
        { text: '批量新开', value: 2 },
        { text: '续费', value: 3 },
        { text: '批量续费', value: 4 },
        { text: '老账号认证', value: 100 },
        { text: '测试', value: 200 }
      ],
      typeFormat (row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '新开'
          case 2:
            return '批量新开'
          case 3:
            return '续费'
          case 4:
            return '批量续费'
        }
        return ''
      },
      payTypeFormat (row, column, cellValue, index) {
        switch (cellValue) {
          case 10:
            return '余额'
          case 70:
            return '微信'
          case 100:
            return '支付宝'
        }
        return ''
      }
    }
  },
  created () {
    this.$store.commit('setCurrentNav', '订单管理>>订单明细')
    this.get()
    this.getProduct()
    // this.getProductWithPackage()
  },
  methods: {
    search () {
      this.searchModel.PageIndex = 1
      this.get()
    },
    get () {
      if (this.dateRange && this.dateRange.length === 2) {
        this.searchModel.Btime = this.dateRange[0]
        this.searchModel.Etime = this.dateRange[1]
      }
      this.$api.get('course/v1/order/page', this.searchModel).then(res => {
        this.retData = res
      })
    },
    getProduct () {
      this.loading = true
      this.$api.get('course/v1/product/page', this.searchModel).then(res => {
        res.Data.forEach(m => {
          var product = {
            label: m.Name,
            value: m.Id
          }
          this.products.push(product)
        })
        this.loading = false
      })
      this.loading = false
    },
    getProductWithPackage () {
      this.loading = true
      this.$api.get('course/v1/product/ProductWithPackage').then(res => {
        var options = []
        res.Data.forEach(m => {
          var product = {
            label: m.Product.Name,
            value: m.Product.Id
          }
          this.products.push(product)

          m.Packages.forEach(p => {
            var pkg = {
              label: p.Name,
              value: p.Id
            }
            this.pa.push(pkg)
          })
          options.push(product)
        })
        this.options = options
        this.loading = false
      })
    },
    filterHandler (filters) {
      console.log(filters)
      var values = Object.values(filters)
      console.log(...values)
      values.forEach(m => {
        this.searchModel.OrderTypes.push(...m)
      })
      this.get()
    },
    dataExport () {
      if (this.dateRange && this.dateRange.length === 2) {
        this.searchModel.Btime = this.dateRange[0]
        this.searchModel.Etime = this.dateRange[1]
      }
      if(this.searchModel.ProductIds.length>0){
        this.searchModel.ProductId = this.searchModel.ProductIds[0]
      }
      this.$api.getDownloadFile('/course/v1/order/Export', this.searchModel)
    }
  }
}
</script>
<style scoped>
</style>
