<template>
  <div class="phone-page">
    <div class="page-header">
      <div class="header-title">
        <span class="header-icon">📞</span>
        <span class="header-text">联系电话管理</span>
      </div>
      <span class="header-sub">配置小程序端展示的各类联系电话</span>
    </div>

    <el-card shadow="never" class="phone-card">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="160px" class="phone-form">
        <el-form-item label="华晟型材专家电话" prop="expertPhone">
          <el-input v-model="form.expertPhone" placeholder="请输入专家电话" maxlength="20" class="phone-input">
            <template #prefix>
              <el-icon><Phone /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="客服电话" prop="servicePhone">
          <el-input v-model="form.servicePhone" placeholder="请输入客服电话" maxlength="20" class="phone-input">
            <template #prefix>
              <el-icon><Headset /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="业务员电话" prop="salesPhone">
          <el-input v-model="form.salesPhone" placeholder="请输入业务员电话" maxlength="20" class="phone-input">
            <template #prefix>
              <el-icon><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="saving" @click="handleSave">
            <el-icon><Check /></el-icon>
            保存
          </el-button>
          <el-button @click="loadPhone">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Phone, Headset, UserFilled, Check, Refresh } from '@element-plus/icons-vue';
import { getPhone, updatePhone, PhoneForm } from '@/api/system/phone';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const saving = ref(false);
const formRef = ref();

const form = reactive<PhoneForm>({
  id: 1,
  expertPhone: '',
  servicePhone: '',
  salesPhone: ''
});

const rules = {
  expertPhone: [{ required: true, message: '专家电话不能为空', trigger: 'blur' }],
  servicePhone: [{ required: true, message: '客服电话不能为空', trigger: 'blur' }],
  salesPhone: [{ required: true, message: '业务员电话不能为空', trigger: 'blur' }]
};

const loadPhone = async () => {
  const res = await getPhone(1);
  form.id = res.data.id;
  form.expertPhone = res.data.expertPhone;
  form.servicePhone = res.data.servicePhone;
  form.salesPhone = res.data.salesPhone;
};

const handleSave = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (!valid) return;
    saving.value = true;
    try {
      await updatePhone({ ...form });
      proxy?.$modal.msgSuccess('保存成功');
    } finally {
      saving.value = false;
    }
  });
};

onMounted(() => {
  loadPhone();
});
</script>

<style scoped>
.phone-page {
    width: 100%;
    height: 100%;
    padding: 20px 24px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: #f7f8fc;
}

/* ==================== 页头 ==================== */
.page-header {
    display: flex;
    align-items: baseline;
    gap: 14px;
    flex-shrink: 0;
}

.header-title {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-icon {
    font-size: 22px;
    line-height: 1;
}

.header-text {
    font-size: 18px;
    font-weight: 700;
    color: #1a202c;
    letter-spacing: 0.3px;
}

.header-sub {
    font-size: 13px;
    color: #a0aec0;
}

/* ==================== 卡片 ==================== */
.phone-card {
    border-radius: 12px !important;
    border: none !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.03) !important;
}

.phone-card :deep(.el-card__body) {
    padding: 28px 32px 24px;
}

/* ==================== 表单 ==================== */
.phone-form {
    max-width: 520px;
}

.phone-form :deep(.el-form-item__label) {
    font-weight: 500;
    color: #4a5568;
}

.phone-input {
    --el-input-border-radius: 8px;
}

.phone-input :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 0 0 1px #e2e8f0 inset;
    padding-left: 10px;
}

.phone-input :deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #cbd5e1 inset;
}

.phone-input :deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px #4f46e5 inset, 0 0 0 3px rgba(79, 70, 229, 0.08);
}

.phone-input :deep(.el-input__prefix) {
    color: #a0aec0;
    margin-right: 6px;
}

.phone-input :deep(.el-input__prefix .el-icon) {
    font-size: 16px;
}

.phone-form :deep(.el-button) {
    border-radius: 8px;
    padding: 8px 22px;
    font-weight: 500;
}

.phone-form :deep(.el-button .el-icon) {
    margin-right: 4px;
}
</style>
