<template>
  <div class="app-container">
    <div ref="topContainerRef">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="方案名称" prop="schemeName">
          <el-input
            v-model="queryParams.schemeName"
            placeholder="请输入方案名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入备注"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:scheme:add']"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>

    <div :style="containerHeight">
      <el-table
        v-loading="loading"
        :data="schemeList"
        height="100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="方案ID" align="center" prop="schemeId" min-width="150" show-overflow-tooltip />
        <el-table-column label="所属项目ID" align="center" prop="projectId" min-width="150" show-overflow-tooltip />
        <el-table-column label="场景" align="center" prop="projectId" min-width="150">
          <template #default="{row}">
            <div>{{ formatSceneType(row.sceneType) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="方案名称" align="center" prop="schemeName" min-width="150" show-overflow-tooltip />
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="备注" align="center" prop="remark" min-width="150" show-overflow-tooltip />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="200"
          fixed="right"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:scheme:edit']"
              >修改</el-button
            >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:scheme:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改语音交互-方案对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="700px"
      :append-to-body="false"
      body-class="plan_dialog"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="方案名称" prop="schemeName">
          <el-input v-model="form.schemeName" placeholder="请输入方案名称" />
        </el-form-item>
        <el-form-item label="播放配置" prop="sceneType">
          <el-select
            v-model="form.sceneType"
            placeholder="Select"
            style="width: 240px"
          >
            <el-option
              v-for="item in settingOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :step="1" step-strictly />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>

      <FunctionSelection :funcIds="form.funcIds" :selectIds="selectIds" @updateSelectRows="getSelectRows"  />

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted } from "vue";
import { useContainerHeight } from "@/hooks/tableHeight";
import FunctionSelection from "./FunctionSelection.vue";

const topContainerRef = ref();
const containerHeight = useContainerHeight(topContainerRef);

const route = useRoute();

const schemeList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: route.params.id,
    schemeName: null,
  },
  rules: {
    projectId: [
      { required: true, message: "所属项目ID不能为空", trigger: "blur" },
    ],
    schemeName: [
      { required: true, message: "方案名称不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询语音交互-方案列表 */
function getList() {
  schemeList.value = [
    {
      schemeId: 1,
      schemeName: 'test1',
      projectId: 1,
      sceneType: 1,
      sort: 1,
      remark: 'test1',
      funcIds: '129'
    },
    {
      schemeId: 2,
      schemeName: 'test2',
      projectId: 1,
      sort: 2,
      sceneType: 2,
      remark: 'test2',
      funcIds: '130'
    },
    {
      schemeId: 3,
      schemeName: 'test3',
      projectId: 1,
      sceneType: 3,
      sort: 3,
      remark: 'test3',
      funcIds: '131'
    }
  ]
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    schemeId: null,
    schemeName: null,
    corpusIds: null,
    remark: null,
    sceneType: null,
    funcIds: ''
  };
}

const formatSceneType = (sceneType) => {
  return settingOptions.find(item => item.value === sceneType).label
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.schemeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

const settingOptions = [
  { value: 1, label: "唤醒场景" },
  { value: 2, label: "单次对话场景" },
  { value: 3, label: "连续对话场景" },
];

const isAdd = ref(true)
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  isAdd.value = true
  title.value = "添加方案";
}

const selectIds = ref([])
const getSelectRows = (arr) => {
  selectIds.value = arr.map(item => item.dictCode);
};

const formRef = ref()
const submitForm = () => {
  if (!formRef.value) return
  if (isAdd.value) {
    schemeList.value.unshift(
      {
        ...form.value,
        schemeId: new Date().getTime(),
        funcIds: selectIds.value.toString(),
        projectId: 1
      }
    )
  } else {
    let index = schemeList.value.findIndex(
      (item) => item.schemeId === form.value.schemeId
    );
    if (index !== -1) {
      schemeList.value[index] = {
        ...form.value,
        hhh: 'ffff',
        funcIds: selectIds.value.toString(),
      };
    }
  }

  reset();
  open.value = false
}

const handleUpdate = (row) => {
  reset();
  open.value = true;
  isAdd.value = false
  title.value = "修改方案";
  form.value = row
}

const handleDelete = (row) => {
   ElMessageBox.confirm(
    `确定要删除项目"${row.schemeName}"吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
     schemeList.value = schemeList.value.filter(item => item.schemeId !== row.schemeId)
  })
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
:deep() {
  .plan_dialog {
    .el-select {
      width: 100% !important;
    }
  }

  .el-scrollbar__view {
    width: 100%;
  }
}
</style>
