<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="'创建日期'">
              <el-date-picker
                v-model="queryParams.times1"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
                @change="checkTime1"
              />
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch">
                {{ '搜索' }}</el-button>
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            v-has-buttons="['BalanceSumExport']"
            size="mini"
            style="background: #ffad41; border-color: #ffad41"
            class="addBtn"
            @click="handleExport"
          >{{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          :header-cell-style="{ background: '#EEEEEE' }"
          height="700px"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />

          <el-table-column
            min-width="100px"
            align="center"
            prop="createTime"
            :label="'日期'"
          />
          <el-table-column
            v-if="showData.account1"
            align="center"
            prop="account1"
            min-width="100px"
            :label="'消费账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account1 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account2"
            align="center"
            prop="account2"
            :label="'现金账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account2 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account3"
            align="center"
            prop="account3"
            :label="'政策账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account3 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account4"
            align="center"
            prop="account4"
            :label="'奖金账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account4 | numberToCurrency }}
            </template>
          </el-table-column>

          <el-table-column
            v-if="showData.account5"
            align="center"
            prop="account5"
            :label="'重消账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account5 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account6"
            align="center"
            prop="account6"
            :label="'积分账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account6 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account7"
            align="center"
            prop="account7"
            :label="'鼓励账户余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account7 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account8"
            align="center"
            prop="account8"
            :label="'复购券余额' + `(${isLocalSymbol()})`"
          >
            <template slot-scope="scope">
              {{ scope.row.account8 | numberToCurrency }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account9"
            align="center"
            prop="account9"
            :label="'全球积分余额' + `(${isLocalSymbol()})`"
          ><template slot-scope="scope">
            {{ scope.row.account9 | numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account10"
            align="center"
            prop="account10"
            :label="'车奖积分余额' + `(${isLocalSymbol()})`"
          ><template slot-scope="scope">
            {{ scope.row.account10 | numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account11"
            align="center"
            prop="account11"
            :label="'直播代金券余额' + `(${isLocalSymbol()})`"
          ><template slot-scope="scope">
            {{ scope.row.account11 | numberToCurrency }}
          </template>
          </el-table-column>
          <el-table-column
            v-if="showData.account12"
            align="center"
            prop="account12"
            :label="'乐学代金券余额' + `(${isLocalSymbol()})`"
          ><template slot-scope="scope">
            {{ scope.row.account12 | numberToCurrency }}
          </template>
          </el-table-column>

          <el-table-column align="center" prop="time" :label="'操作'">
            <template slot-scope="scope">
              <el-button
                type="text"
                style="color: #48b2fd"
                size="small"
                @click="exportRow(scope.row)"
              >
                {{ '导出' }}
              </el-button>
              <el-button
                type="text"
                style="color: #ed1d25; margin-left: 0; margin: 0"
                size="small"
                @click="goDetail(scope.row)"
              >
                {{ '查看' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import { isLocals, isLocalSymbol } from '@/utils/numberToCurrency'
import * as fin from '@/api/financial.js'
import { mapGetters } from 'vuex'
export default {
  name: 'BalanceSummary',
  components: {
    topBar
  },
  data() {
    return {
      moren: 'balanceSummary',
      topList: [
        {
          name: '余额汇总',
          path: 'balanceSummary'
        }
      ],
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      total: 0,
      multipleSelection: [],
      showData: {}
    }
  },
  computed: {
    ...mapGetters(['getUser'])
  },
  created() {
    this.getOthers()
    this.getSearch()
  },
  methods: {
    isLocals,
    isLocalSymbol,
    goDetail(row) {
      this.$router.push({
        path: 'detailSummary',
        query: {
          createTime: row.createTime
        }
      })
    },
    getOthers() {
      fin.getBalanceSummaryField().then((res) => {
        const obj = {}
        res.data.forEach((ele) => {
          obj[ele] = true
        })
        this.showData = obj
      })
    },
    // 导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((_) => {
        this.download(
          '/member/manager/account-detail/export',
          { ...this.queryParams },
          `余额汇总-${new Date().getTime()}.xlsx`
        )
      })
    },
    exportRow(row) {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((_) => {
        this.download(
          '/member/manager/account-detail/export',
          { ...row },
          `余额汇总详情-${new Date().getTime()}.xlsx`
        )
      })
    },
    checkTime1(val) {
      this.queryParams.startDate = val[0]
      this.queryParams.endDate = val[1]
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.queryParams.createTimes = val.map((item) => item.createTime)
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        type: 1
      }
      this.getSearch()
    },
    getSearch() {
      fin.getBalanceSummaryList(this.queryParams).then((res) => {
        res.rows.forEach((item) => {
          item.pkCountryName = this.getUser.country.shortName
          item.pkCountryIcon = this.getUser.country.nationalFlag2
        })
        this.tableData = res.rows
        this.total = res.total
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  height: 600px !important;
}
.tem {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 28px;
    height: 20px;
    margin-right: 3px;
  }
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    background: #f9f9f9;

    .maintop {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>
