<template>
  <div>
    <xy-dialog ref="dialog" :width="60" :is-show.sync="isShow" :type="'form'"
      :title="type === 'add' ? '新增数据菜单' : '编辑数据菜单'" :form="form" :rules='rules' @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>配置名称：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" :disabled="type === 'editor'" placeholder="请输入配置名称" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:key>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>英文标识：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.key" :disabled="type === 'editor'" placeholder="请输入英文标识" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:value>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>配置值：
          </div>
          <div class="xy-table-item-content">
            <div style="width: 100%;">
              <div style="margin-bottom: 10px;">
                <el-button type="primary" size="small" @click="addTableRow">新增</el-button>
              </div>
              <el-table :data="tableData" border style="width: 100%;">
                <el-table-column prop="name" label="名称" width="200">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.name" placeholder="请输入名称" size="small"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="key" label="英文标识" width="200">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.key" placeholder="请输入英文标识" size="small"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="path" label="路径" min-width="300">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.path" placeholder="请输入路径" size="small"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="100" align="center">
                  <template slot-scope="scope">
                    <el-button type="danger" size="mini" @click="removeTableRow(scope.$index)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/info/configs.js"
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        form: {
          name: '',
          key: '',
          value: ''
        },
        tableData: [],
        rules: {
          name:[{
            required:true,
            message:'请输入配置名称'
          }],
          key:[{
            required:true,
            message:'请输入英文标识'
          }]
        }
      }
    },
    created() {},
    methods: {
      addTableRow() {
        this.tableData.push({
          name: '',
          key: '',
          path: ''
        })
      },
      removeTableRow(index) {
        this.tableData.splice(index, 1)
      },
      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        // 将表格数据转换为JSON字符串
        this.form.value = JSON.stringify(this.tableData)
        save(this.form).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增成功' : '编辑成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.requestToForm(res,this.form)
          // 解析JSON字符串为表格数据
          if (this.form.value) {
            try {
              this.tableData = JSON.parse(this.form.value)
              if (!Array.isArray(this.tableData)) {
                this.tableData = []
              }
            } catch (e) {
              console.error('解析配置值失败:', e)
              this.tableData = []
            }
          } else {
            this.tableData = []
          }
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          } else {
            // 新增时重置表格数据
            this.tableData = []
          }
        } else {
          this.id = ''
          this.form = {
            name: '',
            key: '',
            value: ''
          }
          this.tableData = []
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .name,
  ::v-deep .key,
  ::v-deep .value {
    flex-basis: 100%;
  }
</style>
