<template>
  <div class="form-wrap">
    <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"
            :append-to-body="false"
            :editable="false"
            :clearable="false"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="截止时间"
          prop="timeEnd">
          <el-date-picker
            v-model="form.timeEnd"
            :append-to-body="false"
            :editable="false"
            :clearable="false"
            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">
      <!-- <div class="block-title">
        公共订单列表
      </div> -->

      <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
          label="操作"
          width="80">
          <template #default="scope">
            <el-button
              type="text"
              @click="scope && scope.row && showDialog('detail', scope.row, scope.$index)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-wrap">
      <el-pagination
        background
        :hide-on-single-page="false"
        :page-size="10"
        :current-page="form.pageNum"
        @current-change="handlePage"
        layout="prev, pager, next"
        :total="displayTotal">
      </el-pagination>
    </div>

    <div
      v-if="detailVisible"
      class="order-detail-mask"
      @click.self="detailVisible = false">
      <div class="order-detail-panel">
        <div class="order-detail-header">
          <span>订单详情</span>
          <button
            class="order-detail-close"
            type="button"
            @click="detailVisible = false">×</button>
        </div>
        <DetailDialog
          :order-item="currentItem"
          @close-dialog="detailVisible = false"></DetailDialog>
      </div>
    </div>
  </div>
</template>

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

export default {
  components: {
    DetailDialog
  },
  computed: {
    canAddUsers () {
      return true
      // return this.$store.state.user.permissionList.includes('system_settings:add_users')
    },
    displayTotal () {
      return this.total > 0 ? this.total : this.form.pageSize
    }
  },

  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, index) {
      const sourceItem = item || {}
      let fullItem = sourceItem
      if (typeof index === 'number' && this.tableData[index]) {
        fullItem = this.tableData[index]
      } else if (sourceItem.orderId) {
        fullItem = this.tableData.find(row => String(row.orderId) === String(sourceItem.orderId)) || sourceItem
      }
      this.currentItem = { ...fullItem }
      this[`${type}Visible`] = true
    },
    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('请选择起始时间和截止时间')
        this.loading = false
        return
      }
      const payload = {
        ...this.form,
        timeBegin: moment(this.form.timeBegin).format('YYYY-MM-DD HH:mm:ss'),
        timeEnd: moment(this.form.timeEnd).format('YYYY-MM-DD HH:mm:ss')
      }
      getOrderList(payload).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>
.pagination-wrap {
  display: flex;
  justify-content: center;
  padding: 10px 20px;
  background: #f2f6ff;
  border-radius: 4px;
}

.order-detail-mask {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
}

.order-detail-panel {
  width: 80%;
  max-height: 88vh;
  overflow: auto;
  background: #fff;
  border-radius: 4px;
}

.order-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid #ebeef5;
  font-size: 16px;
  color: #303133;
}

.order-detail-close {
  border: 0;
  background: transparent;
  color: #909399;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.order-detail-panel :deep(.dialog-btn-wrap) {
  display: flex;
  justify-content: flex-end;
  padding: 10px 16px;
  border-top: 1px solid #ebeef5;
}

</style>
