<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>亮点管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/highlights/list">亮点列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type">
      <el-select style="width:150px" v-model="params.i18n_lang_type_guid" clearable placeholder="请选择">
        <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid" :label="item.i18n_lang_type_code"
          :value="item.i18n_lang_type_guid"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="标题">
      <el-input v-model='params.highlights_title' placeholder='请输入标题'></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="tableRef.reload()" icon="ElIconSearch">
        搜索
      </el-button>
    </el-form-item>
  </el-form>
  <el-space style="margin-bottom: 10px;">
    <!-- 添加亮点 -->
    <el-col :span="1">
      <el-button type="primary" @click="addHighlightsDialogVisible = true"> 添加 </el-button>
    </el-col>



    <!-- 下拉操作 -->
    <el-dropdown v-if="selectionData.length">
      <el-button type="primary">
        批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="handleDelete(selectionData)">
            批量删除
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </el-space>
  <!-- 数据表格 -->
  <DataTable ref="tableRef" style="width: 100%" :onSelectionChange="data => (selectionData = data)" :column="column"
    :params="params" :request="params => getHighlightsList(params)">



    <!-- 排序 -->
    <template #highlights_sort='scope'>
      <el-input-number :disabled='loading' v-model='scope.row.highlights_sort' :min='1' controls-position='right'
        @change='handleEditOrder(scope.row)'></el-input-number>
    </template>

    <template #i18n_lang_type_code="scope">
      <el-tag type="primary">{{ scope.row.i18n_lang_type_code }}</el-tag>
    </template>

    <template #chaoz="scope">
      <el-space>
        <el-button size="small" @click="handleUpdate(scope.row)">
          编辑
        </el-button>
        <el-dropdown @command="handleCommand">
          <el-button type="primary" size="small">
            更多<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item :command="{ type: 'detail', row: scope.row }">
                详情
              </el-dropdown-item>
              <el-dropdown-item :command="{ type: 'delete', row: scope.row }">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
    </template>
  </DataTable>

  <!-- 添加亮点 -->
  <AddHighlightsDialog v-model="addHighlightsDialogVisible" :done="() => tableRef.reload()"></AddHighlightsDialog>
  <!-- 编辑亮点 -->
  <EditHighlightsDialog v-model="EditHighlightsDialogVisible" :data="EditHighlightsDialogRow"
    :done="() => tableRef.reload()"></EditHighlightsDialog>
  <!-- 亮点详情 -->
  <DetailHighlightsDialog v-model="DetailHighlightsDialogVisible" :data="DetailHighlightsDialogRow">
  </DetailHighlightsDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getHighlightsList, editHighlights, deleteHighlights } from '~/service/highlights';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddHighlightsDialog from './components/AddHighlightsDialog.vue';
import EditHighlightsDialog from './components/EditHighlightsDialog.vue';
import DetailHighlightsDialog from './components/DetailHighlightsDialog.vue';

const tableRef = ref();
const selectionData = ref([]);
const store = useLoginStore();

const addHighlightsDialogVisible = ref(false);
const EditHighlightsDialogVisible = ref(false);
const EditHighlightsDialogRow = ref({});
const DetailHighlightsDialogVisible = ref(false);
const DetailHighlightsDialogRow = ref({});

const headers = {
  Accept: "application/json",
  ...store.headers,
};

// 查询参数
const params = reactive({
  i18n_lang_type_guid: "",
  highlights_title: "",

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
    width: '150'
  },
  {
    prop: "highlights_title",
    label: '标题',
    width: '450'
  },
  {
    prop: "highlights_sort",
    label: '排序',
    width: '250'
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250',
    fixed: 'right'
  }
];


const handleCommand = ({ type, row }) => {
  switch (type) {
    case "detail":
      handleDetail(row);
      break;
    case 'delete':
      handleDelete([row]);
      break;
  }
};

// 删除数据
const handleDelete = data => {
  ElMessageBox.confirm(`您确定要删除该亮点吗？`).then(async () => {
    const res = await deleteHighlights({
      highlights_guid: data.map(v => v.highlights_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditHighlightsDialogVisible.value = true
  EditHighlightsDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailHighlightsDialogVisible.value = true
  DetailHighlightsDialogRow.value = row
}



//排序
const loading = ref(false)
async function handleEditOrder(data) {
  loading.value = true
  const { code } = await editHighlights(data);
  if (code == 0) {
    loading.value = false
    tableRef.value.reload()
  }
}


const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}
geti18n_lang_typeList()

</script>
