<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="当前状态0待接受1以接受" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择当前状态0待接受1以接受" clearable size="small">
          <el-option
            v-for="dict in dict.type.task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下单用户标识" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入下单用户标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务类型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务报酬" prop="taskMoney">
        <el-input
          v-model="queryParams.taskMoney"
          placeholder="请输入任务报酬"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <template v-if="showMoreCondition">
        <el-form-item label="主题" prop="theme">
          <el-input
            v-model="queryParams.theme"
            placeholder="请输入主题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="任务截至时间" prop="taskEndTime">
          <el-date-picker
            clearable
            size="small"
            v-model="queryParams.taskEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择任务截至时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务进度" prop="taskState">
          <el-select v-model="queryParams.taskState" placeholder="请选择任务进度" clearable size="small">
            <el-option
              v-for="dict in dict.type.task_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="examineState">
          <el-select v-model="queryParams.examineState" placeholder="请选择审核状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.task_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核说明" prop="examineText">
          <el-input
            v-model="queryParams.examineText"
            placeholder="请输入审核说明"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </template>
      <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-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini" @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</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="id" />-->
      <el-table-column label="当前状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="下单用户标识" align="center" prop="memberId" />
      <el-table-column label="任务类型名称" align="center" prop="taskName" />
<!--      <el-table-column label="任务小图" align="center" prop="taskIcon" v-if="columns[3].visible"/>-->

      <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="image" v-if="columns[5].visible"/>-->

      <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="taskEndTime" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.taskEndTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务进度" align="center" prop="taskState" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_state" :value="scope.row.taskState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="examineState" v-if="columns[10].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_state" :value="scope.row.examineState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核说明" align="center" prop="examineText" v-if="columns[11].visible"/>
<!--      <el-table-column label="审核图片" align="center" prop="examineImage" v-if="columns[12].visible"/>-->

      <el-table-column label="审核图片" align="center" prop="examineImage">
        <template slot-scope="{ row }">
          <el-image v-if="row.examineImage" :src="row.examineImage" :preview-src-list="[row.examineImage]" class="small-img circle-img"/>
        </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="status">
          <el-select v-model="form.status" placeholder="请选择当前状态">
            <el-option
              v-for="dict in dict.type.task_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单用户标识" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入下单用户标识" />
        </el-form-item>
        <el-form-item label="任务类型名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务类型名称" />
        </el-form-item>
        <el-form-item label="任务小图">
<!--          <imageUpload v-model="form.taskIcon"/>-->
          <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="图片">
<!--          <imageUpload v-model="form.image"/>-->
          <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="taskEndTime">
          <el-date-picker clearable size="small"
                          v-model="form.taskEndTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择任务截至时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务进度" prop="taskState">
          <el-select v-model="form.taskState" placeholder="请选择任务进度">
            <el-option
              v-for="dict in dict.type.task_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="examineState">
          <el-select v-model="form.examineState" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.task_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核说明" prop="examineText">
          <el-input v-model="form.examineText" placeholder="请输入审核说明" />
        </el-form-item>
        <el-form-item label="审核图片">
<!--          <imageUpload v-model="form.examineImage"/>-->
          <oss-image-upload v-model="form.examineImage" :limit="1"></oss-image-upload>
        </el-form-item>
<!--        <el-form-item label="DEL_FLAG" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入DEL_FLAG" />-->
<!--        </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 { listAppletTask, getAppletTask, delAppletTask, addAppletTask, updateAppletTask, exportAppletTask } from "@/api/model/AppletTask";

export default {
  name: "AppletTask",
  dicts: ['task_status', 'task_state', 'task_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务中心表格数据
      appletTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        memberId: null,
        taskName: null,
        taskIcon: null,
        taskMoney: null,
        image: null,
        title: null,
        theme: null,
        taskEndTime: null,
        taskState: null,
        examineState: null,
        examineText: null,
        examineImage: 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  },
      ],
      showMoreCondition: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务中心列表 */
    getList() {
      this.loading = true;
      listAppletTask(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,
        status: null,
        memberId: null,
        taskName: null,
        taskIcon: null,
        taskMoney: null,
        image: null,
        title: null,
        theme: null,
        taskEndTime: null,
        taskState: null,
        examineState: null,
        examineText: null,
        examineImage: 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
      getAppletTask(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) {
            updateAppletTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletTask(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 delAppletTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有任务中心数据项？').then(() => {
        this.exportLoading = true;
        return exportAppletTask(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
