<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/news/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-select v-model="params.news_type" clearable placeholder="请选择">
        <el-option v-for="item in news_type" :key="item.dictionary_guid" :label="item.dictionary_name"
          :value="item.dictionary_value"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="新闻标题">
      <el-input v-model='params.news_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="addNewsDialogVisible = true"> 添加 </el-button>
    </el-col>

    <!-- 导入 -->
    <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="ElIconDocument" @click="exportExcel(params)">导出</el-button>

    <!-- 下载导入模板 -->
    <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 => getNewsList(params)">

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

    <template #news_type='scope'>
      <dict-tag :options='news_type' :value='scope.row.news_type' />
    </template>

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

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

  <!-- 添加新闻 -->
  <AddNewsDialog v-model="addNewsDialogVisible" :done="() => tableRef.reload()"></AddNewsDialog>
  <!-- 编辑新闻 -->
  <EditNewsDialog v-model="EditNewsDialogVisible" :data="EditNewsDialogRow" :done="() => tableRef.reload()">
  </EditNewsDialog>
  <!-- 新闻详情 -->
  <DetailNewsDialog v-model="DetailNewsDialogVisible" :data="DetailNewsDialogRow"></DetailNewsDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getNewsList, editNews, deleteNews, getDictionary, exportExcel, downloadTemplate, importExcel } from '~/service/news';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddNewsDialog from './components/AddNewsDialog.vue';
import EditNewsDialog from './components/EditNewsDialog.vue';
import DetailNewsDialog from './components/DetailNewsDialog.vue';

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

const addNewsDialogVisible = ref(false);
const EditNewsDialogVisible = ref(false);
const EditNewsDialogRow = ref({});
const DetailNewsDialogVisible = ref(false);
const DetailNewsDialogRow = ref({});

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

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

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
    width: "150",
  },
  {
    prop: "news_type",
    label: '新闻类型',
  },
  {
    prop: "news_cover",
    label: '新闻封面',
    width: "200",
  },
  {
    prop: "news_title",
    label: '新闻标题',
    showOverflowTooltip: true,
    width: "500",
  },
  // {
  //   prop: "news_intro",
  //   label: '新闻简介',
  //   showOverflowTooltip: true,
  //   width: "400",
  // },
  {
    prop: "news_issue_date",
    label: '发布日期',
    sortable: true
  },
  {
    prop: "news_views_num",
    label: '浏览次数',
    sortable: true,
    width: "150",
  },
  {
    prop: "news_sort",
    label: '新闻排序',
    width: "180",
    sortable: true
  },
  {
    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 deleteNews({
      news_guid: data.map(v => v.news_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditNewsDialogVisible.value = true
  EditNewsDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailNewsDialogVisible.value = true
  DetailNewsDialogRow.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 editNews(data);
  if (code == 0) {
    loading.value = false
    tableRef.value.reload()
  }
}




// 字典获取
const news_type = ref([]);
async function get_news_type() {
  await getDictionary({ dictionary_value: 'news_type' }).then((res) => {
    news_type.value = res
  })
}
get_news_type()


// 语言类型code
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>
