<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1000px">
    <div class="flex">
      <el-form style="flex: 1" :model="form" ref="formRef" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="本次跟进时间" prop="operateTime">
              <el-date-picker
                v-model="form.operateTime"
                type="datetime"
                format="YYYY-MM-DD HH:mm"
                valueFormat="YYYY-MM-DD HH:mm"
                placeholder="本次跟进时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="下次跟进时间" prop="nextFollowTime">
              <el-date-picker
                v-model="form.nextFollowTime"
                type="date"
                format="YYYY-MM-DD"
                valueFormat="YYYY-MM-DD"
                placeholder="下次跟进时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="跟进内容" prop="content">
              <el-input
                v-model="form.content"
                type="textarea"
                :autosize="{ minRows: 4 }"
                placeholder="请输入跟进内容"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="ml-20px" style="width: 300px">
        <el-input
          v-model="keyword"
          placeholder="请输入关键字查询关键话术"
          clearable
          @keyup.enter="filterList"
        />
        <el-collapse v-model="activeQues" :accordion="false">
          <el-collapse-item
            v-for="item in showList"
            :key="item.skillId"
            :title="item.question"
            :name="item.skillId"
          >
            <div v-dompurify-html="item.content"></div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :disabled="formLoading" type="primary" @click="handleSave">保 存</el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup name="DialogFollow">
import { createFollow } from '@/api/clue/followRecord'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formRef = ref() // 表单 Ref

const form = ref({})

const rules = {
  operateTime: { required: true, message: '本次跟进时间不可为空', trigger: 'change' },
  nextFollowTime: { required: true, message: '下次跟进时间不可为空', trigger: 'change' },
  content: { required: true, message: '跟进内容不可为空', trigger: 'blur' }
}

const open = async (clueId) => {
  dialogVisible.value = true
  dialogTitle.value = '新增跟进记录'
  resetForm(clueId)
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
function resetForm(clueId) {
  form.value = {
    clueId,
    operateTime: undefined,
    nextFollowTime: undefined,
    content: undefined
  }
}

const resultList = [
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 1,
    deptId: 167,
    question: '暂时先看题的',
    skillKey: '先看科目一',
    keyList: ['先看科目一'],
    content:
      '<p>科目一的题目不难的，等会可以加个微信给您发一些做题技巧，我们驾校离你也很近，后面您计划找驾校报名了可以接您来我们驾校实地看看的</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 2,
    deptId: 167,
    question: '你们怎么练车的',
    skillKey: '练车人数',
    keyList: ['练车人数'],
    content:
      '<p>我们驾校这边练车是自己提前在微信上跟教练约时间来练车的，科二练车的时候到你练车了是你一个人一辆车，教练一对一教学的，也可以一对二，不用排队等的</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 3,
    deptId: 167,
    question: '教练怎么样 凶嘛',
    skillKey: '教练凶',
    keyList: ['教练凶'],
    content:
      '<p>我们驾校教练都是驾校本部教练，也都是经过正规培训上岗的，脾气比较温和幽默的，不存在凶学员的情况，学车氛围也比较好；学员也不需要给教练送礼的</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 4,
    deptId: 167,
    question: '驾校通过率咋样',
    skillKey: '通过率',
    keyList: ['通过率'],
    content:
      '<p>我们驾校在合肥也是老牌驾校的了，在合肥干了十几年了，并且拥有自家考场，科二科三考试都在我们自家考场考试，所以通过率一直非常高</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 5,
    deptId: 167,
    question: '可以晚上练车吗？',
    skillKey: '晚上练车',
    keyList: ['晚上练车'],
    content:
      '<p>我们驾校练车时间是根据学员的时间来安排的，但是晚上练车视线不太好，因为考试的时候也是白天嘛，建议最好是白天过来练车，然后等熟练了后期可以让教练加加班晚上</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 6,
    deptId: 167,
    question: '大学生有优惠嘛',
    skillKey: '学生优惠',
    keyList: ['学生优惠'],
    content: '<p>学生我们驾校是有优惠的，而且我们驾校有专设学生班，练车不用排队，随到随练的</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 7,
    deptId: 167,
    question: '拿证时间多久',
    skillKey: '拿证时间',
    keyList: ['拿证时间'],
    content: '<p>C1手动挡正常拿证时间45天左右拿证，C2自动挡正常35天左右拿证</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 8,
    deptId: 167,
    question: '驾校有熟人',
    skillKey: '熟人',
    keyList: ['熟人'],
    content:
      '<p>你朋友认识的人是教练还是驾校里面工作人员呢，驾校场地在哪呢 学车肯定是要对比的 要个近的合适的，我们驾校离你很近，而且学费目前也在做活动比较优惠，您可以对比一下的，找驾校也是可以自己来现场看过后再决定的</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 9,
    deptId: 167,
    question: '考试地点在哪',
    skillKey: '考试地点',
    keyList: ['考试地点'],
    content:
      '<p>科一和科四是统一要去滨湖车管所考试的，科二科三合肥市总共六个考场，科二和科三考试是就近安排考场考试的，这样考试也方便些</p>',
    status: 2
  },
  {
    searchValue: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    params: {},
    orderName: null,
    orderType: null,
    skillId: 10,
    deptId: 167,
    question: '没时间练车',
    skillKey: '没时间学车',
    keyList: ['没时间学车'],
    content:
      '<p>我们驾校练车时间根据学员时间来安排，并且也不用每天都来的，一周抽时间来练个三四次就可以的，每次来一个小时这样，科二来个十来次 科三来个十来次就行，耽误不了你太多时间的</p>',
    status: 2
  }
]

const showList = ref(resultList)
const keyword = ref('')
const activeQues = ref('')

function filterList() {
  showList.value = resultList.filter((it) => it.question.includes(keyword.value))
}

async function handleSave() {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    await createFollow(form.value)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style lang="scss" scoped></style>
