<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>爱情故事管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/love_story/list">爱情故事列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item label="标题">
      <el-input v-model='params.love_story_title' placeholder='请输入标题'></el-input>
    </el-form-item>
    <el-form-item label="地点">
      <el-input v-model='params.love_story_place' 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="addLoveStoryDialogVisible = true"> 添加 </el-button>
    </el-col>


    <!-- 导出 -->
    <el-button icon="ElIconDocument" @click="exportExcel(params)">导出</el-button>

    <!-- 导入 -->
    <el-upload class="upload-demo" :action="importExcel" :headers="headers" :on-success="handleExcelSuccess"
      :on-progress="uploadLoading" :on-error="closeUploadLoading" style="margin-left: 10px" :show-file-list="false">
      <el-button type="primary">导入</el-button>
    </el-upload>

    <!-- 下载导入模板 -->
    <el-button icon="ElIconDownload" @click="downloadTemplate()">下载导入模板</el-button>


    <!-- 下拉操作 -->
    <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 => getLoveStoryList(params)">

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


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


    <!-- 音乐 -->
    <template #love_story_music_name='scope'>
      <div style="text-align: center;margin-top: 5px;">{{ scope.row.love_story_music_name }}</div>
      <audio v-if="scope.row.love_story_music_name" controls="controls" height="100" width="100">
        <source :src=scope.row.love_story_music type="audio/mp3" />
        <embed height="100" width="100" :src=scope.row.love_story_music />
      </audio>
      <div v-else>暂无音乐</div>
    </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>

  <!-- 添加爱情故事 -->
  <AddLoveStoryDialog v-model="addLoveStoryDialogVisible" :done="() => tableRef.reload()"></AddLoveStoryDialog>
  <!-- 编辑爱情故事 -->
  <EditLoveStoryDialog v-model="EditLoveStoryDialogVisible" :data="EditLoveStoryDialogRow"
    :done="() => tableRef.reload()"></EditLoveStoryDialog>
  <!-- 爱情故事详情 -->
  <DetailLoveStoryDialog v-model="DetailLoveStoryDialogVisible" :data="DetailLoveStoryDialogRow"></DetailLoveStoryDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getLoveStoryList, editLoveStory, deleteLoveStory, exportExcel, downloadTemplate, importExcel } from '~/service/love_story';
import AddLoveStoryDialog from './components/AddLoveStoryDialog.vue';
import EditLoveStoryDialog from './components/EditLoveStoryDialog.vue';
import DetailLoveStoryDialog from './components/DetailLoveStoryDialog.vue';

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

const addLoveStoryDialogVisible = ref(false);
const EditLoveStoryDialogVisible = ref(false);
const EditLoveStoryDialogRow = ref({});
const DetailLoveStoryDialogVisible = ref(false);
const DetailLoveStoryDialogRow = ref({});

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

// 查询参数
const params = reactive({
  love_story_title: "",
  love_story_place: "",
  love_story_date: "",

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "love_story_title",
    label: '标题',
    width: '150'
  },
  {
    prop: "love_story_author",
    label: '作者',
    width: '150'
  },
  {
    prop: "love_story_place",
    label: '地点',
    width: '150'
  },
  {
    prop: "love_story_date",
    label: '日期',
    width: '150'
  },
  {
    prop: "love_story_cover",
    label: '封面',
    width: '150'
  },
  {
    prop: "love_story_music_name",
    label: '音乐',
    width: '330'
  },
  {
    prop: "love_story_sort",
    label: '排序',
    width: '200'
  },
  {
    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 deleteLoveStory({
      love_story_guid: data.map(v => v.love_story_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditLoveStoryDialogVisible.value = true
  EditLoveStoryDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailLoveStoryDialogVisible.value = true
  DetailLoveStoryDialogRow.value = row
}


// 导入方法
let loadingImoprt = null;
const uploadLoading = () => {
  loadingImoprt = ElLoading.service({
    lock: true,
    text: "正在导入中...",
    background: "rgba(255, 255, 255, 0.7)",
  });
};
const closeUploadLoading = () => loadingImoprt.close();
const handleExcelSuccess = (value) => {
  if (value.code == 0) {
    ElMessageBox.alert(value.msg, "导入信息", {
      dangerouslyUseHTMLString: true,
      confirmButtonText: "确定",
    });
  } else {
    ElMessage.error(value.msg);
  }
  closeUploadLoading();
  tableRef.value.reload();
};



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




</script>
