<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="startDate">
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="交易类型"
        prop="keyWords1">
        <el-select v-model="form.keyWords1">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in tradeType"
            :key="item.value"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label-width="120px"
        :label="agencyName()"
        prop="keyWords2">
        <el-select v-model="form.keyWords2">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in agencyList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item
        label="支付方式"
        prop="keyWords3">
        <el-select v-model="form.keyWords3">
          <el-option
            v-for="item in payChannelOptions"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item> -->

      <div class="btn-wrap">
        <el-button @click="reset()">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList(true)">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        :data="tableData"
        v-loading="loading">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          :label="`${agencyName()}名称`"
          prop="travelAgencyName">
        </el-table-column>
        <el-table-column
          label="消费类型"
          prop="type">
          <template slot-scope="scope">
            <el-tag :type="getType(scope.row.type).color">
              {{ getType(scope.row.type).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="金额"
          prop="amount">
          <template slot-scope="scope">
            <span :class="getType(scope.row.type).color">{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="流水号"
          prop="trade_no">
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remark">
        </el-table-column>
        <el-table-column
          label="操作员"
          prop="manager_name">
        </el-table-column>
        <el-table-column
          label="操作时间"
          prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime | formatTime }}
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>

    <ElDialog
      title="统计详情"
      width="760px"
      v-model="dialogVisible">
      <DetailDialog
        :info="currentItem"
      ></DetailDialog>
    </ElDialog>
  </div>
</template>

<script>
import { getTravelAgencyPrepaymentLog } from '@/api/travelAgency'
import ElDialog from '@/components/Dialog'
import DetailDialog from './guideStatistic/DetailDialog'
import moment from 'moment'

const tradeType = [
  {
    label: '充值',
    value: 1,
    color: 'success'
  },
  {
    label: '消费',
    value: 2,
    color: 'danger'
  },
  {
    label: '退款',
    value: 3,
    color: 'primary'
  },
  {
    label: '扣款',
    value: 4,
    color: 'warning'
  }
]

export default {
  components: {
    ElDialog,
    DetailDialog
  },
  computed: {
    agencyList () {
      return this.$store.state.app.agencyList
    },
    payChannelOptions () {
      return this.$store.state.app.payChannelOptions
    }
  },
  data () {
    return {
      tradeType,
      loading: false,
      total: 0,
      form: {
        pageSize: 10,
        pageNum: 1,
        startDate: new Date(moment().format('YYYY-MM-DD') + ' 00:00:00'),
        endDate: new Date(moment().format('YYYY-MM-DD') + ' 23:59:59'),
        keyWords1: '',
        keyWords2: '',
        keyWords3: ''
      },
      ticketTypeList: [],
      tableData: [],
      currentItem: {},
      dialogVisible: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getType (value) {
      return tradeType.find(i => i.value === value)
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取数据
    getList (goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)
      getTravelAgencyPrepaymentLog(this.form).then(res => {
        this.tableData = res.list
        this.total = res.total
      }).finally(() => {
        this.loading = false
      })
    },
    showDetail (item) {
      this.currentItem = item
      this.dialogVisible = true
      console.log(item)
    }
  }
}
</script>

<style scoped>
.warning{
  color:#e6a23c
}
</style>
