<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>首页视频管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/home_video/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>
      <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="addHomeVideoDialogVisible = 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 => getHomeVideoList(params)">

    <template #home_video_cover="scope">
      <el-image v-if="scope.row.home_video_cover" :src="scope.row.home_video_cover.split(',')[0]" lazy
        :preview-src-list="scope.row.home_video_cover.split(',')" :preview-teleported="true" :hide-on-click-modal="true"
        fit="contain" class="el-avatar"></el-image>
      <template v-else>暂无图片</template>
    </template>

    <template #home_video_url="scope">
      <video :src="scope.row.home_video_url" controls class="video" style="width:350px;"></video>
    </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>

  <!-- 添加首页视频 -->
  <AddHomeVideoDialog v-model="addHomeVideoDialogVisible" :done="() => tableRef.reload()"></AddHomeVideoDialog>
  <!-- 编辑首页视频 -->
  <EditHomeVideoDialog v-model="EditHomeVideoDialogVisible" :data="EditHomeVideoDialogRow"
    :done="() => tableRef.reload()"></EditHomeVideoDialog>
  <!-- 首页视频详情 -->
  <DetailHomeVideoDialog v-model="DetailHomeVideoDialogVisible" :data="DetailHomeVideoDialogRow">
  </DetailHomeVideoDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getHomeVideoList, editHomeVideo, deleteHomeVideo } from '~/service/home_video';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddHomeVideoDialog from './components/AddHomeVideoDialog.vue';
import EditHomeVideoDialog from './components/EditHomeVideoDialog.vue';
import DetailHomeVideoDialog from './components/DetailHomeVideoDialog.vue';

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

const addHomeVideoDialogVisible = ref(false);
const EditHomeVideoDialogVisible = ref(false);
const EditHomeVideoDialogRow = ref({});
const DetailHomeVideoDialogVisible = ref(false);
const DetailHomeVideoDialogRow = ref({});

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

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

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
  },
  {
    prop: "home_video_cover",
    label: '视频封面',
  },
  {
    prop: "home_video_url",
    label: '视频',
  },
  {
    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 deleteHomeVideo({
      home_video_guid: data.map(v => v.home_video_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditHomeVideoDialogVisible.value = true
  EditHomeVideoDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailHomeVideoDialogVisible.value = true
  DetailHomeVideoDialogRow.value = row
}



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>
