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


    <el-form-item label="tdk所属模块">
      <el-select v-model="params.tdk_type" clearable placeholder="请选择">
        <el-option v-for="item in tdk_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.tdk_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;">
    <!-- 添加网站tdk -->
    <el-col :span="1">
      <el-button type="primary" @click="addTdkDialogVisible = 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="ElIconDownload" @click="downloadTemplate()">下载导入模板</el-button>

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


    <template #tdk_type='scope'>
      <dict-tag :options='tdk_type' :value='scope.row.tdk_type' />
    </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>

  <!-- 添加网站tdk -->
  <AddTdkDialog v-model="addTdkDialogVisible" :done="() => tableRef.reload()"></AddTdkDialog>
  <!-- 编辑网站tdk -->
  <EditTdkDialog v-model="EditTdkDialogVisible" :data="EditTdkDialogRow" :done="() => tableRef.reload()"></EditTdkDialog>
  <!-- 网站tdk详情 -->
  <DetailTdkDialog v-model="DetailTdkDialogVisible" :data="DetailTdkDialogRow"></DetailTdkDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getTdkList, deleteTdk, getDictionary, exportExcel, downloadTemplate, importExcel } from '~/service/tdk';
import AddTdkDialog from './components/AddTdkDialog.vue';
import EditTdkDialog from './components/EditTdkDialog.vue';
import DetailTdkDialog from './components/DetailTdkDialog.vue';

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

const addTdkDialogVisible = ref(false);
const EditTdkDialogVisible = ref(false);
const EditTdkDialogRow = ref({});
const DetailTdkDialogVisible = ref(false);
const DetailTdkDialogRow = ref({});

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

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

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "tdk_type",
    label: 'tdk所属模块',
    width: '150'
  },
  {
    prop: "tdk_title",
    label: '网页标题',
    width: '250'
  },
  {
    prop: "tdk_description",
    label: '网页简介',
    width: '400',
    showOverflowTooltip: true,
  },
  {
    prop: "tdk_keyword",
    label: '网页关键词',
    width: '300',
    showOverflowTooltip: 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(`您确定要删除该网站tdk吗？`).then(async () => {
    const res = await deleteTdk({
      tdk_guid: data.map(v => v.tdk_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditTdkDialogVisible.value = true
  EditTdkDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailTdkDialogVisible.value = true
  DetailTdkDialogRow.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 tdk_type = ref([]);
async function get_tdk_type() {
  await getDictionary({ dictionary_value: 'tdk_type' }).then((res) => {
    tdk_type.value = res
  })
}
get_tdk_type()
</script>
