<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select
        v-model="listQuery.configType" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请选择活动配置类型"
      >
        <el-option label="升级活动" :value="1" />
        <el-option label="分佣活动" :value="2" />
        <el-option label="社区活动" :value="3" />
        <el-option label="赠券活动" :value="4" />
        <el-option label="赠金活动" :value="5" />
        <el-option label="商圈订单" :value="6" />
        <el-option label="爆品家具" :value="7" />
        <el-option label="社区订单" :value="8" />
        <el-option label="用户代金券转增" :value="9" />
        <el-option label="商家代金券转赠" :value="10" />
      </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({ configId: '' })"
      >
        添加
      </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="configId" fixed="left" />
        <el-table-column align="center" min-width="120" label="关联组合活动ID" prop="composeId">
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.composeId }}</span>
              <el-button
                v-if="row.composeId"
                style="margin-left: 14px;" type="warning" size="mini"
                @click="$refs.DetailModal2 && $refs.DetailModal2.handleOpen({ composeId: row.composeId })"
              >
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="110" label="活动配置类型" prop="configType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.configType === 1" effect="plain">升级活动</el-tag>
            <el-tag v-else-if="row.configType === 2" effect="plain">分佣活动</el-tag>
            <el-tag v-else-if="row.configType === 3" effect="plain">社区活动</el-tag>
            <el-tag v-else-if="row.configType === 4" effect="plain">赠券活动</el-tag>
            <el-tag v-else-if="row.configType === 5" effect="plain">赠金活动</el-tag>
            <el-tag v-else-if="row.configType === 6" effect="plain">商圈订单</el-tag>
            <el-tag v-else-if="row.configType === 7" effect="plain">爆品家具</el-tag>
            <el-tag v-else-if="row.configType === 8" effect="plain">社区订单</el-tag>
            <el-tag v-else-if="row.configType === 9" effect="plain">用户代金券转增</el-tag>
            <el-tag v-else-if="row.configType === 10" effect="plain">商家代金券转赠</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否参与" prop="joinRole">
          <template slot-scope="{ row }">
            <el-tag v-if="row.joinRole === 1" effect="plain">是</el-tag>
            <el-tag v-else-if="row.joinRole === 2" effect="plain" type="danger">否</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="规则" prop="joinRule">
          <template slot-scope="{ row }">
            <el-tag v-if="row.joinRule === 1" effect="plain">金额</el-tag>
            <el-tag v-else-if="row.joinRule === 2" effect="plain" type="danger">比例</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="升级类型" prop="roleType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.roleType === 1" effect="plain">团长</el-tag>
            <el-tag v-else-if="row.roleType === 2" effect="plain" type="danger">合伙人</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="100" label="参与金额" prop="joinMoney">
          <template slot-scope="{ row }">
            <span>
              ￥{{ typeof row.joinMoney === 'number' ? row.joinMoney : '--' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="100" label="关联佣金配置ID" prop="ruleId" show-overflow-tooltip />
        <el-table-column align="center" min-width="120" label="关联佣金配置ID" prop="ruleId">
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.ruleId }}</span>
              <el-button
                v-if="row.ruleId"
                style="margin-left: 14px;" type="warning" size="mini"
                @click="$refs.DetailModal1 && $refs.DetailModal1.handleOpen({ ruleId: row.ruleId })"
              >
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" width="150" label="更新时间" prop="updateTime" />
        <el-table-column align="center" label="操作" width="200" 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" />
    <!-- 佣金配置详情 -->
    <DetailModal1 ref="DetailModal1" />
    <!-- 组合活动详情 -->
    <DetailModal2 ref="DetailModal2" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import DetailModal1 from '../../commissionAllocation/gradeCommission/components/DetailModal'
import DetailModal2 from '../combinationActivities/components/DetailModal'
import { getPlatformComposeConfigPage, deletePlatformComposeConfig } from '@/api/active/combinationActivities'

export default {
  name: 'CombinationActivityConfiguration',
  components: {
    EditModal,
    DetailModal,
    DetailModal1,
    DetailModal2
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        configType: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPlatformComposeConfigPage(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 deletePlatformComposeConfig({ id: row.configId })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="less" 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>
