<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  :data="INSTACTIONTASKList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="活动名称" align="center" prop="acname">
        <template slot-scope="scope">
          {{ scope.row.acname }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="fromKeyword ">
        <template slot-scope="scope">
          {{ scope.row.fromKeyword }}
        </template>
      </el-table-column>
      <el-table-column label="流程名称" align="center" prop="proname">
        <template slot-scope="scope">
          {{ scope.row.proname }}
        </template>
      </el-table-column>
      <el-table-column label="任务启动时间" align="center" prop="beginTime">
        <template slot-scope="scope">
          {{ scope.row.beginTime ? scope.row.beginTime.substr(0, 10) : '无' }}
        </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="审核意见" align="center" prop="comments">
        <template slot-scope="scope">
          {{ scope.row.comments }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '0'" size="mini" type="text" @click="handleExamine(scope.row)" v-hasPermi="['business:INSTACTIONTASK:task']">审核</el-button>
          <el-button size="mini" type="text" @click="handleDetail(scope.row)" v-hasPermi="['business:INSTACTIONTASK:detail']" 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" 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 type="primary" @click="submitBtn('同意')">同意</el-button>
        <el-button type="warning" @click="submitBtn('驳回')">驳回</el-button>
        <el-button 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 questionService from '@/views/QUESTION/questionService.vue';
import articleTask from '@/views/ARTICLE/task/index.vue'

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

export default {
  name: 'INSTACTIONTASK',
  components: {
    newsTask,
    couTask,
    questionService,
    articleTask
  },
  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: {
    closeDia() {
      // console.log('关闭');
    },
    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);
    //       console.log('下载转换后:',this.taskList.formobj.picture)
    //       this.dialogVisible = true;
    //     });
    //   });
    // },
    //点击审核
    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>
