<template>
  <div class="flex">
    <div class="mr-20px" style="width: 500px">
      <el-input
        v-model="searchForm.nickname"
        placeholder="请输入关键字查询"
        clearable
        class="mb-10px"
        @keyup.enter="getUserList"
      />

      <el-table
        v-loading="loading"
        :data="userList"
        :row-class-name="setRowClass"
        @row-click="handleRowClick"
      >
        <el-table-column prop="nickname" label="员工姓名" />
        <el-table-column prop="mobile" label="电话" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            {{ ['在职', '离职'][row.status] }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="searchForm.pageSize"
        v-model:page="searchForm.pageNo"
        small
        layout="total, prev, pager, next, jumper"
        :total="total"
        @pagination="getUserList"
      />
    </div>
    <el-form :model="form" ref="sendForm" :rules="rules" label-width="100px" :inline="false">
      <el-form-item label="是否自动分配">
        <el-radio-group v-model="form.isAuto">
          <el-radio :label="1"> 自动分配 </el-radio>
          <el-radio :label="0"> 手动分配 </el-radio>
        </el-radio-group>
      </el-form-item>
      <div v-if="form.isAuto">
        <el-form-item label="线索来源">
          <div>
            <el-checkbox
              v-model="checkResourceAll"
              :indeterminate="resourceIndeterminate"
              @change="resourceCheckAllChange"
            >
              全选
            </el-checkbox>
            <!-- 得根据自动获得的线索做配置 -->
            <el-checkbox-group v-model="form.resource" @change="resourceCheckedChange">
              <el-checkbox
                v-for="(item, index) in resourceOptions"
                :key="index"
                :label="item.value"
                :value="item.value"
              >
                {{ item.label }}
              </el-checkbox>
            </el-checkbox-group>
          </div>
        </el-form-item>
        <el-form-item label="权重配置">
          <el-input v-model="form.weight" type="number" placeholder="请输入权重">
            <template #suffix> % </template>
          </el-input>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="ClueSend">
import { getUserPage } from '@/api/system/user'
import { getClueDistributeRuleByUser } from '@/api/clue/clueGetSet'

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

const searchForm = ref({
  nickname: '',
  pageSize: 20,
  pageNo: 1
})

const total = ref(0)
const loading = ref(false)
const userList = ref([])

function setRowClass({ row }) {
  return row.id == currentRowId.value ? 'current-row' : ''
}

const currentRowId = ref('')

const form = ref({
  isAuto: 1,
  users: [1, 2],
  resource: [3],
  weight: undefined
})
const rules = ref({})

async function getUserList() {
  loading.value = true
  try {
    const data = await getUserPage(searchForm.value)
    userList.value = data.list
    if (userList.value.length) {
      handleRowClick(userList.value[0])
    }
    total.value = data.total
  } finally {
    loading.value = false
  }
}

function onSubmit() {
  console.log('hhahah')
}

const checkResourceAll = ref(false)
const resourceIndeterminate = ref(true)
const resourceOptions = ref([
  { label: '抖音', value: 1 },
  { label: '一点通', value: 2 },
  { label: '驾考宝典', value: 3 }
])

function resourceCheckAllChange(val) {
  form.value.resource = val ? resourceOptions.value.map((it) => it.value) : []
  resourceIndeterminate.value = false
}

function resourceCheckedChange(val) {
  const checkedCount = val.length
  checkResourceAll.value = checkedCount == resourceOptions.value.length
  resourceIndeterminate.value = checkedCount > 0 && checkedCount < resourceOptions.value.length
}

function handleRowClick(row) {
  currentRowId.value = row.id
  getClueDistributeRuleByUser({ employeeId: row.id }).then((data) => {
    form.value = data
  })
}

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

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