<template>
  <el-dialog
    :visible.sync="visibleProxy"
    :show-close="false"
    width="520px"
    :close-on-click-modal="false"
    custom-class="invite-dialog"
    append-to-body
  >
    <div class="invite-dialog-inner">
      <div class="invite-header">
        <div class="invite-title-wrap">
          <div class="invite-title">新增沟通记录</div>
        </div>
      </div>

      <div v-if="customer && customer.name" class="invite-customer-info">
        <span>客户：{{ customer.name }}</span>
        <span v-if="customer.phone" style="margin-left:12px">电话：{{ customer.phone }}</span>
      </div>

      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="96px"
        size="small"
        class="invite-form"
      >
        <el-form-item label="沟通方式" prop="gtfs">
          <el-select
            v-model="form.gtfs"
            placeholder="请选择沟通方式"
            filterable
            clearable
          >
            <el-option label="电话" value="电话" />
            <el-option label="微信" value="微信" />
          </el-select>
        </el-form-item>

        <el-form-item label="是否接听" prop="sfjt">
          <el-select
            v-model="form.sfjt"
            placeholder="请选择是否接听"
            clearable
          >
            <el-option label="是" value="是" />
            <el-option label="否" value="否" />
            <el-option label="未拨通" value="未拨通" />
          </el-select>
        </el-form-item>

        <el-form-item label="联系记录" prop="lxjl">
          <el-input
            v-model="form.lxjl"
            type="textarea"
            :rows="3"
            placeholder="请输入联系记录"
          />
        </el-form-item>

        <el-form-item label="下次联系时间" prop="xcLxsj">
          <el-date-picker
            v-model="form.xcLxsj"
            type="datetime"
            placeholder="可选，方便下次跟进"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>

      <div class="invite-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>
import request from '@/utils/request'

export default {
  name: 'InviteAddDialog',
  props: {
    visible: { type: Boolean, default: false },
    customer: { type: Object, default: () => ({}) },
    /** 关联拓客记录编号（lq_yaoyjl.tkbh） */
    tkbh: { type: String, default: '' }
  },
  data() {
    return {
      submitting: false,
      form: {
        gtfs: '',
        sfjt: '',
        lxjl: '',
        xcLxsj: ''
      },
      rules: {
        gtfs: [{ required: true, message: '请选择沟通方式', trigger: 'change' }],
        sfjt: [{ required: true, message: '请选择是否接听', trigger: 'change' }],
        lxjl: [
          { required: true, message: '请输入联系记录', trigger: 'blur' },
          { min: 2, message: '联系记录至少 2 个字', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    visibleProxy: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.resetForm()
      }
    }
  },
  methods: {
    resetForm() {
      this.form = {
        gtfs: '',
        sfjt: '',
        lxjl: '',
        xcLxsj: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
    },
    handleCancel() {
      this.visibleProxy = false
      this.resetForm()
      this.$emit('cancel')
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.submitting = true
        const user = this.$store.getters.userInfo || {}
        const body = {
          yyr: user.userId || user.id || '',
          yysj: new Date().getTime(),
          yykh: this.customer.id || '',
          yykhxm: this.customer.name || '',
          dhsfyx: '',
          gtfs: this.form.gtfs,
          sfjt: this.form.sfjt,
          lxsj: new Date().getTime(),
          lxjl: this.form.lxjl,
          xcLxsj: this.form.xcLxsj || undefined
        }
        if (this.tkbh) {
          body.tkbh = this.tkbh
        }
        request({
          url: '/api/Extend/LqYaoyjl',
          method: 'post',
          data: body
        }).then(() => {
          this.submitting = false
          this.$message.success('已添加沟通记录')
          this.visibleProxy = false
          this.resetForm()
          this.$emit('success')
        }).catch(() => {
          this.submitting = false
        })
      })
    }
  }
}
</script>

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

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

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

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

.invite-customer-info {
  padding: 8px 14px;
  margin-bottom: 6px;
  font-size: 13px;
  color: #475569;
  background: rgba(241, 245, 249, 0.8);
  border-radius: 8px;
}

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

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

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

::v-deep .invite-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 .invite-dialog .el-input__inner {
  border-radius: 999px;
  height: 32px;
  line-height: 32px;
  border-color: #e5e7eb;
  background-color: #f9fafb;
}

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

::v-deep .invite-dialog .el-textarea__inner {
  border-radius: 12px;
  border-color: #e5e7eb;
  background-color: #f9fafb;
}

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

/* 统一按钮风格：胶囊 + 蓝色主按钮，与其他弹窗一致 */
::v-deep .invite-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 .invite-dialog .el-button--primary.is-disabled {
  box-shadow: none;
}

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

