<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="微信昵称" prop="receiveNickName">
        <el-input
          v-model="queryParams.receiveNickName"
          placeholder="请输入微信昵称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取人" prop="receiveUserAccount">
        <el-input
          v-model="queryParams.receiveUserAccount"
          placeholder="请输入领取人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >I
        <!-- INI-已领取待核销, USED-已核销, EXPIRYED-已失效, ERR-发放失败, X-已删除 -->
        <el-option
            key=""
            label="全部"
            value=""
          />
          <el-option
            key="INI"
            label="已领取待核销"
            value="INI"
          />
          <el-option
            key="USED"
            label="已核销"
            value="USED"
          />
          <el-option
            key="EXPIRYED"
            label="已失效"
            value="EXPIRYED"
          />
         <el-option
            key="ERR"
            label="发放失败"
            value="ERR"
          />
          <el-option
            key="X"
            label="已删除"
            value="X"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发放渠道" prop="sendChannel">
        <el-input
          v-model="queryParams.sendChannel"
          placeholder="请输入发放渠道"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销时间">
        <el-date-picker
          v-model="dateRange"
          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="verificationChannelEmployeeName">
        <el-input
          v-model="queryParams.verificationChannelEmployeeName"
          placeholder="请输入核销人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
           <el-form-item label="核销门店" prop="verificationChannelName">
        <el-input
          v-model="queryParams.verificationChannelName"
          placeholder="请输入核销门店"
          clearable
          size="small"
          style="width: 240px"
          @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-download"
              size="mini"
              @click="handleAdd"
            >下载报表</el-button>
          </el-col>
        
         
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column label="id" prop="roleId" width="60" >
        <template  slot-scope="scope">
          {{
            (scope.$index + 1)
        }}
        </template>
      </el-table-column>
      <el-table-column label="优惠券标题" prop="couponName" width="120" />
      <el-table-column label="领取人" prop="receiveUserAccount" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="微信昵称" prop="receiveNickName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="领取时间" prop="receiveTime" width="150" />
      <el-table-column label="券码" prop="couponCode" width="150" />
      <el-table-column label="状态" align="center" width="120">
        <template slot-scope="scope">
          <!-- INI-已领取待核销, USED-已核销, EXPIRYED-已失效, ERR-发放失败, X-已删除 -->
          <span v-if="scope.row.state=='INI'">已领取待核销</span>
          <span v-if="scope.row.state=='USED'">已核销</span>
          <span v-if="scope.row.state=='EXPIRYED'">已失效</span>
          <span v-if="scope.row.state=='ERR'">发放失败</span>
          <span v-if="scope.row.state=='X'">已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="核销时间" align="center" prop="verificationTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verificationTime) }}</span>
        </template>
      </el-table-column>
   <el-table-column label="核销人" align="center" prop="verificationChannelEmployeeName" width="150"/>
<el-table-column label="核销门店" align="center" prop="verificationChannelName" width="150"/>
<el-table-column label="使用人" align="center" prop="receiveNickName" width="150"/>
<el-table-column label="会员体系ID" align="center" prop="memberSysId" width="150"/>
<el-table-column label="发放渠道" align="center" prop="sendChannel" width="150"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { verificationList} from "@/api/coupon/couponData";


export default {
  name: "Role",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项

      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        "couponCode": "",
        "couponInfoId": "",
        "couponName": "",
        "memberSysId": "",
        "receiveNickName": "",
        "receiveUserAccount": "",
        "sendChannel": "",
        "state": "",
        "tableName": "",
        "verificationChannelEmployeeName": "",
        "verificationChannelName": "",
        "verificationEndTime": "",
        "verificationStartTime": ""

      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
   
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      let params = this.queryParams;
      if(this.dateRange.length!=0){
         params.verificationStartTime=this.dateRange[0];
         params.verificationEndTime=this.dateRange[1];
        }
   
      verificationList(params).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

 
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDataScope":
          this.handleDataScope(row);
          break;
        case "handleAuthUser":
          this.handleAuthUser(row);
          break;
        default:
          break;
      }
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      if (type == 'menu') {
        let treeList = this.menuOptions;
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
        }
      } else if (type == 'dept') {
        let treeList = this.deptOptions;
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
        }
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value, type) {
      if (type == 'menu') {
        this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
      } else if (type == 'dept') {
        this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
      }
    },
    // 树权限（父子联动）
    handleCheckedTreeConnect(value, type) {
      if (type == 'menu') {
        this.form.menuCheckStrictly = value ? true: false;
      } else if (type == 'dept') {
        this.form.deptCheckStrictly = value ? true: false;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getMenuTreeselect();
      this.open = true;
      this.title = "添加角色";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roleId = row.roleId || this.ids
      const roleMenu = this.getRoleMenuTreeselect(roleId);
      getRole(roleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.$nextTick(() => {
          roleMenu.then(res => {
            let checkedKeys = res.checkedKeys
            checkedKeys.forEach((v) => {
                this.$nextTick(()=>{
                    this.$refs.menu.setChecked(v, true ,false);
                })
            })
          });
        });
        this.title = "修改角色";
      });
    },
    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if(value !== '2') {
        this.$refs.dept.setCheckedKeys([]);
      }
    },
    /** 分配数据权限操作 */
    handleDataScope(row) {
      this.reset();
      const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId);
      getRole(row.roleId).then(response => {
        this.form = response.data;
        this.openDataScope = true;
        this.$nextTick(() => {
          roleDeptTreeselect.then(res => {
            this.$refs.dept.setCheckedKeys(res.checkedKeys);
          });
        });
        this.title = "分配数据权限";
      });
    },
    /** 分配用户操作 */
    handleAuthUser: function(row) {
      const roleId = row.roleId;
      this.$router.push("/system/role-auth/user/" + roleId);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roleId != undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateRole(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addRole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function() {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openDataScope = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleId || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(function() {
        return delRole(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有用户数据项？').then(() => {
        this.exportLoading = true;
        return exportRole(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>