<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      机构交易管理
    </h2>
    <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      @submit="search"
      @reset="reset"
      :saveKey="'tenantTradeManager'"
      :model.sync="searchForm"
    >
      <el-form-item prop="transNo">
        <el-input
          placeholder="交易流水号"
          clearable
          type="text"
          v-model.trim="searchForm.transNo"
        ></el-input>
      </el-form-item>
      <el-form-item prop="orderNo">
        <el-input
          placeholder="订单号"
          clearable
          type="text"
          v-model.trim="searchForm.orderNo"
        ></el-input>
      </el-form-item>
      <el-form-item prop="tenantId" v-if="baseTenantId < 0">
        <!-- 小于0则是平台 -->
        <el-select
          v-model.trim="searchForm.tenantId"
          filterable
          multiple
          collapse-tags
          placeholder="机构名称"
          clearable
        >
          <el-option v-for="(item, index) in  tenantList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="orderType">
        <el-select
          v-model.trim="searchForm.orderType"
          clearable
          filterable
          @clear="onClear('orderType')"
          placeholder="交易类型"
        >
          <el-option
            v-for="(item, index) in tenantStatus"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="orderState">
        <el-select
          v-model.trim="searchForm.orderState"
          clearable
          filterable
          @clear="onClear('orderState')"
          placeholder="交易状态"
        >
          <el-option label="待支付" :value="0"></el-option>
          <el-option label="已支付" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="createTimer">
        <el-date-picker
          v-model.trim="searchForm.createTimer"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="入驻开始时间"
          end-placeholder="入驻结束时间"
          :picker-options="{ firstDayOfWeek: 1 }"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
        <el-button native-type="reset" type="primary">重置</el-button>
      </el-form-item>
    </save-form>
    <!-- 列表 -->
    <div class="tableWrap">
      <el-table
        :data="tableList"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
      >
        <el-table-column align="center" prop="transNo" label="交易流水号">
        </el-table-column>
        <el-table-column align="center" label="订单号" prop="orderNo">
        </el-table-column>
        <el-table-column align="center" label="订单时间" prop="payDate">
        </el-table-column>
        <el-table-column align="center" label="机构编号" prop="tenantId">
        </el-table-column>
        <el-table-column align="center" label="机构名称" prop="tenantName">
        </el-table-column>
        <el-table-column align="center" label="交易类型">
          <template slot-scope="scope">
            {{ scope.row.orderType | tenantStatus }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单金额">
          <template slot-scope="scope">
            {{ scope.row.expectAmount | moneyFormat }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="网络教室剩余时长" prop="productName">
        </el-table-column> -->
        <el-table-column align="center" label="支付金额">
          <template slot-scope="scope">
            {{ scope.row.actualAmount | moneyFormat }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="交易状态">
          <template slot-scope="scope">
            {{ scope.row.orderState | tenantOrderStatus }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="openService(scope.row)"
              v-permission="'tenantInfo/info'"
              type="text"
              >查看</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        :saveKey="'tenantTradeManager'"
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>

    <el-dialog title="订单详情" :visible.sync="orderVisible" width="1200px">
      <order-info />
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { tenantInfoQueryPage } from "../organManager/api";
import { tenantOrderRecordQueryPage } from "./api";
import { tenantStatus, dealStatus } from "@/utils/searchArray";
import OrderInfo from './model/orderInfo'
import { getTimes } from "@/utils";
const initSearch = {
  transNo: null,
  orderNo: null,
  tenantId: [],
  orderType: null,
  orderState: null,
  createTimer: [],
};
export default {
  components: { pagination, OrderInfo },
  data() {
    const baseTenantId = sessionStorage.getItem('baseTenantId')
    return {
      baseTenantId,
      tenantStatus,
      dealStatus,
      tableList: [],
      orderVisible: false,
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      searchForm: { ...initSearch },
      tenantList: []
    };
  },
  async mounted() {
    await this.getTenant()
    this.getList();
  },
  methods: {
    async getTenant() {
      try {
        const res = await tenantInfoQueryPage({ page: 1, rows: 999 }, )
        this.tenantList = res.data?.rows || []
      } catch(e) {}
    },
    async getList() {
      try {
        let { createTimer, tenantId, ...reset } = this.searchForm;
        const res = await tenantOrderRecordQueryPage({
          ...reset,
          tenantId: tenantId.join(','),
          ...getTimes(createTimer, ["startDate", "endDate"], "YYYY-MM-DD"),
          page: this.pageInfo.page,
          rows: this.pageInfo.limit,
        });
        this.pageInfo.total = res.total;
        this.tableList = res.rows;
      } catch (e) {}
    },
    search() {
      this.pageInfo.page = 1;
      this.$refs.searchForm.save(this.searchForm);
      this.$refs.searchForm.save(this.pageInfo, "page");
      this.getList();
    },
    reset() {
      this.searchForm = { ...initSearch };
      this.search();
    },
    openService(row) {
      this.orderVisible = true
    },
  },
  filters: {
    tenantOrderStatus(val) {
      const template = {
        0: "待支付",
        1: "已支付",
        2: "支付失败"
      }
      return template[val]
    }
  }
};
</script>
<style lang="scss" scoped>
.courseMask .el-dialog__body {
  padding-bottom: 0;
}
</style>
