<template>
  <div>
    <el-table :data="list" border>
      <el-table-column type="index" width="50" />
      <el-table-column label="来源名称" width="200px">
        <template #default="{ row }">
          <el-input v-model="row.name" placeholder="请输入" :clearable="false" />
        </template>
      </el-table-column>
      <el-table-column label="渠道" width="300px">
        <template #default="{ row }">
          <el-select
            v-if="row.edit"
            v-model="row.resource"
            placeholder="选择渠道"
            clearable
            filterable
            @change="handleChange(row)"
          >
            <el-option
              v-for="item in resourceOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <span>{{ row.resourceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获取连接" prop="link" />
      <el-table-column label="参数详情" prop="params" />
      <el-table-column label="是否启用" width="100px">
        <template #default="{ row }">
          <el-switch v-model="row.inEnable" :active-value="true" :inactive-value="false" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100px">
        <template #default="{ $index }">
          <el-button type="primary" style="padding: 0px" text @click="handleRemove($index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-20px flex justify-center">
      <el-button type="primary" @click="handleInsert">新增规则</el-button>
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script setup>
const list = ref([
  {
    name: '一点通账号1',
    resourceName: '驾校一点通',
    link: 'https://sscrm.ahduima.com/clue/get?cid=1001&aid=1001&res=2',
    params: '参数详情'
  },
  {
    name: '一点通账号2',
    resourceName: '驾校一点通',
    link: 'https://sscrm.ahduima.com/clue/get?cid=1001&aid=1001&res=2',
    params: '参数详情'
  },
  {
    name: '宝典账号',
    resourceName: '驾考宝典',
    link: 'https://sscrm.ahduima.com/clue/get?cid=1001&aid=1001&res=1',
    params: '参数详情'
  },
  {
    name: '抖音',
    resourceName: '抖音/开心学车',
    link: 'https://sscrm.ahduima.com/clue/get?cid=1001&aid=1001&res=3',
    params: '参数详情'
  }
])

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

function handleInsert() {
  list.value.push({ name: '', link: '', edit: true, inEnable: true })
}

function onSubmit() {
  console.log('保存成功')
}

function handleRemove(idx) {
  list.value.splice(idx, 1)
}

function handleChange(row) {
  row.link = `https://sscrm.ahduima.com/clue/get?cid=1001&aid=1001&res=${row.resource}`
  row.params = '参数详情'
}
</script>

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