<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" :inline="true" >
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申请人" prop="title">
            <el-input
              v-model="queryParams.person"
              placeholder="请输入申请人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="starttime">

            <el-date-picker
              v-model="setime"
              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>
            <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-col>
    </el-row>

    <el-table v-loading="loading" border :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="55" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程名称" width="150" align="center" prop="taskName"/>
      <el-table-column label="标题" align="center" prop="title"  :show-overflow-tooltip="true" />
      <el-table-column label="申请人" width="150" align="center" prop="applyUser"/>
      <el-table-column label="创建时间" width="150" align="center" prop="create_time">
        <template slot-scope="scope">
          <span>{{scope.row.create_time | timefilters}}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前处理" width="150" align="center" prop="dispose">
        <template slot-scope="scope">
          <span>{{scope.row.dispose }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="planLook(scope.row.taskid,scope.row.businessKey)"
          >进度查看
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="detern(scope.row.definitionId,scope.row.businessKey)"
          >详细
          </el-button>

          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteTask(scope.row.taskid,scope.row.businessKey,scope.row.dispose)"
          >删除
          </el-button>
        </template>

      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getAllList"
    />


    <el-dialog title="审批跟踪记录" :visible.sync="dialogVisible" width="1200px">
      <el-tabs :tab-position="tabPosition" style="min-height: 200px;">
        <el-tab-pane>
          <span slot="label"><i class="el-icon-date"></i> 审批记录</span>
          <img :src="picUrl">
        </el-tab-pane>
        <el-tab-pane>
          <span slot="label"><i class="el-icon-date"></i> 审批历史</span>
          <el-table border :data="historyExamine" @selection-change="handleSelectionChange">
            <el-table-column label="序号" type="index" width="130" align="center"></el-table-column>
            <el-table-column label="任务名称" width="180" align="center" prop="activityName"/>
            <el-table-column label="处理人" align="center" prop="assigneeName"/>
            <el-table-column label="审批意见" align="center" prop="comment"/>

            <el-table-column label="申请时间" width="180" align="center" prop="startTime">
              <template slot-scope="scope">
                <span>{{scope.row.startTime | timefilters}}</span>
              </template>
            </el-table-column>

            <el-table-column label="结束时间" width="180" align="center" prop="endTime">
              <template slot-scope="scope">
                <span>{{scope.row.endTime | timefilters}}</span>
              </template>
            </el-table-column>

            <el-table-column label="耗时" width="180" align="center" prop="activityType">
              <template slot-scope="scope">
                <span>{{scope.row.activityType }}</span>
              </template>
            </el-table-column>
          </el-table>
          <!--          formatTotalDateSub()-->

        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
           <el-button @click="cancel">关 闭</el-button>
        <!--          <el-button type="primary" @click="cancel">提 交</el-button>-->
      </span>
    </el-dialog>


    <el-dialog id="actform" title="流程详情" :visible.sync="actOpen" :before-close="cancel" append-to-body width="80%"
               top="10" height="90%">
      <!-- 动态引入组件-->
      <component :is="actForm" :data="details":actOpen="actOpen" @getList="getAllList" ></component>
    </el-dialog>


    <!-- 删除原因 -->
    <el-dialog   :visible.sync="qsshow" width="500px" append-to-body>

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"   >
        <el-form-item label="删除原因" prop="cause">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入删除原因"
            v-model="ruleForm.cause">
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="confirm('ruleForm')">确 定</el-button>
          <el-button @click="cancel">关 闭</el-button>
        </el-form-item>
      </el-form>
      <!--      <div slot="footer" class="dialog-footer">-->
      <!--        <el-button type="primary" @click="confirm('ruleForm')">确 定</el-button>-->
      <!--        <el-button type="primary" @click="cancel">关 闭</el-button>-->
      <!--      </div>-->

    </el-dialog>

  </div>
</template>


<script>
  import { recordImg, examineHistory ,ActManager,deleteTask} from '@/api/system/meapply/meapply'
  import {getStartFormKey } from '@/api/activiti/process/startprocess'
  import moment from "moment";
  export default {
    name: 'meapply',
    filters: {
      timefilters(val) {
        if (val == null || val == "") {
          return "暂无时间";
        } else {
          let d = new Date(val);   //val 为表格内取到的后台时间
          let month =
            d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
          let day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
          let hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
          let min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
          let sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
          let times=d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec;
          return moment(val).format("YYYY-MM-DD HH:mm")
        }
      }
    },
    data() {
      return {
        ruleForm: {
          //删除流程实例的原因
          cause: '',
        },
        rules: {
          cause: [
            { required: true, message: '删除原因不能为空', trigger: 'blur' },
          ]},
        // 删除原因弹出层弹出层
        qsshow: false,
        //流程遮罩层
        actOpen: false,
        //传入到下一页面的值
        details:{getactkey: '',
          formId:'',
          flg:'2',
        },
        picUrl: '',
        dialogVisible: false,
        tabPosition: 'left',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        flgs: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        infoList: [],
        //审批历史
        historyExamine: [
          {
            activityName: '',
            assignee: '',
            assigneeName: '',
            comment: '',
            startTime: '',
            endTime: '',
            activityType: ''
          }

        ],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        actForm: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          senderName: undefined,
          senderAccount: undefined,
          receiverName: undefined,
          receiverAccount: undefined,
          mailTheme: undefined,
          mailContent: undefined,
          status: undefined,
          deleteTime: undefined,
          beginTime: undefined,
          endTime: undefined,
          starttime:undefined,
          endtime:undefined,
          person:undefined,
        },
        // 表单参数
        form: {},
        setime:[],
        // 表单校验
        // rules: {},
        deleteActkey:'',
        deleteId:'',
        deleteDispose:''
      }
    },
    created() {
      this.getAllList();
    },
    methods: {
      /*进度查看*/
      planLook(row,businessKey) {
        var formdata = new FormData
        formdata.append('pProcessInstanceId', row)
        //进度查看
        recordImg(formdata).then(response => {
          this.picUrl = window.URL.createObjectURL(response)
        })
        examineHistory(row,businessKey).then(response => {
          this.historyExamine = response.rows;
          console.log(response.rows)
        })
        this.dialogVisible = true
      },
      detern(actkey,id) {
        let vue = this
        actkey=actkey.split(":")[0];
        this.details.getactkey = actkey//动态传参到下面的页面
        this.details.formId = id//动态传参到下面的页面
        getStartFormKey(actkey).then(response => {
          var path = response.msg
          this.actForm = () => Promise.resolve().then(() => require(`@/views/${path}`))
        })
        this.actOpen = true
        return vue.actForm = this.actForm
      },

      deleteTask(actkey,id,dispose) {
        this.qsshow=true;
        this.$refs["ruleForm"].resetFields();
        this.deleteActkey=actkey;
        this.deleteId=id,
          this.deleteDispose=dispose;
        this.cause='';
      },
      confirm(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var deleteActkey=this.deleteActkey;
            var deleteId=this.deleteId;
            var deleteDispose=this.deleteDispose;
            var cause=this.cause;
            // deleteTask(deleteActkey,deleteId,deleteDispose,cause).then(response => {
            // alert("删除成功")
            // })
            this.$confirm('删除将无法恢复，是否确认删除?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
              return deleteTask(deleteActkey,deleteId,deleteDispose,cause);
            }).then(() => {
              this.qsshow=false
              this.getAllList();
              this.msgSuccess("删除成功");
            }).catch(function() {});
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      /** 所有申请流程列表 */
      getAllList() {
        this.loading = true
        ActManager(this.queryParams).then(response => {

          this.infoList = response.rows

          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
        this.dialogVisible = false
        this.actOpen = false;
        this.qsshow=false;
      },
      // 表单重置
      reset() {
        this.form = {
          mailGuid: undefined,
          senderName: undefined,
          senderAccount: undefined,
          receiverName: undefined,
          receiverAccount: undefined,
          mailTheme: undefined,
          mailContent: undefined,
          createTime: undefined,
          status: '0',
          deleteTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.endtime =this.setime[0]
        this.queryParams.startime = this.setime[1]
        this.queryParams.pageNum = 1
        this.getAllList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.person = undefined;
        this.setime=[];
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.mailGuid)
        this.flgs = selection.map(item => item.flg)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },


    }
  }
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 0px !important;
  }
</style>
