<template>
  <div class="">
    <el-card style="margin-bottom: 20px">
      <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="isonline">
          <el-select v-model="queryParams.isonline" placeholder="是否在线">
            <el-option label="在线" value="0"></el-option>
            <el-option label="下线" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="status">
          <el-select v-model="queryParams.status" placeholder="兑换券状态">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="type">
          <el-select v-model="queryParams.type" placeholder="兑换券类型">
            <el-option label="兑换券" value="0"></el-option>
            <el-option label="洗车券" value="1"></el-option>
            <el-option label="洗车卡" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>

        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-bottom: 20px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"

          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:exchange:edit']"
          >修改
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">

        <!--      <el-table-column label="主键id" align="center" prop="id" />-->
        <!--      <el-table-column label="所属连锁店id" align="center" prop="chainStorId" />-->
        <!--      <el-table-column label="所属店铺id" align="center" prop="storeId" />-->
        <el-table-column label="是否在线" align="center" prop="isonline">
          <template slot-scope="scope">
            <span v-if="scope.row.isonline == 0">在线</span>
            <span v-if="scope.row.isonline == 1">下线</span>
          </template>
        </el-table-column>
        <el-table-column label="兑换券状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">  <el-tag>启用</el-tag> </span>
            <span v-if="scope.row.status == 1">  <el-tag type="danger">禁用</el-tag> </span>

          </template>
        </el-table-column>
        <el-table-column label="兑换券名称" align="center" prop="name"/>
        <el-table-column label="兑换券类型 " align="center" prop="type">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 0"> <el-tag>兑换券</el-tag> </span>
            <span v-if="scope.row.type == 1"> <el-tag type="success">洗车券</el-tag> </span>
            <span v-if="scope.row.type == 2"> <el-tag type="warning">洗车卡</el-tag> </span>
          </template>
        </el-table-column>
        <el-table-column label="兑换礼品名" align="center" prop="giftName"/>
        <el-table-column label="卡券有效期" align="center" prop="validity">
          <template slot-scope="scope">
            <span>{{scope.row.validity}}天</span>

          </template>
        </el-table-column>
        <el-table-column label="使用说明" align="center" prop="useInstructions"/>
        <el-table-column label="二维码链接" align="center" prop="qrCodeLink">
          <template slot-scope="scope">
            <i class="el-icon-picture" style="size: 20px;cursor: pointer" @click="qrcodelook(scope.row.qrCodeLink)"></i>
            <!--        <span>{{ scope.row.qrCodeLink }}</span>-->
          </template>
        </el-table-column>
        <el-table-column label="参与次数" align="center" prop="count"/>
        <el-table-column label="到期时间" align="center" prop="outTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-if="scope.row.isonline == 0"
              @click="handleXia(scope.row)"
            >下线
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-if="scope.row.isonline == 1"
              @click="handleShang(scope.row)"
            >上线
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改兑换券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--        <el-form-item label="所属连锁店id" prop="chainStorId">-->
        <!--          <el-input v-model="form.chainStorId" placeholder="请输入所属连锁店id" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="所属店铺id" prop="storeId">-->
        <!--          <el-input v-model="form.storeId" placeholder="请输入所属店铺id" />-->
        <!--        </el-form-item>-->
        <el-form-item label="兑换券名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入兑换券名称"/>
        </el-form-item>
        <el-form-item label="兑换券类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio label="0">兑换券</el-radio>
            <el-radio label="1">洗车券</el-radio>
            <el-radio label="2">洗车卡</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="兑换礼品名" prop="giftName">
          <el-input type="textarea" v-model="form.giftName" placeholder="请输入兑换礼品名"></el-input>
        </el-form-item>
        <el-form-item label="卡券有效期" prop="validity">
          <el-input placeholder="请输入卡券有效期" v-model="form.validity">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="使用说明" prop="useInstructions">
          <el-input type="textarea" v-model="form.useInstructions" placeholder="请输入使用说明"></el-input>
        </el-form-item>
        <el-form-item label="参与次数" prop="count">
          <el-input-number v-model="form.count" controls-position="right" :min="1" :max="999"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="二维码"
      v-loading="loadingdialog"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <div style="display: flex;align-items: center">
        <div class="qr-code" id="qrCode" ref="qrCodeUrl"></div>
      </div>

      <span slot="footer" class="dialog-footer">
         <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import QRCode from 'qrcodejs2'
  import {
    listExchange,
    getExchange,
    delExchange,
    addExchange,
    updateExchange
  } from '@/api/EventMarketing/cardExchange'

  export default {
    name: 'Exchange',
    data() {
      return {
        centerDialogVisible: false,
        loadingdialog:true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 兑换券表格数据
        exchangeList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          chainStorId: null,
          storeId: null,
          isonline: null,
          status: null,
          name: null,
          type: null,
          giftName: null,
          validity: null,
          useInstructions: null,
          qrCodeLink: null,
          count: null,
          outTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: '请输入兑换券名称', trigger: 'change' }
          ],
          type: [
            { required: true, message: '请输入兑换券类型', trigger: 'change' }
          ],
          giftName: [
            { required: true, message: '请输入兑换礼品名', trigger: 'change' }
          ],
          validity: [
            { required: true, message: '请输入卡券有效期', trigger: 'change' }
          ],
          useInstructions: [
            { required: true, message: '请输入使用说明', trigger: 'change' }
          ],
          count: [
            { required: true, message: '请输入参与次数', trigger: 'change' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ]

        }
      }
    },
    created() {
      this.getList()

    },
    methods: {
      qrcodelook(url) {
        this.loadingdialog = true
        this.centerDialogVisible = true
        setTimeout(() => {
          this.creatQrCode(url)
          this.loadingdialog = false
        }, 1000);

      },
      creatQrCode(url) {

        document.getElementById("qrCode").innerHTML = ""

        new QRCode(this.$refs.qrCodeUrl, {
          text: url, // 二维码的内容
          width: 300,
          height: 300,
          colorDark: '#000',
          colorLight: '#fff',
          correctLevel: QRCode.CorrectLevel.H
        })


      },

      /** 查询兑换券列表 */
      getList() {
        this.loading = true
        listExchange(this.queryParams).then(response => {
          this.exchangeList = response.data.records
          this.total = response.data.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          chainStorId: null,
          storeId: null,
          isonline: null,
          status: null,
          name: null,
          type: null,
          giftName: null,
          validity: null,
          useInstructions: null,
          qrCodeLink: null,
          count: null,
          outTime: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加兑换券'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.form = row
        this.open = true
        this.title = '修改兑换券'
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateExchange(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addExchange(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 下线操作 */
      handleXia(row) {
        let data = row
        data.isonline = 1
        updateExchange(data).then(res => {
          if (res.code == 200) {
            this.$message.success('下线成功')
          }
        })
      },
      /** 上线操作 */
      handleShang(row) {
        let data = row
        data.isonline = 0
        updateExchange(data).then(res => {
          if (res.code == 200) {
            this.$message.success('上线成功')
          }
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/exchange/export', {
          ...this.queryParams
        }, `exchange_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>
<style scoped lang="scss">
  .qr-code{
    margin: 0px auto;
  }
</style>
