<!--
  @description: 模型管理
  @Modifier: haoFx
  @routerPath:/flow/model
  @date: 2020/3/26-15:10
-->
<template>
  <div>
    <!--标题-->
    <div class="title title_border p0-16">
      <i class="el-icon-s-home" />模型管理
    </div>
    <section class="p0-16">
      <!--查询条件-->
      <div class="crud_menu_title clearfix">
        <span class="icon-line fl">查询条件</span>
      </div>
      <div :class="{'SearchContent_show':toggleSearchStatus}" class="clearfix SearchContent">
        <div class="SearchContent_leftInput fl">
          <el-form
            ref="searchForm"
            :model="searchForm"
            inline
          >
            <el-form-item label="模型标识" prop="modelKey">
              <el-input v-model.trim="searchForm.modelKey" placeholder="模型标识" style="width:268px" />
            </el-form-item>
            <el-form-item label="模型名称" prop="name">
              <el-input v-model.trim="searchForm.name" placeholder="流程分类" style="width:268px" />
            </el-form-item>
          </el-form>
        </div>
        <div class="fr">
          <el-button type="primary" @click="onSearch">
            搜 索
          </el-button>
          <el-button plain type="info" @click="resetForm('searchForm')">
            重 置
          </el-button>
        </div>
      </div>
      <!--查询结果-->
      <div class="crud_menu_title">
        <span class="icon-line">查询结果</span>
      </div>
      <div class="mb10">
        <el-button
          type="primary"
          icon="el-icon-circle-plus"
          plain
          @click="handleCreate"
        >
          创 建
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
        >
          删 除
        </el-button>
      </div>
      <div>
        <el-table
          ref="smallTable"
          v-loading="loading"
          :data="tableData"

          @selection-change="selectionChange"
        >
          <el-table-column
            align="center"
            type="selection"
            width="55"
          />
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60"
          />
          <el-table-column
            header-align="center"
            prop="id"
            label="模型主键"
            min-width="230"
          />
          <el-table-column
            header-align="center"
            prop="modelKey"
            label="模型标识"
            min-width="150"
          />
          <el-table-column
            header-align="center"
            label="模型名称"
            prop="name"
            align="left"
            min-width="150"
          />
          <el-table-column
            header-align="center"
            label="流程版本"
            prop="version"
            align="left"
            width="80"
          />
          <el-table-column
            header-align="center"
            label="创建时间"
            prop="created"
            align="center"
            min-width="150"
          >
            <template slot-scope="scope">
              {{ scope.row.created && $Moment(scope.row.created).format(momentDateTime) }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="更新时间"
            prop="lastUpdated"
            align="center"
            min-width="150"
          >
            <template slot-scope="scope">
              {{ scope.row.lastUpdated && $Moment(scope.row.lastUpdated).format(momentDateTime) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click.stop="handleUpdate(scope.row,scope.index)"
              >
                配置
              </el-link>
              <el-link
                type="primary"
                @click.stop="handleDeploy(scope.row,scope.index)"
              >
                部署
              </el-link>
              <el-link
                type="primary"
                @click.stop="handleDownload(scope.row,scope.index)"
              >
                下载
              </el-link>
              <el-link
                type="primary"
                @click.stop="handleSlotDelete(scope.row,scope.index)"
              >
                删除
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination :pagination-data="paginationData" @change="loadData" />
      </div>
    </section>
    <el-dialog
      :visible.sync="flowBox"
      :fullscreen="true"
      title="流程配置"
      custom-class="el-dialog__body-p0"
    >
      <iframe
        :src="flowUrl"
        width="100%"
        height="700"
        title="流程设计器"
        frameBorder="no"
        border="0"
        marginWidth="0"
        marginHeight="0"
        scrolling="no"
        allowTransparency="yes"
      />
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button type="primary" @click="handleRefresh">确 定</el-button>
        <el-button @click="flowBox = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="deployBox"
      title="流程部署"
      width="20%"
    >
      <el-form
        ref="form"
        :model="searchForm"
        label-width="80px"
      >
        <el-form-item label="流程类型">
          <el-select v-model="categoryValue" filterable placeholder="请选择" value="">
            <el-option
              v-for="item in category"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button type="primary" @click="handleDoDeploy">确 定</el-button>
        <el-button @click="deployBox = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { modelList, removeModel, deployModel } from '@api/work/flow'
import { getDictionary } from '@api/work/dict'
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'
/* 组件*/
import pagination from '@comp/common/pagination/pagination'
/* web页面全局配置文件 */
import website from '@/assets/js/config/website.js'
/* 工作流统一分类格式*/
import { flowCategory } from '@assets/js/workutils/flow'
export default {
  components: {
    pagination
  },
  mixins: [ListMixin],
  data() {
    return {
      loading: false,
      searchForm: {
        modelKey: '', // 模型标识
        name: '' // 模型名称
      },
      selectionId: '',
      selectionList: [],
      deployBox: false, // 流程部署弹框
      flowBox: false, // 流程配置弹框
      flowUrl: '',
      category: [],
      categoryValue: '',
      tableData: []
    }
  },
  computed: {
    ids() {
      const ids = []
      this.selectionList.forEach(ele => {
        ids.push(ele.id)
      })
      return ids.join(',')
    }
  },
  created() {
    // 获取模型管理
    this.loadData(this.paginationData)
  },
  methods: {
    selectionChange(list) {
      this.selectionList = list
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeModel(this.ids)
        })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.loadData(this.paginationData)
        })
    },
    handleCreate() {
      this.flowUrl = `${website.flowDesignUrl}/index.html`
      this.flowBox = true
    },
    // 操作-配置
    handleUpdate(row) {
      this.flowUrl = `//${website.flowDesignUrl}/index.html#/editor/${row.id}`
      this.flowBox = true
    },
    // 操作-部署
    handleDeploy(row) {
      this.deployBox = true
      this.selectionId = row.id
    },
    // 操作-下载
    handleDownload(row) {
      window.open(`${website.flowDesignUrl}/app/rest/models/${row.id}/bpmn20`)
    },
    // 流程部署-确定按钮
    handleDoDeploy() {
      if (!this.categoryValue) {
        this.$message({
          type: 'warn',
          message: '请先选择流程类型!'
        })
      }
      deployModel({ modelId: this.selectionId, category: flowCategory(this.categoryValue) }).then(res => {
        const data = res.data
        if (data.success) {
          this.$message({
            type: 'success',
            message: data.msg
          })
          this.deployBox = false
        } else {
          this.$message({
            type: 'warn',
            message: data.msg
          })
        }
      })
    },
    // 操作-删除
    handleSlotDelete(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeModel(row.id)
        })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.loadData(this.paginationData)
        })
    },
    // 流程配置弹框确定
    handleRefresh() {
      this.flowBox = false
      this.loadData(this.paginationData)
    },
    // 加载数据
    loadData(page, params = {}) {
      const values = {
        ...params
      }
      modelList(page.pageNo, page.pageSize, Object.assign(values, this.searchForm))
        .then(response => {
          this.loading = false
          const { data } = response.data
          this.tableData = data.records
          this.paginationData.total = data.total
        }).catch(() => {
          this.loading = false
        })
      getDictionary({ code: 'flow' }).then(res => {
        this.category = res.data.data
      })
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData(this.paginationData)
    },
    // 重置
    resetForm(formName) {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.$refs[formName].resetFields()
      this.loadData(this.paginationData)
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.el-dialog.is-fullscreen{
    .el-dialog__body{
      height: calc(100% - 38px - 53px);
    }
  }
</style>
