<template>
  <view class="container" style="paddingBottom:1rpx;">
    <u-navbar
      leftIconSize="40rpx"
      leftIconColor="#333333"
      :title="title"
    >
    </u-navbar>
    <view class="section">
      <view class="section-title">{{ title }}</view>
      <view class="form-view">
        <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" label-width="auto"
          :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
          <u-form-item label="岗位名称" prop="postName" required>
            <u--input v-model="form.postName" placeholder="请输入岗位名称" maxlength="30"
              inputAlign="right" border="none"></u--input>
          </u-form-item>

          <u-form-item label="岗位编码" prop="postCode" required>
            <u--input v-model="form.postCode" placeholder="请输入岗位编码" maxlength="30"
              inputAlign="right" border="none"></u--input>
          </u-form-item>

          <u-form-item label="岗位排序">
            <u-number-box v-model="form.postSort" :min="0" :max="999" inputAlign="right"></u-number-box>
          </u-form-item>

          <u-form-item label="状态" prop="statusName" @click="showStatusPicker = true">
            <u--input v-model="statusName" disabled disabledColor="#ffffff" placeholder="请选择状态"
              inputAlign="right" border="none"></u--input>
            <template #right>
              <u-icon name="arrow-down"></u-icon>
            </template>
          </u-form-item>

          <u-form-item label="备注" prop="remark" labelPosition="top">
            <u--textarea v-model="form.remark" placeholder="请输入备注" border="none" autoHeight inputAlign="right" count
              maxlength="500" style="padding:18rpx 0;"></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>
    <u-picker itemHeight="88" :show="showStatusPicker" :columns="statusList" keyName="dictLabel" @cancel="showStatusPicker = false"
      @confirm="handleStatusConfirm"></u-picker>
  </view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { getPost, addPost, updatePost } from '@/api/system/post'
import { getDicts } from "@/api/system/dict/data"
import { onLoad } from "@dcloudio/uni-app"

const { proxy } = getCurrentInstance()
const statusList = ref([])
const title = ref('添加岗位')
const showStatusPicker = ref(false)
const statusName = ref('')

const form = reactive({
  postId: undefined,
  postName: undefined,
  postCode: undefined,
  postSort: 0,
  status: '0',
  remark: undefined
})

const rules = {
  postName: [
    { required: true, message: '岗位名称不能为空', trigger: ['blur', 'change'] }
  ],
  postCode: [
    { required: true, message: '岗位编码不能为空', trigger: ['blur', 'change'] }
  ]
}

onLoad((option) => {
  getDicts('sys_normal_disable').then(res => {
    statusList.value = [res.data]
  })

  if (option.id) {
    title.value = '修改岗位'
    getDetail(option.id)
  } else {
    setTimeout(() => {
      updateStatusName()
    }, 100)
  }
})

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

// 更新状态显示名称
function updateStatusName() {
  if (statusList.value[0]) {
    const item = statusList.value[0].find(v => v.dictValue === form.status)
    statusName.value = item ? item.dictLabel : ''
  }
}

// 状态选择确认
function handleStatusConfirm(e) {
  form.status = e.value[0].dictValue
  statusName.value = e.value[0].dictLabel
  showStatusPicker.value = false
}

// 提交
function submit() {
  proxy.$refs['uForm'].validate().then(() => {
    console.log('提交的表单数据:', form)
    if (form.postId) {
      updatePost(form).then(() => {
        proxy.$refs['uToast'].show({
          message: '修改成功', complete() {
            uni.navigateTo({ url: `/pages_mine/pages/system/post/list` })
          }
        })
      })
    } else {
      addPost(form).then(() => {
        proxy.$refs['uToast'].show({
          message: '新增成功', complete() {
            uni.navigateTo({ url: `/pages_mine/pages/system/post/list` })
          }
        })
      })
    }
  }).catch(() => {
    proxy.$refs['uToast'].show({
      type: 'error',
      message: '请填写完整信息'
    })
  })
}
</script>

<style lang="scss" scoped>
.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;
    }
  }
}
</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;
}
</style>
