<template>
  <div>
    <el-dialog
      title="添加券包奖品"
      width="1000px"
      :visible.sync="dialogVisible"
      :before-close="close"
    >
      <div class="dialog-search">
        <div>券名称：</div>
        <el-input
          v-model="title"
          placeholder="请输入券包名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getCouponList"
        />
        <div class="ge"></div>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getCouponList"
          >查询</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="couponList"
        row-key="id"
        @selection-change="handleSelectionCouponPkg"
        class="el-table"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
          :reserve-selection="true"
        />
        <el-table-column label="券包编号" prop="id" />
        <el-table-column label="券包名称" prop="title" min-width="85" />
        <el-table-column label="优惠券数量" prop="couponNum" />
        <el-table-column label="总面值" min-width="85">
          <template slot-scope="scope">
            <div>￥{{ $numberFormat(scope.row.facePrice) }}</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="couponPkgTotal > 0"
        :total="couponPkgTotal"
        :page.sync="pageParams.pageNum"
        :limit.sync="pageParams.pageSize"
        @pagination="getCouponList"
      />
      <div class="dialog-btn">
        <el-button size="small" @click="close"> 取 消 </el-button>
        <div class="ge"></div>
        <el-button type="primary" size="small" @click="confirmCouponPkg">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common";
import { getCouponList } from "@/api/business/couponPkg";
export default {
  name: "CouponAdd",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      title: "", // 券包名称
      couponList: [], // 券包列表
      couponPkgTotal: 0, // 券包总数
      selectCouponPkgList: [], // 选中券包
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getCouponList();
  },
  methods: {
    // 券包列表
    getCouponList() {
      this.loading = true;
      getCouponList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        { title: this.title, status: 1 }
      ).then((res) => {
        this.couponList = res.rows
        this.couponPkgTotal = res.total;
        this.loading = false;
      });
    },

    // 选中券包
    handleSelectionCouponPkg(e) {
      this.selectCouponPkgList = e.map((item) => {
        return {
          prizeType: "coupon_pkg",
          // quantity: 1,
          id: item.id,
          picUrl: publicFileGetUrl + item.picUrl,
          title: item.title,
          sortWeight: 100
        };
      });
    },

    // 确认选中券包
    confirmCouponPkg() {
      if(!this.selectCouponPkgList.length) {
        this.msgInfo('请选择券包')
        return
      }
      this.$emit("confirmCouponPkg", this.selectCouponPkgList);
    },

    close() {
      this.$emit("close");
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-search {
  display: flex;
  line-height: 32px;
  margin-bottom: 20px;
  .ge {
    width: 40px;
  }
}
.dialog-btn {
  display: flex;
  align-content: center;
  justify-content: flex-end;
  padding: 40px 0 0;
  .ge {
    width: 40px;
  }
}
</style>
