<template>
  <el-dialog
    :visible.sync="visibleProxy"
    :show-close="false"
    width="520px"
    :close-on-click-modal="false"
    custom-class="booking-dialog"
    append-to-body
  >
    <div class="booking-dialog-inner">
      <div class="booking-header">
        <div class="booking-title-wrap">
          <div class="booking-title">新建预约</div>
        </div>
      </div>

      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="96px"
        size="small"
        class="booking-form"
      >
        <el-form-item label="预约体验项目" prop="yytyxm">
          <el-select
            v-model="form.yytyxm"
            placeholder="请选择体验项目"
            filterable
            clearable
          >
            <el-option
              v-for="item in projectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="顾客姓名" prop="gkxm">
          <el-select
            v-model="form.gkxm"
            placeholder="请选择顾客姓名"
            filterable
            clearable
          >
            <el-option
              v-for="item in customerOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="预约健康师" prop="yyjks">
          <el-select
            v-model="form.yyjks"
            placeholder="请选择健康师"
            filterable
            clearable
          >
            <el-option
              v-for="item in healthWorkerOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="预约日期" prop="yyrq">
          <el-date-picker
            v-model="form.yyrq"
            type="date"
            placeholder="请选择预约日期"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="预约时间" prop="yysj">
          <el-time-select
            v-model="form.yysj"
            :picker-options="timeOptions"
            placeholder="请选择预约时间"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>

      <div class="booking-footer">
        <el-button size="small" @click="handleCancel">取 消</el-button>
        <el-button
          type="primary"
          size="small"
          :loading="submitting"
          @click="handleSubmit"
        >
          {{ submitting ? '提交中...' : '保 存' }}
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'BookingDialog',
  props: {
    visible: { type: Boolean, default: false },
    prefill: {
      type: Object,
      default: () => ({ name: '', phone: '' })
    }
  },
  data() {
    return {
      submitting: false,
      form: {
        yytyxm: '',
        gkxm: '',
        yyjks: '',
        yyrq: '',
        yysj: ''
      },
      projectOptions: [
        { value: 'proj001', label: '基础护理' },
        { value: 'proj002', label: '深度清洁' },
        { value: 'proj003', label: '美白护理' },
        { value: 'proj004', label: '抗衰护理' },
        { value: 'proj005', label: '补水保湿' }
      ],
      customerOptions: [
        { value: 'cust001', label: '张三' },
        { value: 'cust002', label: '李四' },
        { value: 'cust003', label: '王五' },
        { value: 'cust004', label: '赵六' },
        { value: 'cust005', label: '钱七' }
      ],
      healthWorkerOptions: [
        { value: 'jks001', label: '张健康师' },
        { value: 'jks002', label: '李健康师' },
        { value: 'jks003', label: '王健康师' },
        { value: 'jks004', label: '赵健康师' },
        { value: 'jks005', label: '钱健康师' }
      ],
      timeOptions: {
        start: '09:00',
        step: '00:30',
        end: '21:00'
      },
      rules: {
        yytyxm: [{ required: true, message: '请选择体验项目', trigger: 'change' }],
        gkxm: [{ required: true, message: '请选择顾客姓名', trigger: 'change' }],
        yyjks: [{ required: true, message: '请选择健康师', trigger: 'change' }],
        yyrq: [{ required: true, message: '请选择预约日期', trigger: 'change' }],
        yysj: [{ required: true, message: '请选择预约时间', trigger: 'change' }]
      }
    }
  },
  computed: {
    visibleProxy: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.applyPrefill()
      }
    },
    prefill: {
      deep: true,
      handler() {
        if (this.visible) {
          this.applyPrefill()
        }
      }
    }
  },
  methods: {
    applyPrefill() {
      if (this.prefill && this.prefill.name) {
        this.form.gkxm = this.prefill.name
      }
      if (!this.form.yyrq) {
        this.form.yyrq = new Date()
      }
    },
    resetForm() {
      this.form = {
        yytyxm: '',
        gkxm: '',
        yyjks: '',
        yyrq: '',
        yysj: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
    },
    handleCancel() {
      this.visibleProxy = false
      this.resetForm()
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.submitting = true
        setTimeout(() => {
          this.submitting = false
          this.$message.success('预约已保存（示例）')
          this.visibleProxy = false
          this.resetForm()
        }, 800)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.booking-dialog-inner {
  padding: 18px 22px 14px;
}

.booking-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: 14px;
  background: rgba(219, 234, 254, 0.96);
}

.booking-title-wrap {
  flex: 1;
}

.booking-title {
  font-size: 17px;
  font-weight: 600;
  color: #0f172a;
}

.booking-form {
  padding-top: 6px;
}

::v-deep .booking-dialog .el-form-item__label {
  white-space: nowrap;
}

.booking-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

::v-deep .booking-dialog {
  max-width: 520px;
  margin-top: 10vh !important;
  border-radius: 20px;
  padding: 0;
  background: radial-gradient(
    circle at 0 0,
    rgba(255, 255, 255, 0.96) 0,
    rgba(248, 250, 252, 0.98) 40%,
    rgba(241, 245, 249, 0.98) 100%
  );
  box-shadow:
    0 24px 48px rgba(15, 23, 42, 0.18),
    0 0 0 1px rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(22px);
  -webkit-backdrop-filter: blur(22px);
}

::v-deep .el-dialog__header {
  display: none;
}

::v-deep .el-dialog__body {
  padding: 0;
}

/* 输入框圆角与现代化样式 */
::v-deep .booking-dialog .el-input__inner {
  border-radius: 999px;
  height: 32px;
  line-height: 32px;
  border-color: #e5e7eb;
  background-color: #f9fafb;
}

::v-deep .booking-dialog .el-input__inner:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.18);
}

::v-deep .booking-dialog .el-picker-panel {
  border-radius: 12px;
}

/* 统一按钮风格：胶囊 + 蓝色主按钮，与其他弹窗一致 */
::v-deep .booking-dialog .el-button--primary {
  border-radius: 999px;
  padding: 0 20px;
  height: 30px;
  line-height: 30px;
  background: #2563eb;
  border-color: #2563eb;
  box-shadow: 0 4px 10px rgba(37, 99, 235, 0.35);
  font-size: 12px;
}

::v-deep .booking-dialog .el-button--primary.is-disabled {
  box-shadow: none;
}

::v-deep .booking-dialog .el-button--default {
  border-radius: 999px;
  padding: 0 18px;
  height: 30px;
  line-height: 30px;
  background: rgba(239, 246, 255, 0.9);
  color: #2563eb;
  border-color: rgba(37, 99, 235, 0.18);
  font-size: 12px;
}

::v-deep .booking-dialog .el-button--default:hover {
  background: rgba(219, 234, 254, 0.95);
  color: #1d4ed8;
}
</style>

