<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select
        v-model="listQuery.activityType" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请选择活动类型"
      >
        <el-option label="499" :value="1" />
        <el-option label="商圈" :value="2" />
        <el-option label="爆品" :value="3" />
        <el-option label="消费金" :value="4" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ ruleId: '' })"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="规则ID" prop="ruleId" fixed="left" />
        <el-table-column align="center" width="80" label="活动ID" prop="activityId" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" width="100" label="关联活动" prop="activityName" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" label="活动类型" prop="activityType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.activityType === 5" effect="plain">消费金</el-tag>
            <el-tag v-else-if="row.activityType === 6" effect="plain">组合活动</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="团长规则" prop="leadersRule">
          <template slot-scope="{ row }">
            <el-tag v-if="row.leadersRule === 1" type="success">比例</el-tag>
            <el-tag v-else-if="row.leadersRule === 2" type="warning">金额</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="直接团长规则" prop="leadersMoney" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="间接团长规则" prop="leadersInMoney" show-overflow-tooltip />
        <el-table-column align="center" label="合伙人规则" prop="partnerRule">
          <template slot-scope="{ row }">
            <el-tag v-if="row.partnerRule === 1" type="success">比例</el-tag>
            <el-tag v-else-if="row.partnerRule === 2" type="warning">金额</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="直接合伙人规则" prop="partnerMoney" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="间接合伙人规则" prop="partnerInMoney" show-overflow-tooltip />
        <el-table-column align="center" label="加盟商规则" prop="franchiseeRule">
          <template slot-scope="{ row }">
            <el-tag v-if="row.franchiseeRule === 1" type="success">比例</el-tag>
            <el-tag v-else-if="row.franchiseeRule === 2" type="warning">金额</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="加盟商佣金规则" prop="franchiseeMoney" show-overflow-tooltip />
        <el-table-column align="center" label="社区店规则" prop="communityRule">
          <template slot-scope="{ row }">
            <el-tag v-if="row.communityRule === 1" type="success">比例</el-tag>
            <el-tag v-else-if="row.communityRule === 2" type="warning">金额</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="社区店佣金规则" prop="communityMoney" show-overflow-tooltip />
        <el-table-column align="center" label="分佣来源" prop="sourceType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.sourceType === 1">订单金额</el-tag>
            <el-tag v-else-if="row.sourceType === 2">赠送代金券</el-tag>
            <el-tag v-else-if="row.sourceType === 3">消费金</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="220" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import { getAllPlatformBeeRule, deleteIdPlatformBeeRule } from '@/api/commissionAllocation/gradeCommission'

export default {
  name: 'GradeCommission',
  components: {
    EditModal,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        activityType: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getAllPlatformBeeRule(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleEdit(row) {
      this.$refs.EditModal && this.$refs.EditModal.handleOpen(row)
    },
    handleDelete(row) {
      this.$confirm('确定删除此项？')
        .then(async () => {
          await deleteIdPlatformBeeRule({ id: row.ruleId })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
