<template>
  <div>
    <el-dialog
      title="添加券"
      width="1000px"
      :visible.sync="dialogVisible"
      :before-close="close"
    >
      <div class="dialog-search">
        <div>券名称：</div>
        <el-input
          v-model="couponTitle"
          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"
        @selection-change="handleSelectionCoupon"
        class="el-table"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <!--  -->
        <el-table-column label="券ID" prop="couponId" width="60" />
        <el-table-column label="券图片" align="center">
          <template slot-scope="scope">
            <div>
              <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.picUrl"
                :preview-src-list="[scope.row.picUrl]"
              >
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="券名称" prop="title" min-width="85" />
        <el-table-column label="使用场景" min-width="85">
          <template slot-scope="scope">
            <div>{{ scope.row.type.desc }}</div>
          </template>
        </el-table-column>
        <el-table-column label="券价格" min-width="85">
          <template slot-scope="scope">
            <div>￥{{ $numberFormat(scope.row.discount) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="门店承担比例" prop="channelSharedRate">
          <template slot-scope="{row}">
            {{row.channelSharedRate}}%
          </template>
        </el-table-column>
        <el-table-column label="有效期限" min-width="85">
          <template slot-scope="scope">
            <div>领取后{{ scope.row.dueDays }}天有效</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="couponTotal > 0"
        :total="couponTotal"
        :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="confirmCoupon">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common";
import { getCouponList } from "@/api/business/coupon";
export default {
  name: "CouponAdd",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    ids: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      couponTitle: "", // 券名称
      couponList: [], // 卡券列表
      couponTotal: 0, // 卡券总数
      selectCouponList: [], // 选中卡券
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getCouponList();
  },
  methods: {
    // 卡券列表
    getCouponList() {
      this.loading = true;
      getCouponList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        { title: this.couponTitle, status: "on", excludeCouponIds: this.ids }
      ).then((res) => {
        this.couponList = res.rows.map((item) => {
          return {
            ...item,
            type: JSON.parse(item.type),
            useArea: JSON.parse(item.useArea),
            picUrl: publicFileGetUrl + item.picUrl,
          };
        });
        this.couponTotal = res.total;
        this.loading = false;
      });
    },

    // 选中卡券
    handleSelectionCoupon(e) {
      this.selectCouponList = e.map((item) => {
        return {
          couponNum: 1,
          couponId: item.couponId,
          title: item.title,
          createdTime: item.createdTime,
          updatedTime: item.updatedTime,
          type: item.type,
          discount: item.discount,
          dueDays: item.dueDays,
          channelSharedRate: item.channelSharedRate
        };
      });
    },

    // 确认选中卡券
    confirmCoupon() {
      this.$emit("confirmCoupon", this.selectCouponList);
      this.close()
    },

    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>
