<template>
  <div>
    <div class="tab_top">
      <p class="tab_tittle">页签管理</p>
      <el-button size="small" @click="addFormClick">添加页签</el-button>
    </div>
    <el-table :data="tabData" border>
      <el-table-column prop="tabName" label="页签名称"></el-table-column>
      <el-table-column prop="tabFormName" label="页签表单名称">
        <template slot-scope="scope">
          <span>{{ scope.row.tabFormName }}({{ scope.row.tabFormId }})</span>
        </template>
      </el-table-column>
      <el-table-column prop="tabColumnName" label="页签表单字段">
        <template slot-scope="scope">
          <span>{{ scope.row.tabColumnComment }}({{ scope.row.tabColumnName }})</span>
        </template>
      </el-table-column>
      <el-table-column prop="columnName" label="主表单字段">
        <template slot-scope="scope">
          <span>{{ scope.row.columnComment }}({{ scope.row.columnName }})</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="editTab(scope.row)">编辑</el-button>
          <el-button size="mini" @click="deleteTab(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog :title="addForm.id ? '页签编辑' : '新增页签'" :visible.sync="addFormVisible" append-to-body width="40%">
        <el-form :model="addForm" label-position="left" label-width="100px" ref="addForm">
          <el-form-item label="页签名称">
            <el-input v-model="addForm.tabName" placeholder="请输入页签名称"></el-input>
          </el-form-item>
          <el-form-item label="页签表单">
            <el-select v-model="addForm.tabFormId" filterable placeholder="请选择页签表单" @change="onTabFormId" style="width: 100%">
              <el-option :key="item.id" :label="item.fullName + '(' + item.id + ')'" :value="item.id" v-for="item in tabFormList" />
            </el-select>
          </el-form-item>
          <el-form-item label="页签表单字段" v-if="addForm.tabFormId">
            <el-select v-model="addForm.tabFieldId" filterable placeholder="请选择页签表单字段" @change="onTabFieldId" style="width: 100%">
              <el-option :key="item.id" :label="item.columnTitle + '(' + item.columnName + ')'" :value="item.id" v-for="item in tabCubeList" />
            </el-select>
          </el-form-item>
          <el-form-item label="主表单字段">
            <el-select v-model="addForm.fieldId" filterable placeholder="请选择主表单字段" @change="onFieldId" style="width: 100%">
              <el-option :key="item.id" :label="item.columnTitle + '(' + item.columnName + ')'" :value="item.id" v-for="item in formItemList" />
            </el-select>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="addForm.tabSort" placeholder="请输入排序"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addCustomTab, customTabList, deleteCustomTab, getCateInfo, getVisualDevInfo } from '@/api/onlineDev/visualDev'

export default {
  name: 'tabManage',
  props: ['tables', 'dbSourceId', 'primaryTableName', 'modelId', 'activeName'],
  data() {
    return {
      tabData: [],
      addFormVisible: false,
      addForm: {
        id: '',
        tabName: '', //页签名称
        tableName: '', //物理表名称
        fieldId: '', //表单关联字段数据集ID
        columnName: '', //表单关联字段列名称
        columnComment: '', //表单关联字段列描述
        tabFormId: '', //页签表单ID
        tabTableName: '', //页签表单物理表名称
        tabFieldId: '', //页签关联字段数据集ID
        tabColumnName: '', //页签关联字段列名称
        tabColumnComment: '', //页签关联字段列描述
        tabSort: '' //	排序字段
      },
      tabFormList: [],
      tabCubeList: []
    }
  },
  created() {
    this.getIntoList()
    this.getTabList()
  },
  computed: {
    formItemList() {
      return this.$store.state.generator.dbItemList
    }
  },
  watch: {
    activeName: {
      deep: true,
      handler(val) {
        if (val && val === 'tabManage') {
          this.getTabList()
        }
      }
    }
  },
  methods: {
    getTabList() {
      let params = {
        current: 1,
        size: 1000
      }
      customTabList(this.modelId, params).then((res) => {
        this.tabData = res.data.data.records
      })
    },
    // 获取列表方法
    getIntoList() {
      getCateInfo({ current: 1, size: 1000 }).then((res) => {
        this.tabFormList = res.data.data.records || []
      })
    },
    // 切换页签表单
    onTabFormId(val) {
      getVisualDevInfo(val).then((res) => {
        this.addForm.tabTableName = res.data.data.primaryTableName
        let columnData = JSON.parse(res.data.data.columnData)
        this.tabCubeList = columnData.cubeList
      })
    },
    // 切换页签表单字段
    onTabFieldId(val) {
      this.tabCubeList.forEach((item) => {
        if (item.id === val) {
          this.addForm.tabColumnName = item.columnName
          this.addForm.tabColumnComment = item.columnComment
        }
      })
    },
    // 切换主表单字段
    onFieldId(val) {
      this.formItemList.forEach((item) => {
        if (item.id === val) {
          this.addForm.columnName = item.columnName
          this.addForm.columnComment = item.columnComment
        }
      })
    },
    // 打开弹框 并将表单置空
    addFormClick() {
      this.addFormVisible = true
      Object.keys(this.addForm).forEach((key) => {
        this.addForm[key] = ''
      })
      this.addForm.tableName = this.primaryTableName
    },
    // 新增/编辑页签
    onSubmit() {
      addCustomTab(this.modelId, this.addForm).then((res) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.addFormVisible = false
        this.getTabList()
      })
    },
    // 点击修改页签
    editTab(row) {
      this.addFormVisible = true
      this.onTabFormId(row.tabFormId)
      this.$nextTick(() => {
        let newRow = Object.assign({}, row)
        for (let key in this.addForm) {
          this.addForm[key] = newRow[key]
        }
        this.addForm.tableName = this.primaryTableName
        this.addForm.fieldId = Number(row.fieldId)
        this.addForm.tabFieldId = Number(row.tabFieldId)
      })
    },
    //  删除页签
    deleteTab(id) {
      this.$confirm('是否删除该页签?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = new URLSearchParams()
        param.append('id', id)
        deleteCustomTab(param).then((res) => {
          this.$message({
            message: res.data.msg,
            type: 'success'
          })
          this.getTabList()
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.tab_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1px solid #cccccc;
  .tab_tittle {
    font-size: 18px;
  }
}
</style>
