<template>
  <div class="workspace">
    <el-button icon="el-icon-back" class="back" type="info" size="mini" plain
               @click="$router.push('/')">返回主页
    </el-button>
    <el-tabs v-model="active" type="border-card">
      <el-tab-pane label="审批列表" name="approveList">
        <el-row style="margin-bottom: 20px">
          <el-col :xs="12" :sm="10" :md="8" :lg="6" :xl="4">
            <el-input size="medium" v-model="formList.inputs" placeholder="搜索表单" @keyup.enter.native="getGroups" clearable>
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
          </el-col>
        </el-row>
        <el-collapse v-model="actives">
          <el-collapse-item v-for="(group, index) in formList.list" :key="index"
                            :title="group.name" :name="group.name"
                            v-show="group.items.length > 0 && group.id > 0">
            <div>
              <div v-for="(item, index) in group.items" :key="index" class="form-item" @click="enterItem(item)">
                <i :class="item.logo.icon" :style="'background: '+item.logo.background"></i>
                <div>
                  <ellipsis hover-tip :content="item.deploymentName"/>
                  <span>发起审批</span>
                </div>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
        <div>
          <div v-for="(item, index) in formList.searchResult" :key="index" class="form-item" @click="enterItem(item)">
            <i :class="item.icon" :style="'background: '+item.background"></i>
            <div>
              <span>{{ item.deploymentName }}</span>
              <span>发起审批</span>
            </div>
          </div>
          <div class="no-data" v-show="formList.searchResult.length === 0 && formList.inputs !== ''">
            没有找到相关的表单 😥
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="待我处理(3)" name="disposalTask">
        <disposal-task v-if="active === 'disposalTask'" ref="disposalTask"/>
      </el-tab-pane>
      <el-tab-pane label="我发起的" name="initiated">
        <initiated-instance v-if="active === 'initiated'"  ref="InitiatedInstance"/>
      </el-tab-pane>
      <el-tab-pane label="关于我的"  name="aboutMe">
        <about-instance v-if="active === 'aboutMe'"  ref="AboutInstance"/>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="发起审批" :visible.sync="openItemDl" :close-on-click-modal="true">
      <initiate-process ref="processInstance" :code="selectForm.processDefinitionKey" v-if="openItemDl"></initiate-process>
      <span slot="footer" class="dialog-footer">
				<el-button size="mini" @click="openItemDl = false">取 消</el-button>
				<el-button size="mini" type="primary" @click="submitForm">提 交</el-button>
			</span>
    </el-dialog>
  </div>
</template>

<script>
import InitiateProcess from "./InitiateProcess";
import InitiatedInstance from "./InitiatedInstance";
import DisposalTask from "./DisposalTask";
import {getProcessDefinitionList} from "@/api/processDefinition";
import {startProcessInstance} from "@/api/processInstance";
import AboutInstance from "./AboutInstance";

export default {
  name: "workSpace",
  components: {AboutInstance, InitiateProcess,DisposalTask,InitiatedInstance},
  data() {
    return {
      active: 'disposalTask',
      openItemDl: false,
      selectForm: {},
      formItem: {},
      actives: [],
      formList: {
        list: [],
        inputs: '',
        searchResult: []
      },
      pending: {
        list: []
      },
    }
  },
  mounted() {
    this.getGroups()
  },
  methods: {
    getGroups() {
      let groupItem = {
        id:100,
        name:"clay测试",
        items:[]
      }
      let query = {
        state: 1
      }
      getProcessDefinitionList(query).then(res=>{
        groupItem.items = res.data.rows
        groupItem.items.forEach(item=>{
          item.logo = JSON.parse(item.logo)
        })
        this.actives.push(groupItem.name)
        this.formList.list.push(groupItem)
      }).catch(err => {
        this.$message.error(err.response.data)
      })
    },
    enterItem(item) {
      this.selectForm = item
      this.openItemDl = true
    },
    // todo 提交流程测试
    submitForm(){
      let processInstance = this.$refs.processInstance;
      let selectUserMap = {}
      this.$store.state.selectUserMap.forEach(((value, key) => {selectUserMap[key]=value}))
      let paramsData = {
        processDefinitionId: this.selectForm.processDefinitionId,
        formData: JSON.stringify(processInstance.formData),
        optionalUser: selectUserMap
      }
      processInstance.validate(valid => {
        if (valid) {
          startProcessInstance(paramsData).then(res=>{
            this.openItemDl = false
            this.$message.success(res.msg)
            this.$refs.disposalTask.getList()
          })
        } else {
          this.$message.warning("请完善表单😥")
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@media screen and (max-width: 1852px) {
  /deep/.el-dialog {
    width: 70%;
  }
  }
@media screen and (max-width: 1766px) {
  /deep/.el-dialog {
    width: 85%!important;
  }
}
@media screen and (max-width: 1660px) {
  /deep/.el-dialog {
    width: 90%!important;
  }
}
@media screen and (max-width: 1560px) {
  /deep/.el-dialog {
    width: 65%;
    //height: 1270px!important;
  }
  /deep/.el-main {
    width: 100%!important;
  }
}

@media screen and (max-width: 1360px) {
  /deep/.el-dialog {
    min-width: 1072px;
  }
}
/deep/.el-dialog {
  width: 80%;
  //height: 830px;
  margin-top: 4vh!important;
}
/deep/.el-main {
  height: 600px!important;
  overflow: auto;
}
/deep/.el-main::-webkit-scrollbar{
  width: 6px;
  height: 6px;
  background-color: #f8f8f8;
}
/deep/.el-main::-webkit-scrollbar-thumb{
  width: 6px;
  background-color: #adacac;
  border-radius: 4px;
}
.workspace {
  padding: 50px 20px;
  position: relative;

  .back {
    position: absolute;
    left: 20px;
    top: 13px;
  }

  .no-data {
    text-align: center;
    padding: 50px 0;
    color: #656565;
    margin: 0 auto;
  }

  /deep/ .el-collapse {
    padding: 0 15px;
    background: #ffffff;

    .el-collapse-item__header {
      font-size: medium;
    }

    .el-collapse-item__wrap {
      padding: 20px 10px;
    }

    .el-tabs--border-card .el-tabs__content {
      padding: 40px 15px;
    }
  }

  .form-item {
    padding: 15px 10px;
    width: 200px;
    cursor: pointer;
    border: 1px solid #d9dada;
    border-radius: 5px;
    float: left;
    margin: 5px 10px;
    height: 37px;

    &:hover {
      border: 1px solid #448ed7;

      span {
        display: inline-block !important;
      }
    }

    i {
      padding: 8px;
      border-radius: 8px;
      float: left;
      font-size: 20px;
      color: #ffffff;
      background: #38adff;
      height: 20px;
      line-height: 20px;
    }

    div {
      height: 35px;
      line-height: 35px;

      div{
        display: inline-block;
        margin-left: 10px;
        width: 100px;
      }

      span {
        display: none;
        float: right;
        color: #38adff;
        font-size: 12px;
      }
    }

    /*span:nth-child(1) {
      float: left;
      margin: 5px 0 0 10px;
    }*/
    /*span:nth-child(1) {
      float: right;
      color: #448ed7;
      font-size: x-small;
      margin: 5px 0 5px 0;
    }*/
  }
}

@media screen and (max-width: 800px) {
  .form-item {
    padding: 12px 10px !important;
    width: 150px !important;
    margin: 5px !important;

    &:hover {
      span:last-child {
        display: none !important;
      }
    }
  }
}
</style>
