<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as DictTypeApi from '@/api/system/dict/dict.type'
import { CommonStatusEnum } from '@/utils/constants'

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const state = ref(false)
const type = ref<any>(null)
const params = ref({
  id: undefined,
  name: '',
  type: '',
  status: CommonStatusEnum.ENABLE,
  remark: ''
})

const formRef = ref(null)

const rules = reactive<any>({
  name: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '字典类型不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
})

const popName = computed(() => {
  return {
    check: '查看',
    edit: '编辑',
    new: '新增'
  }[type.value]
})

const goClose = () => {
  state.value = false
}

const onClosed = () => {
  // 数据清除
  params.value = {
    id: undefined,
    name: '',
    type: '',
    status: CommonStatusEnum.ENABLE,
    remark: ''
  }
}

// 提交
const onSubmit = async () => {
  const base = await new Promise((resolve) => {
    formRef.value?.validate((e) => {
      resolve(e)
    })
  })
  if (!base) {
    return ElMessage.error('请完成数据填写！')
  }

  if (type.value === 'create') {
    //新增
    await DictTypeApi.createDictType(params.value)
  } else {
    //修改
    await DictTypeApi.updateDictType(params.value)
  }

  ElMessage.success(type.value === 'create' ? '创建成功！' : '修改成功！')

  state.value = false

  emit('success')
}

/** 打开弹窗 */
const open = async (typeString: string, id?: number) => {
  state.value = true
  type.value = typeString
  // resetForm()
  // 修改时，设置数据
  if (id) {
    params.value = await DictTypeApi.getDictType(id)
  }
}

/** 重置表单 */
const resetForm = () => {
  params.value = {
    id: undefined,
    type: '',
    name: '',
    status: CommonStatusEnum.ENABLE,
    remark: ''
  }
  // formRef.value?.resetFields()
}
defineExpose({
  open
})
</script>

<template>
  <el-dialog
    class="org-model"
    v-model="state"
    width="600"
    :destroy-on-close="true"
    :align-center="true"
    @closed="onClosed"
  >
    <div class="title">
      <div class="text">{{ popName }}新增</div>
      <Icon @click="goClose" class="close" icon="ep:close" />
    </div>

    <div :class="['content', { disabled: type === 'check' }]">
      <el-form ref="formRef" :model="params" :rules :show-message="false">
        <div class="block-container road-setting">
          <el-form-item prop="name" label="字典名称：">
            <el-input v-model="params.name" placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="type" label="字典类型：">
            <el-input
              v-model="params.type"
              placeholder="请输入..."
              :readonly="type == 'create' ? false : true"
            />
          </el-form-item>

          <el-form-item prop="status" label="状态：">
            <el-radio-group v-model="params.status">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item prop="remark" label="备注：">
            <el-input type="textarea" rows="3" v-model="params.remark" placeholder="请输入..." />
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="footer">
        <el-button @click="goClose">取消</el-button>
        <el-button @click="onSubmit" type="primary">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
$base_url: '@/assets/images/home/';

.title {
  height: 48px;
  display: flex;
  align-content: center;
  align-items: center;
  color: white;
  padding: 0 20px;
  font-size: 18px;
  font-weight: 600;
  justify-content: space-between;
  .close {
    cursor: pointer;
  }
}

.content {
  padding: 0 16px;
  // height: 600px;

  .block-container {
    padding-right: 24px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-top: 20px;
  }

  &.disabled {
    :deep(.base-block) {
      pointer-events: none;
    }
    :deep(.road-setting) {
      pointer-events: none;
    }
    :deep(.el-input) {
      background: rgba($color: #ffffff, $alpha: 0.2);
    }
  }
}

.footer {
  // background: rgba(69, 116, 182, 0.36);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 24px;
  color: white;
}
</style>

<style lang="scss">
.org-model {
  padding: 0;
  box-sizing: border-box;
  // border: 1px solid rgba(255, 255, 255, 0.5);
  background: #0d3561;
  border-radius: 4px;

  .el-dialog__header {
    display: none;
  }
  .el-dialog__footer {
    padding-top: 0;
  }
  .def-btn {
    background: #1270ff;
    border: 1px solid rgba(155, 193, 255, 0.74);
    height: 28px;
    padding: 8px 10px;
    color: white;
    &:hover {
      border: 1px solid rgba(255, 255, 255, 0.8);
    }
  }
}
</style>
