<template>
  <div class="divBox">
    <!--主要内容-->
    <el-card class="box-card">
      <!--顶部搜索栏-->
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small" label-width="80px">
            <el-form-item label="">
              <el-input v-model="tableFrom.title" placeholder="请输入等级名称" clearable>
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)"/>
              </el-input>
            </el-form-item>
          </el-form>
          <el-button size="small" type="success" @click="editInfo">添加等级</el-button>
        </div>
      </div>
      <!--表格信息-->
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini">
        <el-table-column label="ID" prop="id" min-width="80" align="center"/>
        <el-table-column label="名称" prop="title" min-width="150" align="center"/>
        <el-table-column label="权重" prop="weight" min-width="80" align="center"/>
        <el-table-column label="数量限制" min-width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.mer_quota > 0 ? scope.row.mer_quota : '不限' }}
          </template>
        </el-table-column>
        <el-table-column label="支付金额" min-width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.price > 0 ? scope.row.price : '免费' }}
          </template>
        </el-table-column>
        <el-table-column label="成为股东赠送相关额度" align="center">
          <el-table-column label="瓶装酒额度" prop="quota" min-width="80" align="center"/>
          <el-table-column label="菜卡额度" prop="vegetable_quota" min-width="80" align="center"/>
          <el-table-column label="油卡额度" prop="oil_quota" min-width="80" align="center"/>
          <el-table-column label="封坛酒额度" prop="wine_quota" min-width="80" align="center"/>
          <el-table-column label="优惠券" min-width="80" align="center">
            <template slot-scope="scope">
              {{ scope.row.coupon_ids ? Object.values(scope.row.coupon_ids.split(',')).length : '0' }}张
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作" min-width="100" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editInfo({id: scope.row.id})">编辑</el-button>
            <el-button type="text" size="small" @click="delInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination :page-size="tableFrom.limit" :current-page="tableFrom.page" :total="tableData.total" @current-change="pageChange" />
      </div>
    </el-card>

  </div>
</template>

<script>
import {merchantTypeText} from "@/filters";
import {merShareholderDelForm, merShareholderEditForm, merShareholderList} from "@/api/merchant";

export default {
  name: "MerchantShareholderSet",
  components: {},
  data() {
    return {
      merchantTitle: '商户',
      // 列表
      listLoading: false,
      tableFrom: {
        page: 1,
        limit: 20,
        title: '',
        merchant_type: '',
      },
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  watch: {
    // 路由变更
    '$route' () {
      this.tableFrom.merchant_type = this.$route.meta.merchant_type || 0;
    },
    // 商户类型变更
    'tableFrom.merchant_type' () {
      this.merchantTitle = merchantTypeText(this.tableFrom.merchant_type);
      this.getList();
    },
  },
  mounted() {
    this.tableFrom.merchant_type = this.$route.meta.merchant_type || 0;
  },
  methods: {
    // 列表
    getList(num = ''){
      let _this = this;
      _this.listLoading = true;
      _this.tableFrom.page = num ? num : _this.tableFrom.page;
      merShareholderList(_this.tableFrom).then((res) => {
        _this.tableData.data = res.data.list;
        _this.tableData.total = res.data.count;
        _this.listLoading = false;
      }).catch((res) => {
        _this.listLoading = false;
        _this.$message.error(res.message);
      });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    // 添加 || 编辑
    editInfo(data = {}){
      data.merchant_type = this.tableFrom.merchant_type;
      this.$modalForm(merShareholderEditForm(data)).then(() => this.getList(''));
    },
    // 删除
    delInfo(id){
      let _this = this;
      this.$confirm('删除后不可恢复，您确认删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '不删除',
        type: 'warning'
      }).then(() => {
        merShareholderDelForm(id).then((res) => {
          _this.getList('')
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        }).catch((res) => {
          console.log('错误',res)
        });
      }).catch(action => {})
    },



  }
};
</script>

<style scoped lang="scss">
/deep/ .el-select{
  width: 100% !important;
}
/deep/ .cell{
  padding: 0!important;
}
</style>
