<template>
  <div>
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="起始时间"
          prop="timeBegin">
          <el-date-picker
            v-model="form.timeBegin"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="截止时间"
          prop="timeEnd">
          <el-date-picker
            v-model="form.timeEnd"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="项目ID"
          prop="projectId">
          <el-select
            v-model="form.projectId"
            filterable
            clearable
            placeholder="请选择项目">
            <el-option
              label="全部"
              value=""></el-option>
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="订单号"
          prop="orderNo">
          <el-input
            v-model="form.orderNo"
            clearable></el-input>
        </el-form-item>
        <el-form-item
          label="合作方订单号"
          prop="partnerOrderNo">
          <el-input
            v-model="form.partnerOrderNo"
            clearable></el-input>
        </el-form-item>
        <el-form-item
          label="分销商"
          prop="otaSourceId">
          <el-select
            v-model="form.otaSourceId"
            filterable
            clearable
            placeholder="请选择分销商">
            <el-option
              label="全部"
              value=""></el-option>
            <el-option
              v-for="item in otaSourceList"
              :key="item.id"
              :label="item.otaSourceName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getOrderList">搜索</el-button>
      </div>
    </div>
    <div class="tableBox">
      <el-table
        v-loading="loading"
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="createTime"
          label="创建时间" />
        <el-table-column
          prop="orderNo"
          label="订单号" />
        <el-table-column
          prop="partnerOrderNo"
          label="合作方订单号" />
        <el-table-column
          prop="projectName"
          label="项目名称" />
        <el-table-column
          prop="otaSourceCode"
          label="分销商编码" />
        <el-table-column
          prop="otaSourceName"
          label="分销商名称" />
        <el-table-column
          fixed="right"
          label="操作"
          width="80">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showDialog('detail',scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>

    <ElDialog
      title="订单详情"
      width="80%"
      v-model="detailVisible">
      <DetailDialog :order-item="currentItem"></DetailDialog>
    </ElDialog>
  </div>
</template>

<script>
import { getProjectList, getOrderList, getOtaSourceList } from '@/api/platform'
import moment from 'moment'
import ElDialog from '@/components/Dialog'
import DetailDialog from './DetailDialog'

export default {
  name: 'OrderList',
  components: {
    ElDialog,
    DetailDialog
  },
  created () {
    this.getProjectList()
    this.getOtaSourceList()
    this.getOrderList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      tableData: [],
      otaSourceList: [],
      projectList: [],
      total: 0,
      form: {
        timeBegin: moment().subtract(1, 'months').startOf('day'),
        timeEnd: moment().endOf('day'),
        projectName: '',
        projectId: '',
        otaSourceId: '',
        orderNo: '',
        partnerOrderNo: '',
        pageNum: 1,
        pageSize: 10
      },
      currentItem: {},
      detailVisible: false
    }
  },
  methods: {
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = item
    },
    getProjectList () {
      getProjectList({
        pageSize: -1,
        pageNum: 1
      }).then(res => {
        this.projectList = res.data.records || []
      })
    },
    getOrderList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      if (!this.form.timeBegin || !this.form.timeEnd) {
        this.$message.error('请选择起始时间和截止时间')
        return
      }
      if (this.form.timeBegin) {
        this.form.timeBegin = moment(this.form.timeBegin).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.form.timeEnd) {
        this.form.timeEnd = moment(this.form.timeEnd).format('YYYY-MM-DD HH:mm:ss')
      }
      getOrderList(this.form).then(res => {
        this.loading = false
        this.tableData = res.data.records || []
        this.total = res.data.total || 0
      })
    },
    getOtaSourceList () {
      getOtaSourceList({ pageSize: -1, pageNum: 1 }).then(res => {
        this.otaSourceList = res.data.records || []
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getOrderList()
    }
  }
}
</script>

<style scoped>

</style>
