<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.programmeName" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请输入定价方案名称"
      />
      <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({ priceId: '' })"
      >
        添加
      </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="priceId" fixed="left" />
        <el-table-column align="center" width="150" label="方案名称" prop="programmeName" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="优惠定价系数" prop="preferentialPrice" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="市场价定价系数" prop="marketPrice" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="供货价定价系数" prop="supplyPrice" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="渠道价定价系数" prop="channelPrice" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="活动开始时间" prop="startTime" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="活动结束时间" prop="endTime" show-overflow-tooltip />
        <el-table-column align="center" label="状态" prop="status">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 0" effect="plain" type="info">未开始</el-tag>
            <el-tag v-else-if="row.status === 1" effect="plain" type="success">进行中</el-tag>
            <el-tag v-else-if="row.status === 2" effect="plain" type="danger">已结束</el-tag>
            <el-tag v-else-if="row.status === 3" effect="plain" type="warning">已停用</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="类型" prop="type">
          <template slot-scope="{ row }">
            <el-tag v-if="row.type === 0" effect="plain" type="info">平台</el-tag>
            <el-tag v-else-if="row.type === 1" effect="plain" type="success">团长</el-tag>
            <el-tag v-else-if="row.type === 2" effect="plain" type="danger">合伙人</el-tag>
            <el-tag v-else-if="row.type === 3" effect="plain" type="warning">加盟商</el-tag>
            <el-tag v-else-if="row.type === 4" effect="plain">代理商</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否默认选择" prop="isDefault">
          <template slot-scope="{ row }">
            <el-tag v-if="row.isDefault === 0" effect="plain" type="info">否</el-tag>
            <el-tag v-else-if="row.isDefault === 1" effect="plain" type="success">是</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="300" 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 { priceControlGetPage, priceControlDeletePlan } from '@/api/selectionCenter/selectionCenter'

export default {
  name: 'DistributionPricingPlan',
  components: {
    EditModal,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        programmeName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await priceControlGetPage(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 priceControlDeletePlan({ id: row.priceId })
          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>
