<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="业务编码" prop="modelCode">
            <el-input
              v-model="queryParams.modelCode"
              placeholder="请输入业务编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="业务名称" prop="modelName">
            <el-input
              v-model="queryParams.modelName"
              placeholder="请输入业务名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="业务描述" prop="modelComment">
            <el-input
              v-model="queryParams.modelComment"
              placeholder="请输入业务描述"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <EasyAdvancedQuery class="mr10" label-width="98px" @advQuery="advQuery">
              <template #default="{model}">
                <el-row :gutter="10" type="flex">
                  <el-col :span="12">
                    <el-form-item label="状态" prop="modelEnable">
                      <DictSelect v-model="model.modelEnable" placeholder="请选择状态" dict-type="WORKFLOW_ENABLE_FLAG" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="工作流启用标记" prop="bpmEnable">
                      <DictSelect v-model="model.bpmEnable" placeholder="请选择工作流启用标记" dict-type="WORKFLOW_ENABLE_FLAG" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="创建时间" prop="createTime">
                      <el-date-picker
                        v-model="queryParams.createTime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </template>
            </EasyAdvancedQuery>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['infra:model-def:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['infra:model-def:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <template #default="{height}">
        <el-table
          v-loading="loading"
          :height="height"
          :data="list"
          border
        >
          <el-table-column
            label="业务编码"
            align="center"
            prop="modelCode"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="业务名称"
            align="center"
            prop="modelName"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="业务描述"
            align="center"
            prop="modelComment"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="状态"
            align="center"
            prop="modelEnable"
            width="80"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WORKFLOW_ENABLE_FLAG" :value="scope.row.modelEnable" />
            </template>
          </el-table-column>
          <el-table-column
            label="表单id"
            align="center"
            prop="formId"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="页签选项"
            align="center"
            prop="pageShow"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="工作流启用标记"
            align="center"
            prop="bpmEnable"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WORKFLOW_ENABLE_FLAG" :value="scope.row.bpmEnable" />
            </template>
          </el-table-column>
          <el-table-column
            label="工作流程ID"
            align="center"
            prop="bpmId"
            width="110"
            show-overflow-tooltip
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="120"
            class-name="small-padding fixed-width"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['infra:model-def:update']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                v-hasPermi="['infra:model-def:delete']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                class="text-danger"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { deleteModelDef, getModelDefPage, exportModelDefExcel } from '@/api/bpm/processModel'

export default {
  name: 'ModelDef',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务模型定义列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        modelCode: null,
        modelName: null,
        modelComment: null,
        remark: null,
        modelEnable: null,
        formId: null,
        pageShow: null,
        bpmEnable: null,
        bpmId: null,
        createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        modelCode: [{ required: true, message: '业务编码不能为空', trigger: 'blur' }],
        modelName: [{ required: true, message: '业务名称不能为空', trigger: 'blur' }],
        modelComment: [{ required: true, message: '业务描述不能为空', trigger: 'blur' }],
        modelEnable: [{ required: true, message: '0=禁用；1=启用不能为空', trigger: 'blur' }],
        formId: [{ required: true, message: '表单id-[bpm_form]不能为空', trigger: 'blur' }],
        pageShow: [{ required: true, message: '页签选项：不能为空', trigger: 'blur' }],
        bpmEnable: [{ required: true, message: '工作流启用标记，0=禁用；1=启用不能为空', trigger: 'blur' }],
        bpmId: [{ required: true, message: '工作流程ID [act_re_model.ID_]不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.advParams
      }
      getModelDefPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        modelCode: undefined,
        modelName: undefined,
        modelComment: undefined,
        remark: undefined,
        modelEnable: undefined,
        formId: undefined,
        pageShow: undefined,
        bpmEnable: undefined,
        bpmId: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.advParams = {}
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$router.push({ path: '/bpm/manager/model-def-detail' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const { id } = row
      this.$router.push({ path: '/bpm/manager/model-def-detail', query: { id }})
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除业务模型定义编号为"' + id + '"的数据项?').then(function() {
        return deleteModelDef(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有业务模型定义数据项?').then(() => {
        this.exportLoading = true
        return exportModelDefExcel(params)
      }).then(response => {
        this.$download.excel(response, '业务模型定义.xls')
        this.exportLoading = false
      }).catch(() => {})
    },
    // 高级查询
    advQuery(advQueryParams) {
      this.advParams = advQueryParams
      this.handleQuery()
    }
  }
}
</script>
