<template>
  <div class="app-container">
    <div class="title-container">
      <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
    </div>
    <y-page-list-layout :page-list="pageData.records" :page-para="listQuery" :get-page-list="getList">
      <template slot="left">
        <el-input
          v-model="listQuery.userName"
          placeholder="人员名称"
          clearable
          style="margin-left: 20px;width: 320px;float: left;"
        >
        </el-input>
        <el-select
          v-model="listQuery.cate"
          placeholder="结算类型"
          clearable
          filterable
          style="margin-left: 20px;width: 150px;float: left;"
          class="filter-item"
        >
          <el-option label="项目结算" value="ITEM_SETTLE"/>
          <el-option label="默认预提" value="DEFAULT_AHEAD"/>
        </el-select>
        <el-button
          class="filter-item"
          style="margin-left: 10px;float: left;"
          type="primary"
          @click="getList"
          round
        >搜索
        </el-button>
        <el-button
          class="filter-item"
          style="float: left;"
          round
          type="warning"
          @click="resetSearch()"
        >重置
        </el-button>
      </template>
      <parentTable
        v-loading="listLoading"
        :data="pageData"
        slot="table"
        style="width: 100%;"
        :showSummary="true"
        :summary-method="getSummaries"
      >
        <el-table-column label="人员名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结算总金额(元)" align="center">
          <template slot-scope="{row}">
            <p v-html="getAmount(row)"></p>
          </template>
        </el-table-column>
        <el-table-column label="结算类型" align="center">
          <template slot-scope="{row}">
            <span>{{settleCate(row.cate)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="提成体现" align="center">
          <template slot-scope="{row}">
            <span>{{ row.embody}}</span>
          </template>
        </el-table-column>
        <el-table-column label="结算时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.created}}</span>
          </template>
        </el-table-column>
      </parentTable>
    </y-page-list-layout>
  </div>
</template>
<script>
  import YPageListLayout from '@/components/YPageListLayout'
  import Breadcrumb from '@/components/Breadcrumb'
  export default {
    name: 'ViewsBrokerageDeduction',
    components: {
      Breadcrumb,
      YPageListLayout,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        isDisable:false,
        tableKey: 0,
        pageData: { records: [] },
        details:[],
        total: 20,
        listLoading: true,
        listQuery: {
          page: 1,
          size: 10,
          descs: 'id',
        },
        listQueryKey: 'keyword',
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getAmount(row){
        const amount = row.brokerageAmount;
        if (row.brokerageAmount<0){
          return '<span style="color: red">'+amount+'</span>'
        }else{
          return '<span style="color: green">'+amount+'</span>'
        }
      },
      getSummaries(){
        const param = this.pageData;
        const sums = [];
        param.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index === 1){
            const values = param.map(item => Number(item.brokerageAmount));
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0);
              sums[index] += '';
            } else {
              sums[index] = 'N/A';
            }
          }else {
            sums[index] = '-';
          }
        });
        return sums;
      },
      brokerageRule(val){
        if (val === 'LAND_OTHER_RULE'){
          return '参与人员规则';
        }
        if (val === 'LAND_MARKETER_RULE'){
          return '市场人员规则';
        }
      },
      brokerageMode(val){
        if (val === 'PERSONAL'){
          return '个人提成';
        }
        if (val === 'TEAM_SHARE'){
          return '团队抽成';
        }
      },
      itemCate(val){
        if (val === 'AHEAD'){
          return '预提';
        }
        if (val === 'SETTLE'){
          return '结算';
        }
      },
      resetSearch() {
        this.listQuery = {
          current: 1,
          size: 10,
          descs: 'id',
        };
        this.getList()
      },
      settleCate(val){
        if (val === 'DEFAULT_AHEAD'){
          return '默认预提';
        }
        if (val === 'ITEM_SETTLE'){
          return '项目结算';
        }
      },
      getList() {
        const that = this;
        this.listLoading = true;
        this.$api.itemBrokerageSequence.deduction(Object.assign({}, that.listQuery)).then((res) => {
          that.pageData = res.data;
          setTimeout(() => {
            that.listLoading = false
          }, 200);
        })
          .catch(() => {
            that.listLoading = false
          })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .right {
    flex: 1;

    .title {
      font-size: 16px;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      line-height: 35px;
      margin-bottom: 8px;
    }

    .menu-2-box {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
    }

    .menu-2-item {
      display: flex;
      align-items: center;
      color: #656565;
      font-size: 12px;
      width: 230px;
      height: 101px;
      background: rgb(255, 185, 129);
      border-radius: 3px;
      padding-left: 20px;
      margin-right: 10px;
      margin-bottom: 10px;
      cursor: pointer;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);

      .text {
        margin-left: 16px;
      }
    }
  }
  /deep/ .el-table{
    display: flex;
    flex-direction: column;
  }
  /deep/ .el-table__body-wrapper{
    order: 1;
  }

</style>
