<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="rewardType">
        <el-select v-model="queryParams.rewardType" placeholder="请选择业务类型" clearable size="small">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客服名称" prop="qwUserName">
        <el-input
          v-model="queryParams.qwUserName"
          placeholder="请输入客服名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="公司名称" prop="companyName">-->
<!--        <el-input-->
<!--          v-model="queryParams.companyName"-->
<!--          placeholder="请输入公司名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="积分数" prop="coinAmount">
        <el-input
          v-model="queryParams.coinAmount"
          placeholder="请输入积分数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放时间">
        <el-date-picker
          v-model="daterangeSendTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="领取时间">
        <el-date-picker
          v-model="daterangeCollectTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="失效时间">
        <el-date-picker
          v-model="daterangeExpiryTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="领取状态" prop="collectType">
        <el-select v-model="queryParams.collectType" placeholder="请选择领取状态" clearable size="small">
          <el-option
            v-for="item in collectTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="群聊名称" prop="externalUserName">
        <el-input
          v-model="queryParams.externalUserName"
          placeholder="请输入群聊名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['qw:luckyBagCollectRecord:add']"-->
<!--        >新增</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="['qw:luckyBagCollectRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['qw:luckyBagCollectRecord:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['qw:luckyBagCollectRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="luckyBagCollectRecordList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column v-if="false"  label="主键ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="福袋名称" align="center" prop="luckyBagName" />
      <el-table-column label="客服名称" align="center" prop="qwUserName" />
      <el-table-column label="企微员工名称" align="center" prop="companyUserName" />
      <el-table-column label="业务类型" align="center" prop="rewardType">
        <template slot-scope="scope">
          <div v-for="item in options" v-if="item.value==scope.row.rewardType">{{item.label}}</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="福袋表主键ID" align="center" prop="luckyBagId" />-->
<!--      <el-table-column label="销售ID" align="center" prop="companyUserId" />-->
<!--      <el-table-column label="公司ID" align="center" prop="companyId" />-->
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="积分数量" align="center" prop="coinAmount" />
      <el-table-column label="发放时间" align="center" prop="sendTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="collectTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="expiryTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取状态" align="center" prop="collectType">
        <template slot-scope="scope">
          <div v-for="item in collectTypeOptions" v-if="item.value==scope.row.collectType">{{item.label}}</div>
        </template>
      </el-table-column>
      <el-table-column label="群聊名称" align="center" prop="externalUserName" />
      <el-table-column label="发放ID" align="center" prop="id" />
      <el-table-column label="领取ID" align="center" prop="relationId"/>

<!--      <el-table-column label="群聊会话id" align="center" prop="chatId" />-->
<!--      <el-table-column label="关联id 关联群发记录id" align="center" prop="relationId" />-->
<!--      <el-table-column label="发送链接" align="center" prop="sendLink" />-->
<!--      <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)"-->
<!--            v-hasPermi="['qw:luckyBagCollectRecord:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['qw:luckyBagCollectRecord:remove']"-->
<!--          >删除</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-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="领取用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入领取用户ID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="业务类型" prop="rewardType">
          <el-select v-model="form.rewardType" placeholder="请选择业务类型">
            <el-option label="1" value="群福袋" />
            <el-option label="2" value="个人福袋" />
          </el-select>
        </el-form-item>
        <el-form-item label="福袋表主键ID" prop="luckyBagId">
          <el-input v-model="form.luckyBagId" placeholder="请输入福袋表主键ID" />
        </el-form-item>
        <el-form-item label="销售ID" prop="companyUserId">
          <el-input v-model="form.companyUserId" placeholder="请输入销售ID" />
        </el-form-item>
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="积分数量" prop="coinAmount">
          <el-input v-model="form.coinAmount" placeholder="请输入积分数量" />
        </el-form-item>
        <el-form-item label="发放时间" prop="sendTime">
          <el-date-picker clearable size="small"
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发放时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取时间" prop="collectTime">
          <el-date-picker clearable size="small"
            v-model="form.collectTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择领取时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效时间" prop="expiryTime">
          <el-date-picker clearable size="small"
            v-model="form.expiryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择失效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取状态(0-已发放 1-已领取 2-已失效)" prop="collectType">
          <el-select v-model="form.collectType" placeholder="请选择领取状态(0-已发放 1-已领取 2-已失效)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户或者群聊名称" prop="externalUserName">
          <el-input v-model="form.externalUserName" placeholder="请输入客户或者群聊名称" />
        </el-form-item>
        <el-form-item label="群聊会话id" prop="chatId">
          <el-input v-model="form.chatId" placeholder="请输入群聊会话id" />
        </el-form-item>
        <el-form-item label="关联id 关联群发记录id" prop="relationId">
          <el-input v-model="form.relationId" placeholder="请输入关联id 关联群发记录id" />
        </el-form-item>
        <el-form-item label="发送链接" prop="sendLink">
          <el-input v-model="form.sendLink" type="textarea" placeholder="请输入内容" />
        </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>
  </div>
</template>

<script>
import { listLuckyBagCollectRecord, getLuckyBagCollectRecord, delLuckyBagCollectRecord, addLuckyBagCollectRecord, updateLuckyBagCollectRecord, exportLuckyBagCollectRecord } from "@/api/qw/luckyBagCollectRecord";

export default {
  name: "LuckyBagCollectRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 福袋发放及领取记录表格数据
      luckyBagCollectRecordList: [],
      // 弹出层标题
      title: "",
      // 发放时间时间范围
      daterangeSendTime: [],
      // 领取时间时间范围
      daterangeCollectTime: [],
      // 失效时间时间范围
      daterangeExpiryTime: [],
      // 是否显示弹出层
      open: false,
      options:[{
        label: "群福袋",
        value: 1
      },
      {
        label: "个人福袋",
        value: 2
      }],
      collectTypeOptions:[
        {
          label: "已发放",
          value: "0"
        },
        {
          label: "已领取",
          value: "1"
        },
        {
          label: "已失效",
          value: "2"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        rewardType: null,
        qwUserName: null,
        companyName: null,
        coinAmount: null,
        sendTime: null,
        collectTime: null,
        expiryTime: null,
        collectType: null,
        externalUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rewardType: [
          { required: true, message: "业务类型：1-群福袋 2-个人福袋不能为空", trigger: "change" }
        ],
        luckyBagId: [
          { required: true, message: "福袋表主键ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getRewardTypeLabel(value) {
      const option = this.options.find(item => item.value === value);
      return option ? option.label : '';
    },
    /** 查询福袋发放及领取记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSendTime && '' != this.daterangeSendTime) {
        this.queryParams.params["beginSendTime"] = this.daterangeSendTime[0];
        this.queryParams.params["endSendTime"] = this.daterangeSendTime[1];
      }
      if (null != this.daterangeCollectTime && '' != this.daterangeCollectTime) {
        this.queryParams.params["beginCollectTime"] = this.daterangeCollectTime[0];
        this.queryParams.params["endCollectTime"] = this.daterangeCollectTime[1];
      }
      if (null != this.daterangeExpiryTime && '' != this.daterangeExpiryTime) {
        this.queryParams.params["beginExpiryTime"] = this.daterangeExpiryTime[0];
        this.queryParams.params["endExpiryTime"] = this.daterangeExpiryTime[1];
      }
      listLuckyBagCollectRecord(this.queryParams).then(response => {
        this.luckyBagCollectRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        rewardType: null,
        qwUserName: null,
        luckyBagId: null,
        companyUserId: null,
        companyId: null,
        companyName: null,
        coinAmount: null,
        sendTime: null,
        collectTime: null,
        expiryTime: null,
        collectType: null,
        externalUserName: null,
        chatId: null,
        relationId: null,
        sendLink: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSendTime = [];
      this.daterangeCollectTime = [];
      this.daterangeExpiryTime = [];
      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();
      const id = row.id || this.ids
      getLuckyBagCollectRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改福袋发放及领取记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLuckyBagCollectRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLuckyBagCollectRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除福袋发放及领取记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLuckyBagCollectRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有福袋发放及领取记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportLuckyBagCollectRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
