<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-col :span="12">
            <el-form-item label="意向状态" prop="intentionState">
              <el-select v-model="form.intentionState" filterable>
                <el-option
                  v-for="item in intentionOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </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="skillSearch.question"
          placeholder="请输入关键字查询关键话术"
          clearable
          @keyup.enter="getSkillList"
        />
        <el-collapse v-model="activeQues" :accordion="false">
          <el-collapse-item
            v-for="item in skillList"
            :key="item.skillId"
            :title="item.question"
            :name="item.skillId"
          >
            <div v-dompurify-html="item.content"></div>
          </el-collapse-item>
        </el-collapse>
        <Pagination
          v-model:limit="skillSearch.pageSize"
          v-model:page="skillSearch.pageNo"
          :total="skillCount"
          small
          layout="total, prev, pager, next, jumper"
          @pagination="getSkillList"
        />
      </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 { getSkillPage } from '@/api/clue/skill'
import { createFollow } from '@/api/clue/followRecord'
import { getDictOptions } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'

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

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, status) => {
  dialogVisible.value = true
  dialogTitle.value = '新增跟进记录'
  resetForm(clueId, status)
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
function resetForm(clueId, status) {
  form.value = {
    clueId,
    operateTime: formatDate(new Date(), 'YYYY-MM-DD HH:mm'),
    nextFollowTime: undefined,
    intentionState: status,
    content: undefined
  }
}

const skillList = ref([])
const activeQues = ref('')

const skillSearch = ref({
  pageNo: 1,
  pageSize: 10,
  question: undefined
})

const skillCount = ref(0)

function getSkillList() {
  getSkillPage(skillSearch.value).then((data) => {
    skillList.value = data.list
    skillCount.value = data.total
  })
}

const emit = defineEmits(['success'])

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
  }
}

onMounted(() => {
  getSkillList()
})
</script>

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