<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>交叉轮播图</el-breadcrumb-item>
    <el-breadcrumb-item to="/CrossBanner/list">交叉轮播图列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <!-- <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="addCrossBannerDialogVisible = 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 => getCrossBannerList(params)">

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

    <!-- <template #cross_banner_position="scope">
      <el-select style="width:150px" v-model="scope.row.cross_banner_position" placeholder="请选择位置"
        @change="handleEditPosition(scope.row)">
        <el-option label="上" :value="1"></el-option>
        <el-option label="下" :value="2"></el-option>
      </el-select>
    </template> -->


    <!-- 排序 -->
    <template #cross_banner_sort='scope'>
      <el-input-number :disabled='loading' v-model='scope.row.cross_banner_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>

  <!-- 添加合作伙伴 -->
  <AddCrossBannerDialog v-model="addCrossBannerDialogVisible" :done="() => tableRef.reload()"></AddCrossBannerDialog>
  <!-- 编辑合作伙伴 -->
  <EditCrossBannerDialog v-model="EditCrossBannerDialogVisible" :data="EditCrossBannerDialogRow"
    :done="() => tableRef.reload()">
  </EditCrossBannerDialog>
  <!-- 合作伙伴详情 -->
  <DetailCrossBannerDialog v-model="DetailCrossBannerDialogVisible" :data="DetailCrossBannerDialogRow">
  </DetailCrossBannerDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getCrossBannerList, editCrossBanner, deleteCrossBanner } from '~/service/cross_banner';
import AddCrossBannerDialog from './components/AddCrossBannerDialog.vue';
import EditCrossBannerDialog from './components/EditCrossBannerDialog.vue';
import DetailCrossBannerDialog from './components/DetailCrossBannerDialog.vue';

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

const addCrossBannerDialogVisible = ref(false);
const EditCrossBannerDialogVisible = ref(false);
const EditCrossBannerDialogRow = ref({});
const DetailCrossBannerDialogVisible = ref(false);
const DetailCrossBannerDialogRow = ref({});

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

// 查询参数
const params = reactive({

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "cross_banner_img",
    label: '图片',
    width: '150'
  },
  // {
  //   prop: "cross_banner_position",
  //   label: '位置',
  //   width: '180'
  // },
  {
    prop: "cross_banner_sort",
    label: '排序',
    width: '180'
  },
  {
    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 deleteCrossBanner({
      cross_banner_guid: data.map(v => v.cross_banner_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditCrossBannerDialogVisible.value = true
  EditCrossBannerDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailCrossBannerDialogVisible.value = true
  DetailCrossBannerDialogRow.value = row
}


//排序
const loading = ref(false)
async function handleEditOrder(data) {
  loading.value = true
  const { code } = await editCrossBanner(data);
  if (code == 0) {
    loading.value = false
    tableRef.value.reload()
  }
}
// 位置
async function handleEditPosition(data) {
  loading.value = true
  const { code } = await editCrossBanner(data);
  if (code == 0) {
    loading.value = false
    tableRef.value.reload()
  }
}
</script>
