<template>

  <!-- 对话框(添加 / 修改) -->
  <el-drawer
    title="选择卡券"
    size="60%"
    :visible.sync="drawVisible"
    @close="cancelForm"
  >
    <div style="padding: 0 10px;margin-bottom: 70px">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>{{ formData.name }}</span>
        </div>
        <el-row :gutter="12">
          <el-col v-for="item in formData.couponList" :span="6">
            <el-card shadow="hover">
              {{ item.name }}
            </el-card>
          </el-col>
        </el-row>
      </el-card>

      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="color: red">点击确定后，将{{formData.name}}的附属权益，复制到下列选中的目标卡券中</span>
        </div>
        <!-- 搜索工作栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="卡券名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入卡券名称" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="卡券类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择卡券类型" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_TYPE)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="核销规则" prop="outRule">
            <el-select v-model="queryParams.type" placeholder="请选择核销规则" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_OUT_RULE)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 操作工具栏 -->
        <el-table
          ref="multipleTable"
          height="460"
          v-loading="formLoading"
          :data="list"
          :stripe="true"
          @select="selectRow"
          @select-all="handleSelectAll"
          :show-overflow-tooltip="true">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="卡券名称" width="250" align="left" prop="name"/>
          <el-table-column label="卡券类型" width="80" align="left" prop="type">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.MEMBER_COUPON_TYPE" :value="scope.row.type"/>
            </template>
          </el-table-column>
          <el-table-column label="适用车型" width="130" align="center" prop="carModel">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.MEMBER_CAR" :value="scope.row.carModel"/>
            </template>
          </el-table-column>
          <el-table-column label="开始有效期" align="center" prop="beginTime" width="150">
            <template v-slot="scope">
              <span>{{ parseTime(scope.row.beginTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="结束有效期" align="center" prop="endTime" width="150">
            <template v-slot="scope">
              <span>{{ parseTime(scope.row.endTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="面额" width="100" align="right" prop="amount"/>
          <el-table-column label="备注"  show-overflow-tooltip align="left" prop="remark"/>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>
      </el-card>

    </div>
    <div
      style="position: absolute;bottom: 0;width: 100%;right: 0;padding: 5px 20px 0;z-index: 999;background: white;text-align: right">
      <div class="demo-drawer__footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </div>
    <!-- 对话框(添加 / 修改) -->
  </el-drawer>
  <!-- 对话框(添加 / 修改) -->
</template>

<script>
import * as CouponApi from '@/api/member/coupon';

export default {
  name: "CopyCouponDraw",
  components: {},
  data() {
    return {
      // 弹出层标题
      // 是否显示弹出层
      drawVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      //回参
      formData: {},
      // 总条数
      total: 0,
      // 卡券列表
      list: [],
      multipleSelection: [],
      id:null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 8,
        name: null,
        type: null,
        carModel: null,
        outRule: null,
      },

    };
  },
  methods: {

    /** 打开弹窗 */
    async open(id) {
      this.drawVisible = true;
      this.formLoading = true;
      this.id = id;
      this.reset();
      if (id) {
        try {
          //获取卡券数据
          const res = await CouponApi.getCoupon(id);
          this.formData = res.data;
          //获取分页信息
          const resp = await CouponApi.getCouponPage(this.queryParams);
          this.list = resp.data.records;
          this.total = resp.data.total;
        } finally {
          this.formLoading = false;
        }
      }

    },

    /**选中*/
    selectRow(val, row) {
      //当前点击是否勾选
      let selectBool = val.length && val.indexOf(row) !== -1
      if (!selectBool) {
        //取消勾选
        this.multipleSelection.forEach((item, i) => {
          if (item.id == row.id) {
            this.multipleSelection.splice(i, 1)
          }
        })
      } else {
        //选中
        this.multipleSelection.push(row)
      }
    },

    /**全选*/
    handleSelectAll(val) {
      if (val.length == this.list.length) { //全选
        this.multipleSelection = this.multipleSelection.concat(val)
        //去重
        let obj = {}
        let result = []
        this.multipleSelection.forEach(item => {
          if (!obj[item.id]) {
            result.push(item)
            obj[item.id] = true
          }
        })
        this.multipleSelection = result
      } else { //取消全选
        this.list.forEach(item => {
          this.multipleSelection.forEach((multItem, i) => {
            if (item.id == multItem.id) {
              this.multipleSelection.splice(i, 1)
            }
          })
        })
      }
    },

    cancelForm() {
      this.drawVisible = false
    },

    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },

    /** 查询列表 */
    async getList() {
      try {
        this.formLoading = true
        const res = await CouponApi.getCouponPage(this.queryParams);
        this.list = res.data.records;
        this.total = res.data.total;
      } finally {
        this.formLoading = false
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**重置参数*/
    reset() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        name: null,
        type: null,
        carModel: null,
        outRule: null,
      }
      this.multipleSelection = []
    },
    /** 提交按钮 */
    async submitForm() {
      this.drawVisible = false
      const data = {
        id:this.id,
        couponList:this.multipleSelection
      }
      await CouponApi.copyCoupon(data);
      this.$modal.msgSuccess("复制成功");
      this.dialogVisible = false;
      this.$emit('success');
    },

  }
};
</script>
