<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="活动名称" prop="acname">
        <el-input v-model="queryParams.acname" placeholder="请输入活动名称"></el-input>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable filterable>
          <el-option v-for="dict in statusList" :key="dict.value + 'statusList'" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-hasPermi="['business:INSTACTIONTASK:byUser']" :data="INSTACTIONTASKList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="活动名称" width="180" show-overflow-tooltip align="center" prop="acname">
        <template slot-scope="scope">
          {{ scope.row.acname }}
        </template>
      </el-table-column>
      <el-table-column label="标题" width="220" show-overflow-tooltip align="center" prop="fromKeyword ">
        <template slot-scope="scope">
          {{ scope.row.fromKeyword }}
        </template>
      </el-table-column>
      <el-table-column label="流程名称" width="180" show-overflow-tooltip align="center" prop="proname">
        <template slot-scope="scope">
          {{ scope.row.proname }}
        </template>
      </el-table-column>
      <el-table-column label="递交时间" width="160" show-overflow-tooltip align="center" prop="beginTime">
        <template slot-scope="scope">
          {{ scope.row.beginTime }}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '1'" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status == '2'" type="danger">驳回</el-tag>
          <el-tag v-else-if="scope.row.status == '3'" type="info">退回</el-tag>
          <el-tag v-else-if="scope.row.status == '0'">待审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" width="260" show-overflow-tooltip align="center" prop="comments">
        <template slot-scope="scope">
          {{ scope.row.comments }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '0' && checkPermi(['business:INSTACTIONTASK:query'])" size="mini" type="text" @click="handleExamine(scope.row)"
            >审核</el-button>

          <!-- <el-button v-if="scope.row.status == '0'" size="mini" type="text" @click="handlePass(scope.row)"
            >通过审核</el-button>
          <el-button v-if="scope.row.status == '0'" size="mini" type="text" @click="handleReject(scope.row)"
            >驳回</el-button> -->

          <el-button size="mini" type="text" @click="handleDetail(scope.row)"
             v-if="scope.row.status != '0'">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="审核" :visible.sync="dialogVisible" width="80%" :close-on-click-modal="false" @close="closeDia">
      <component :is="componentUrl" :taskList="taskList" ref="componentSH"></component>
      <el-dialog width="60%" :title="subTitle" :visible.sync="innerVisible" :close-on-click-modal="false"
        append-to-body center @close="qx()">
        <el-form :model="subForm" :rules="subFormRules" label-width="100px" ref="extaskform">
          <el-form-item label="审核意见">
            <el-input type="textarea" v-model="subForm.comments" placeholder="请输入审核意见"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="qx">取 消</el-button>
          <el-button type="primary" @click="extask">确 定</el-button>
        </div>
      </el-dialog>

      <span slot="footer" class="dialog-footer" v-if="taskList.status == 0">
        <el-button v-if="checkPermi(['task:instaction:approve'])" type="primary" @click="submitBtn('审核通过')">审核通过</el-button>
        <el-button  v-if="checkPermi(['task:instaction:reject'])"  type="warning" @click="submitBtn('驳回')">驳回</el-button>
        <!-- <el-button  v-if="checkPermi(['task:instaction:goback'])"  type="danger" @click="submitBtn('退回')">退回</el-button> -->
      </span>
      <span slot="footer" class="dialog-footer" v-else>
        <el-button type="primary" @click="backDia">返回</el-button>
      </span>
    </el-dialog>

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

<script>
//导入审核页面的组件
import newsTask from "@/views/news/task/index.vue";
import couTask from "@/views/COUNSELINGMESSAGE/task/index.vue";
import ModifyCompanyUserService from "@/views/REGISTERTASK/index.vue";
import CompanyUserService from "@/views/CompanyUserService/index.vue";
import questionService from "@/views/QUESTION/questionService.vue";
import articleTask from "@/views/ARTICLE/task/index.vue";
import dataAcquisition from "@/components/DataAcquisition";
import auxiliaryAudit from "@/components/AuxiliaryAudit";

import { checkPermi } from "@/utils/permission.js";



import {
  listINSTACTIONTASK,
  getINSTACTIONTASK,
  delINSTACTIONTASK,
  addINSTACTIONTASK,
  updateINSTACTIONTASK,
  approve, //通过
  reject, //驳回
  goback, //退回
  downloadpic,
} from "@/api/portal/task/task";

export default {
  name: "INSTACTIONTASK",
  components: {
    newsTask,
    couTask,
    ModifyCompanyUserService,
    questionService,
    articleTask,
    dataAcquisition,
    CompanyUserService,
    auxiliaryAudit,

  },
  data() {
    return {
      //审核意见对话框的form
      subForm: {},
      //控制显示审核意见对话框
      innerVisible: false,
      //点击审核那三个按钮会触发submitBtn函数然后设置这个title
      subTitle: "",
      //审核页面的
      taskList: {
        formobj: {
          picture: "",
        },
      },
      //动态组件
      componentUrl: "",
      //审核的dialog
      dialogVisible: false,
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批任务实例表格数据
      INSTACTIONTASKList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        instAcId: null,
        instProId: null,
        acname: null,
        execId: null,
        beginTime: null,
        endTime: null,
        status: "0",
        comments: null,
      },
      // 表单参数
      form: {},
      subFormRules: {
        comments: [
          { required: true, message: "意见不能为空", trigger: "blur" },
        ],
      },
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" },
        ],
        instAcId: [
          {
            required: true,
            message: "活动实例任务ID不能为空",
            trigger: "blur",
          },
        ],
        instProId: [
          { required: true, message: "活动实例id不能为空", trigger: "blur" },
        ],
        acname: [
          { required: true, message: "活动名称不能为空", trigger: "blur" },
        ],
        execId: [
          { required: true, message: "审核人不能为空", trigger: "blur" },
        ],
        beginTime: [
          { required: true, message: "任务启动时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "任务结束时间不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "审核状态(1=审核通过,2=驳回）不能为空",
            trigger: "blur",
          },
        ],
        comments: [
          { required: true, message: "审核意见不能为空", trigger: "blur" },
        ],
      },
      statusList: [
        { value: null, label: "全部" },
        { value: "0", label: "待审核" },
        { value: "1", label: "审核通过" },
        { value: "2", label: "驳回" },
        { value: "3", label: "退回" },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    closeDia() {
    },
    extask() {
      this.$refs["extaskform"].validate((valid) => {
        if (valid) {
          let data = {
            comments: this.subForm.comments,
            taskId: this.taskList.id,
          };

          if (this.subTitle == "审核通过") {
            approve(data).then((res) => {
              this.dialogVisible = false;
              this.qx();
              this.getList();
            });
          } else if (this.subTitle == "驳回") {
            reject(data).then((res) => {
              this.dialogVisible = false;
              this.qx();
              this.getList();
            });
          } else if (this.subTitle == "退回") {
            goback(data).then((res) => {
              this.dialogVisible = false;
              this.qx();
              this.getList();
            });
          }
        }
      });
    },
    backDia() {
      this.dialogVisible = false;
    },
    qx() {
      //审批意见的取消
      this.subForm = {};
      this.innerVisible = false;
      // this.dialogFormVisible = false;
    },
    //点击审核那三个按钮会触发submitBtn函数然后设置这个title
    submitBtn(type) {
      this.subTitle = type;
      this.innerVisible = true;
    },
    // //点击审核
    // handleExamine(row) {
    //   // this.taskList = []
    //   getINSTACTIONTASK(row.id).then((res) => {
    //     this.taskList = res.data;
    //     this.componentUrl = res.data.infoUrl;
    //     downloadpic(res.data.formobj.picture).then((res) => {
    //       this.taskList.formobj.picture = window.URL.createObjectURL(res);
    //       this.dialogVisible = true;
    //     });
    //   });
    // },

    handleReject(data) {
      reject({
        taskId: data.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("驳回成功")
          this.getList();
        }
      });
    },

    handlePass(data) {
      approve({
        taskId: data.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("审核通过")
          this.getList();
        }
      });
    },

    //点击详情
    handleExamine(row) {
      // this.taskList = []
      getINSTACTIONTASK(row.id).then((res) => {
        this.taskList = res.data;
        this.componentUrl = res.data.infoUrl;
        this.dialogVisible = true;
      });
    },
    //点击查看详细
    handleDetail(row) {
      getINSTACTIONTASK(row.id).then((res) => {
        this.taskList = res.data;
        this.componentUrl = res.data.infoUrl;
        downloadpic(this.taskList.formobj.picture).then((res) => {
          this.taskList.formobj.picture = window.URL.createObjectURL(res);
          this.dialogVisible = true;
        });
      });
    },
    /** 查询审批任务实例列表 */
    getList() {
      this.loading = true;
      listINSTACTIONTASK(this.queryParams).then((response) => {
        this.INSTACTIONTASKList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        instAcId: null,
        instProId: null,
        acname: null,
        execId: null,
        beginTime: null,
        endTime: null,
        status: "0",
        comments: 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;
      getINSTACTIONTASK(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) {
            updateINSTACTIONTASK(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addINSTACTIONTASK(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 delINSTACTIONTASK(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/INSTACTIONTASK/export",
        {
          ...this.queryParams,
        },
        `INSTACTIONTASK_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
