<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="关联" prop="taskId">
        <el-select v-model="queryParams.taskId" placeholder="请选择关联" clearable size="small" filterable>
          <el-option
            v-for="task in taskList"
            :key="task.id"
            :label="task.taskName"
            :value="task.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员" prop="memberId">
        <el-select v-model="queryParams.memberId" placeholder="请选择会员" clearable size="small" filterable>
          <el-option
            v-for="member in memberList"
            :key="member.id"
            :label="member.nickname + '(' + member.phoneHidden + ')'"
            :value="member.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结算优惠券" prop="taskCoupon">
        <el-select v-model="queryParams.taskCoupon" placeholder="请选择结算优惠券" clearable size="small" filterable>
          <el-option
            v-for="coupon in couponList"
            :key="coupon.id"
            :label="coupon.stockName"
            :value="coupon.stockName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable size="small">
          <el-option label="待提交" :value="0" />
          <el-option label="待审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="已拒绝" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item class="flex_one tr">
        <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="['model:appletTask: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="['model:appletTask: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="['model:appletTask: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="['model:appletTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appletTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="关联" align="center" prop="taskId" />
      <el-table-column label="会员" align="center" prop="memberId">
        <template slot-scope="scope">
          <span v-if="getMemberById(scope.row.memberId)">{{ getMemberById(scope.row.memberId).nickname }}({{ getMemberById(scope.row.memberId).phoneHidden }})</span>
          <span v-else>{{ scope.row.memberId }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="类型名称" align="center" prop="taskName" /> -->
      <el-table-column label="小图" align="center" prop="taskIcon">
        <template slot-scope="{ row }">
          <el-image v-if="row.taskIcon" :src="row.taskIcon" :preview-src-list="[row.taskIcon]" class="small-img circle-img"/>
        </template>
      </el-table-column>
      <el-table-column label="报酬" align="center" prop="taskMoney" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.type === '0' ? '优惠券' : '现金' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算优惠券" align="center" prop="taskCouponTitle" />
      <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="{ row }">
          <el-image v-if="row.image" :src="row.image" :preview-src-list="[row.image]" class="small-img circle-img"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="主题" align="center" prop="theme" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="证明截图" align="center" prop="proveImage">
        <template slot-scope="{ row }">
          <el-image v-if="row.proveImage" :src="row.proveImage.split(',')[0]" 
          :preview-src-list="row.proveImage.split(',')" 
          class="small-img"/>
        </template>
      </el-table-column>
      <el-table-column label="证明说明" align="center" prop="proveTxt" />
      <el-table-column label="驳回说明" align="center" prop="rejectTxt" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 2 ? 'success' : scope.row.status === 3 ? 'danger' : 'warning'">
            {{ scope.row.status === 0 ? '待提交' : scope.row.status === 1 ? '待审核' : scope.row.status === 2 ? '已通过' : '已拒绝' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="接受时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交审核时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '')}}</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)"
            v-hasPermi="['model:appletTask:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:appletTask: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-form-item label="关联" prop="taskId">
          <el-select v-model="form.taskId" placeholder="请选择关联" filterable>
            <el-option
              v-for="task in taskList"
              :key="task.id"
              :label="task.taskName"
              :value="task.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员" prop="memberId">
          <el-select v-model="form.memberId" placeholder="请选择会员" filterable>
            <el-option
              v-for="member in memberList"
              :key="member.id"
              :label="member.nickname + '(' + member.phoneHidden + ')'"
              :value="member.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="小图">
          <oss-image-upload v-model="form.taskIcon" :limit="1"></oss-image-upload>
        </el-form-item>
        <el-form-item label="报酬" prop="taskMoney">
          <el-input v-model="form.taskMoney" placeholder="请输入报酬" />
        </el-form-item>
        <el-form-item label="结算优惠券" prop="taskCoupon">
          <el-select v-model="form.taskCoupon" placeholder="请选择结算优惠券" clearable filterable>
            <el-option
              v-for="coupon in couponList"
              :key="coupon.id"
              :label="coupon.stockName"
              :value="coupon.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" filterable>
            <el-option label="优惠券" value="0" />
            <el-option label="现金" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片">
          <oss-image-upload v-model="form.image" :limit="1"></oss-image-upload>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="主题" prop="theme">
          <el-input v-model="form.theme" placeholder="请输入主题" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证明截图">
          <oss-image-upload v-model="form.proveImage" :limit="1"></oss-image-upload>
        </el-form-item>
        <el-form-item label="证明说明" prop="proveTxt">
          <el-input v-model="form.proveTxt" type="textarea" placeholder="请输入证明说明" />
        </el-form-item>
        <el-form-item label="驳回说明" prop="rejectTxt">
          <el-input v-model="form.rejectTxt" type="textarea" placeholder="请输入驳回说明" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择审核状态" filterable>
            <el-option label="待提交" :value="0" />
            <el-option label="待审核" :value="1" />
            <el-option label="已通过" :value="2" />
            <el-option label="已拒绝" :value="3" />
          </el-select>
        </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 { listAppletTaskUser, getAppletTaskUser, delAppletTaskUser, addAppletTaskUser, updateAppletTaskUser, exportAppletTaskUser } from "@/api/model/AppletTaskUser";
import { listAppletTask } from "@/api/model/AppletTask";
import { listUmsMemberName } from "@/api/ums/member";
import { listWechatStock } from "@/api/marketing/wechatStock";

export default {
  name: "AppletTaskUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中心表格数据
      appletTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        memberId: null,
        startTime: null,
        endTime: null,
        taskCoupon: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      columns: [
        { key: 1, label: "当前状态0待接受1以接受", visible:  true  },
        { key: 2, label: "会员", visible:  true  },
        { key: 3, label: "类型名称", visible:  true  },
        { key: 4, label: "小图", visible:  false  },
        { key: 5, label: "报酬", visible:  true  },
        { key: 6, label: "图片", visible:  false  },
        { key: 7, label: "标题", visible:  true  },
        { key: 8, label: "主题", visible:  true  },
        { key: 9, label: "截至时间", visible:  true  },
        { key: 10, label: "进度", visible:  false  },
        { key: 11, label: "审核状态", visible:  false  },
        { key: 12, label: "审核说明", visible:  false  },
        { key: 13, label: "审核图片", visible:  false  },
      ],
      // 列表
      taskList: [],
      // 会员列表
      memberList: [],
      // 优惠券列表
      couponList: []
    };
  },
  created() {
    this.getList();
    this.getTaskList();
    this.getMemberList();
    this.getCouponList();
  },
  methods: {
    /** 获取列表 */
    getTaskList() {
      listAppletTask({}).then(response => {
        this.taskList = response.rows || [];
      });
    },
    /** 获取会员列表 */
    getMemberList() {
      listUmsMemberName().then(response => {
        this.memberList = response.data || [];
      });
    },
    /** 根据会员ID获取会员信息 */
    getMemberById(memberId) {
      return this.memberList.find(member => member.id == memberId);
    },
    /** 获取优惠券列表 */
    getCouponList() {
      listWechatStock({ pageNum: 1, pageSize: 999999999 }).then(response => {
        response.rows.forEach(item => {
          item.id = item.id + "";
        })
        this.couponList = response.rows || [];
      });
    },
    /** 查询中心列表 */
    getList() {
      this.loading = true;
      listAppletTaskUser(this.queryParams).then(response => {
        this.appletTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        memberId: null,
        taskName: null,
        taskIcon: null,
        taskMoney: null,
        taskCoupon: null,
        type: null,
        image: null,
        title: null,
        theme: null,
        startTime: null,
        endTime: null,
        proveImage: null,
        proveTxt: null,
        rejectTxt: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: 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();
      const id = row.id || this.ids
      getAppletTaskUser(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) {
            updateAppletTaskUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletTaskUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + ids + '"的数据项？').then(function() {
        return delAppletTaskUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有用户数据项？').then(() => {
        this.exportLoading = true;
        return exportAppletTaskUser(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
