<template>
  <div class="dictionary-edit">
    <el-dialog v-bind="$attrs" :before-close="cancelDialog" :append-to-body="true" :destroy-on-close="true"
      :close-on-click-modal="false" :close-on-press-escape="false" :lock-scroll="false" class="role-dialog" draggable>
      <el-form ref="dictRef" :model="state.form" :rules="state.rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="配置名称" prop="title">
              <el-input v-model="state.form.title" placeholder="请输入配置名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="配置值" prop="substance">
              <el-input v-model="state.form.substance" placeholder="请输入配置值" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分组名称" prop="groupname">
              <el-input v-model="state.form.groupname" placeholder="请输入分组名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上级ID" prop="baseid">
              <el-input v-model="state.form.baseid" placeholder="请输入上级ID" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="配置描述" prop="describes">
              <el-input v-model="state.form.describes" type="textarea" :rows="4" placeholder="请输入配置描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useBaseStore from '@store/modules/base'
import { filterObject } from '@/utils/index'

const baseStore = useBaseStore()
const { proxy } = getCurrentInstance()
const mitter = proxy.mitter
const dictRef = ref(null)

const props = defineProps({
  keys: {
    type: Object,
  },
  modalType: {
    type: String,
  },
})

watchEffect(() => {
  if (props.keys.id && props.modalType === 'edit') getDataView(props.keys.id)
})

const state = reactive({
  form: {
    id: '',
    title: '',
    substance: '',
    groupname: '',
    baseid: '',
    describes: '',
    ispublic: true,
    available: true,
  },
  rules: {
    title: [{ required: true, message: '配置名称不能为空', trigger: 'blur' }],
    substance: [{ required: true, message: '配置值不能为空', trigger: 'blur' }],
    groupname: [{ required: true, message: '分组名称不能为空', trigger: 'blur' }],
    baseid: [{ required: true, message: '上级ID不能为空', trigger: 'blur' }],
    describes: [{ required: true, message: '配置描述不能为空', trigger: 'blur' }],
  },
})

// 拉取修改内容详情接口
const getDataView = async (id) => {
  const result = await baseStore.configurationDataview({ 'id.Value': id })
  state.form = filterObject(result, Object.keys(state.form))
  console.log(state.form, '更新')
}

// 确定按钮
const submitDialog = () => {
  dictRef.value.validate(async (valid) => {
    if (valid) {
      switch (props.modalType) {
        case 'edit':
          await baseStore.configurationUp(state.form)
          proxy.$modal.msgSuccess('修改成功！')
          dictRef.value.resetFields()
          break

        case 'add':
          console.log(state.form)
          await baseStore.configurationAdd(state.form)
          proxy.$modal.msgSuccess('新增成功！')
          dictRef.value.resetFields()
          break

        default:
          break
      }
      mitter.emit('onEditFinish', { refresh: true })
    }
  })
}

const cancelDialog = () => {
  mitter.emit('onEditFinish', false)
  dictRef.value.resetFields()
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
