<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
      class="app-container--search"
    >
      <el-form-item label="表单key" prop="formKey">
        <el-input
          v-model="queryParams.formKey"
          placeholder="请输入表单key"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单名称" prop="formName">
        <el-input
          v-model="queryParams.formName"
          placeholder="请输入表单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>

    <div class="app-container--toolbar">
      <div class="app-container--toolbar-left">
        <div class="toolbar-title">
          <span>数据列表</span>
        </div>
      </div>
      <div class="app-container--toolbar-right">
        <el-button
          v-hasPermi="['workflow:dynamicForm:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>

        <el-button
          v-hasPermi="['workflow:dynamicForm:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>

        <el-button
          v-hasPermi="['workflow:dynamicForm:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>

        <el-button
          v-hasPermi="['workflow:dynamicForm:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>

        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </div>
    </div>

    <el-table
      v-loading="loading"
      :data="dynamicFormList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="主键" align="center" prop="id" />
      <el-table-column label="表单key" align="center" prop="formKey" />
      <el-table-column label="表单名称" align="center" prop="formName" />
      <el-table-column label="表单备注" align="center" prop="formRemark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['workflow:dynamicForm:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['workflow:dynamicForm:edit']"
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleFormDesigner(scope.row)"
          >表单设计</el-button>
          <el-button
            v-hasPermi="['workflow:dynamicForm:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改流程单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单key" prop="formKey">
          <el-input v-model="form.formKey" placeholder="请输入表单key" />
        </el-form-item>
        <el-form-item label="表单名称" prop="formName">
          <el-input v-model="form.formName" placeholder="请输入表单名称" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNo">
          <el-input-number v-model="form.orderNo" placeholder="排序" />
        </el-form-item>
        <el-form-item label="表单备注" prop="formRemark">
          <el-input
            v-model="form.formRemark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          :loading="buttonLoading"
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDynamicForm,
  getDynamicForm,
  delDynamicForm,
  addDynamicForm,
  updateDynamicForm
} from '@/api/workflow/dynamicForm'

export default {
  name: 'DynamicForm',
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程单表格数据
      dynamicFormList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formKey: undefined,
        formName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        formKey: [
          { required: true, message: '表单key不能为空', trigger: 'blur' }
        ],
        formName: [
          { required: true, message: '表单名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFormDesigner(row) {
      this.$router.push('/workflow/dynamicFormDesigner/' + row.id)
    },
    /** 查询流程单列表 */
    getList() {
      this.loading = true
      listDynamicForm(this.queryParams).then((response) => {
        this.dynamicFormList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        formKey: undefined,
        formName: undefined,
        formDesignerText: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        formRemark: undefined,
        status: true,
        orderNo: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加流程单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getDynamicForm(id).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改流程单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateDynamicForm(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addDynamicForm(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除流程单编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delDynamicForm(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'workflow/dynamicForm/export',
        {
          ...this.queryParams
        },
        `dynamicForm_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
