<template>
  <view class="container" style="paddingBottom:1rpx;">
    <view class="section">
      <view class="section-title">{{ title }}</view>
      <view class="form-view">
        <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="140rpx"
          :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
          <u-form-item label="字典名称" prop="dictName" required>
            <u--input v-model="form.dictName" placeholder="请输入字典名称"
              inputAlign="left" :customStyle="getInputStyle('dictName')"></u--input>
          </u-form-item>

          <u-form-item label="字典类型" prop="dictType" required>
            <u--input v-model="form.dictType" placeholder="请输入字典类型"
              inputAlign="left" :customStyle="getInputStyle('dictType')"></u--input>
          </u-form-item>

        <u-form-item label="备注" prop="remark" labelPosition="top">
           <u--textarea v-model="form.remark" placeholder="请输入备注"   inputAlign="right" count
              maxlength="500" style="border: 2rpx solid #dcdfe6 !important;height：160rpx"></u--textarea>
          </u-form-item>
        </u--form>
        <view class="form-btn">
          <u-button type="primary" text="提交" @click="submit"></u-button>
        </view>
      </view>
    </view>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { getType, addType, updateType } from '@/api/system/dict/type'
import { getDicts } from "@/api/system/dict/data"
import { onLoad } from "@dcloudio/uni-app"

const { proxy } = getCurrentInstance()
const title = ref('添加字典类型')

const form = reactive({
  dictId: undefined,
  dictName: undefined,
  dictType: undefined,
  remark: undefined
})

// 表单验证错误字段
const errorFields = ref([])

// 输入框基础样式
const inputBaseStyle = {
  background: '#ffffff',
  border: '2rpx solid #dcdfe6',
  borderRadius: '8rpx',
  padding: '0 24rpx',
  height: '60rpx',
  lineHeight: '60rpx',
  width: '100%',
  boxSizing: 'border-box'
}

// 输入框错误样式
const inputErrorStyle = {
  background: '#fef0f0',
  border: '2rpx solid #f56c6c',
  borderRadius: '8rpx',
  padding: '0 24rpx',
  height: '60rpx',
  lineHeight: '60rpx',
  width: '100%',
  boxSizing: 'border-box'
}

// 根据字段名获取输入框样式
const getInputStyle = (field) => {
  return errorFields.value.includes(field) ? inputErrorStyle : inputBaseStyle
}

const rules = {
  dictName: [
    { required: true, message: '字典名称不能为空', trigger: ['blur', 'change'] }
  ],
  dictType: [
    { required: true, message: '字典类型不能为空', trigger: ['blur', 'change'] }
  ]
}

onLoad((option) => {
  if (option.id) {
    title.value = '修改字典类型'
    getDetail(option.id)
  }
})

// 获取详情
function getDetail(id) {
  getType(id).then(res => {
    Object.assign(form, res.data)
  })
}

// 提交
function submit() {
  errorFields.value = [] // 清空错误字段
  console.log('submit 被调用')
  proxy.$refs['uForm'].validate().then(() => {
    console.log('验证通过', form.dictId)
    if (form.dictId) {
      updateType(form).then(() => {
        console.log('修改成功,将跳转')
        proxy.$refs['uToast'].show({
          message: '修改成功', complete() {
            console.log('complete 被调用,执行跳转')
            uni.navigateTo({ url: `/pages_mine/pages/system/dict/list` })
          }
        })
      })
    } else {
      addType(form).then(() => {
        console.log('新增成功,将跳转')
        proxy.$refs['uToast'].show({
          message: '新增成功', complete() {
            console.log('complete 被调用,执行跳转')
            uni.navigateTo({ url: `/pages_mine/pages/system/dict/list` })
          }
        })
      })
    }
  }).catch((errors) => {
    // 记录验证失败的字段
    if (errors && Array.isArray(errors)) {
      errorFields.value = errors.map(err => err.field || err.prop)
    }
    proxy.$modal.msgError('请填写完整信息')
  })
}
</script>

<style lang="scss" scoped>
.container {
  background-color: #f5f5f5;
}

.section {
  margin: 24rpx;
  padding: 0;
  background-color: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;

  .section-title {
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #ffffff;
    line-height: 1.2;
    font-size: 28rpx;
    font-weight: 600;
    display: flex;
    align-items: center;
    
    &::before {
      content: '';
      width: 6rpx;
      height: 28rpx;
      background: #ffffff;
      border-radius: 3rpx;
      margin-right: 12rpx;
    }
  }

  .form-view {
    padding: 24rpx;

    .form-btn {
      padding-top: 16rpx;
    }
  }
}

.select-input-wrapper {
  position: relative;
  
  .select-arrow {
    position: absolute;
    right: 24rpx;
    top: 50%;
    transform: translateY(-50%);
    color: #c0c4cc;
    font-size: 28rpx;
    pointer-events: none;
  }
}
</style>

<style lang="scss">
.form-btn .u-button {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  border-radius: 24rpx !important;
  height: 80rpx !important;
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.4) !important;
}

.form-btn .u-button__text {
  font-size: 30rpx !important;
  font-weight: 500 !important;
  letter-spacing: 2rpx !important;
}

/* 统一输入框高度和行高 */
.u--input,
.u-input__content__field-wrapper {
  height: 60rpx !important;
  line-height: 60rpx !important;
  min-height: 60rpx !important;
  box-sizing: border-box !important;
}

.u-form-item__body {
  flex: 1 !important;
}

.u-form-item__body .u--input {
  width: 100% !important;
  box-sizing: border-box !important;
}

.u-input__content__field-wrapper__field {
  line-height: 60rpx !important;
  height: 60rpx !important;
}
</style>
