<template>
  <div>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="state.queryForm.name" placeholder="流程名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.queryForm.key" placeholder="流程编码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.queryForm.category" placeholder="流程分类" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-auth="'ebo:flow:flowmanager:save'" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
    class="flex-content-1" height="100%"
        v-loading="state.dataListLoading"
        show-overflow-tooltip
        :data="state.dataList"
        border
        style="width: 100%"
        @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="key" label="流程编码" header-align="center" align="center"></el-table-column>
      <el-table-column prop="name" label="流程名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="category" label="流程类型" header-align="center"
                       align="center"></el-table-column>
      <el-table-column prop="version" label="流程版本" header-align="center"
                       align="center" width="90"></el-table-column>
      <el-table-column prop="deploymentId" label="是否部署" header-align="center"
                       align="center" width="90">
        <template #default="scope">
          {{ scope.row.deploymentId && scope.row.deploymentId!=''?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"
                       width="190"></el-table-column>
      <el-table-column prop="lastUpdateTime" label="更新时间" header-align="center" align="center"
                       width="190"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="230">
        <template #default="scope">

          <el-button v-auth="'ebo:flow:flowmanager:design'" type="primary" link
                     @click="designHandle(scope.row.id)"> 设计
          </el-button>
          <el-button v-auth="'ebo:flow:flowmanager:deployment'" type="primary" link
                     @click="deploymentHandle(scope.row.id)"> 部署
          </el-button>
          <el-button v-auth="'ebo:flow:flowmanager:update'" type="primary" link
                     @click="addOrUpdateHandle(scope.row.id)"> 修改
          </el-button>
          <el-button v-auth="'ebo:flow:flowmanager:delete'" type="primary" link
                     @click="deleteBatchHandle(scope.row.id)"> 删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="state.page"
        :page-sizes="state.pageSizes"
        :page-size="state.limit"
        :total="state.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>

  </div>
</template>

<script setup lang="ts" name="flowFlowmanagerIndex">
import {useCrud} from '@/hooks'
import {reactive, ref} from 'vue'
import {IHooksOptions} from '@/hooks/interface'
import {ElMessage, ElMessageBox} from "element-plus";
import {deploymentFlowApi} from "@/views/ebo/flow/api/flowmanager";
import AddOrUpdate from "@/views/ebo/flow/flowmanager/add-or-update.vue";

const state: IHooksOptions = reactive({
  dataListUrl: '/flow/template/page',
  deleteUrl: '/flow/template',
  queryForm: {
    name: '',
    key: '',
    category: ''
  }
})
const designHandle=(id : any)=>{
    window.open(import.meta.env.VITE_API_URL+"/modeler/modeler.html?modelId="+id);
}

const deploymentHandle=(id : any)=>{
  ElMessageBox.confirm('请确定要部署吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    var dt = {
      "modelId":id
    }
    deploymentFlowApi(dt).then(() => {
      ElMessage.success({
        message: '部署成功',
        duration: 500,
        onClose: () => {
          getDataList()
        }
      })
    })
  })
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: string) => {
  addOrUpdateRef.value.init(id)
}


const {getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle} = useCrud(state)
</script>
