<template>
  <div class="app-container">
    <!-- 按钮操作栏 -->
    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          @click="handleSave"
        >保存</el-button>
      </el-col>
    </el-row>

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="表单id" prop="id">
            <el-input
              v-model="queryParams.id"
              placeholder="表单id由系统生成"
              clearable
              disabled
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="表单名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入表单名"
              clearable
              :disabled="!!$route.query.formId"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="表单状态" prop="status">
            <el-input
              v-model="queryParams.status"
              placeholder="请输入表单状态"
              clearable
              :disabled="!!$route.query.formId"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              :disabled="!!$route.query.formId"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="表" prop="tableIdHeader">
            <formNameSelect
              v-model="tableParams.tableIdHeader"
              filterable
              clearable
              @change="formNameSelectChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="表名" prop="classComment">
            <el-input
              v-model="tableParams.classComment"
              placeholder="请输入表名"
              clearable
              :disabled="!!$route.query.formId"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表-表体 -->
    <el-table v-loading="loading" :data="entityTableList" border>
      <el-table-column label="表" align="center" prop="tableName">
        <template slot-scope="scope">
          <formNameSelect v-model="scope.row.tableName" :selected-ids="selectedIds" @change="tableSelectChange($event,scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="表名" align="center" prop="classComment" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180"
        fixed="right"
      >
        <template slot-scope="{row,$index}">
          <el-button
            type="text"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(row,$index)"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            :disabled="entityTableList.length === 1"
            @click="handleDelete(row,$index)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getForm, createForm, updateForm } from '@/api/bpm/form'
import formNameSelect from './formNameSelect.vue'

export default {
  name: 'BpmFormDetail',
  components: {
    formNameSelect
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流的列表
      list: [],
      entityTableList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        status: null
      },
      tableParams: {
        tableName: null,
        classComment: null
      },
      formId: null,
      // 表单详情
      detailOpen: false,
      selectedIds: [],
      detailForm: {
        fields: []
      }
    }
  },
  created() {
    if (this.$route.query.formId || this.formId) {
      this.getList()
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      getForm(this.$route.query.formId || this.formId).then(res => {
        this.queryParams = res.data || {}
        this.entityTableList = res.data.entityTableList || []
        this.tableParams = res.data.headerTable || {}
        this.tableParams.tableIdHeader = res.data.headerTable.id
        if (this.entityTableList.length === 0 || !this.entityTableList) {
          this.entityTableList.push({
            tableName: '',
            tableId: '',
            classComment: '',
            createTime: '',
            id: this.entityTableList.length
          })
        }
      }).finally(() => {
        this.loading = false
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    tableSelectChange(v, row) {
      const { classComment, tableName, id, createTime } = v
      row.id = id
      row.createTime = createTime
      row.classComment = classComment
      row.tableName = tableName
      this.selectedIds = this.entityTableList.map(item => {
        const { id } = item
        return id
      })
    },
    /** 新增按钮操作 */
    handleAdd(row, $index) {
      this.entityTableList.push({
        tableName: '',
        tableId: '',
        classComment: '',
        createTime: '',
        id: this.entityTableList.length
      })
      this.selectedIds = this.entityTableList.map(item => {
        const { id } = item
        return id
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id } = row
      const index = this.entityTableList.findIndex(item => item.id === id)
      this.entityTableList.splice(index, 1)
      this.selectedIds = this.entityTableList.map(item => {
        const { id } = item
        return id
      })
    },
    formNameSelectChange(v) {
      console.log(v)
      this.tableParams.classComment = v.classComment
    },
    // 保存
    handleSave() {
      const tableIdsEntity = this.entityTableList.map(item => {
        const { id } = item
        return id
      }).join(',')
      const params = {
        id: this.$route.query.formId || null,
        name: this.queryParams.name,
        status: this.queryParams.status,
        tableIdsEntity,
        conf: null,
        fields: [],
        tableIdHeader: this.tableParams.tableIdHeader
      }
      if (this.$route.query.formId) {
        updateForm(params).then(res => {
          this.$modal.msgSuccess('修改成功!')
          this.getList()
        })
        return
      }
      createForm(params).then(res => {
        this.$modal.msgSuccess('创建成功!')
        this.formId = res.data
        this.getList()
      })
    }
  }
}
</script>
