<template>
  <el-dialog title="批量新增" :close-on-click-modal="false" :close-on-press-escape="false"
    :visible.sync="visible" lock-scroll class="NCC-dialog NCC-dialog_center" width="600px">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px"
      label-position="top" v-loading="formLoading">
      <!--      <el-form-item label="绑定表格" prop="bindTable">-->
      <!--        <el-input v-model="dataForm.bindTable" placeholder="输入绑定表格" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="表格描述" prop="bindTableName">-->
      <!--        <el-input v-model="dataForm.bindTableName" placeholder="绑定表格描述" />-->
      <!--      </el-form-item>-->
      <div class="json-demo">
        <pre>
          // 示例
          [
            {
              "fullName":"名称",
              "enCode":"fullName"
            }
          ]
        </pre>
      </div>
      <el-form-item label="字段Json" prop="columnJson">
        <div class="formCodeEditor">
          <NCCCodeEditor :options="options" v-model="content" ref="CodeEditor" />
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
        {{$t('common.confirmButton')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { batchCreateColumn } from '@/api/system/columnAuthorize'
import NCCCodeEditor from '@/components/NCCEditor/monaco'

export default {
  components: { NCCCodeEditor },
  data() {
    return {
      options: {
        readOnly: false,
        language: 'json'
      },
      visible: false,
      formLoading: false,
      btnLoading: false,
      content: '',
      dataForm: {
        moduleId: '',
        bindTable: '',
        bindTableName: '',
        columnJson: []
      },
      dataRule: {}
    }
  },
  methods: {
    init(moduleId) {
      this.dataForm.moduleId = moduleId
      this.visible = true
      this.formLoading = true
      this.$nextTick(() => {
        this.content = ''
        this.$refs['dataForm'].resetFields()
        this.$refs.CodeEditor.changeEditor({
          value: '',
          options: this.options
        })
        this.formLoading = false
      })
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const rtnData = this.content
          if (!rtnData) return this.$message.warning('请输入字段JSON')
          const fixedRtnData = rtnData.replace(/("\w+":)(?=[},])/g, '$1null')
          const jsonData = JSON.parse(fixedRtnData)
          this.dataForm.columnJson = jsonData
          this.btnLoading = true
          batchCreateColumn(this.dataForm).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
                this.content = ''
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => { this.btnLoading = false })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.formCodeEditor {
  width: 100%;
  height: 260px;
  margin: 0;
  padding: 0;
  border: 1px solid #c0c4cc;
  overflow: hidden;
}
.json-demo {
  width: 100%;
  background: #f4f4f5;
  border-radius: 4px;
  color: #909399;
  padding-top: 10px;
  pre {
    margin-left: -40px;
    font-size: 12px;
  }
}
</style>