<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>海报管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/poster/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-width='labelWidth' label="位置" prop="poster_location">
      <el-select v-model="params.poster_location" clearable placeholder="请选择">
        <el-option v-for="item in poster_location" :key="item.dictionary_guid" :label="item.dictionary_name"
          :value="item.dictionary_value"></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="addPosterDialogVisible = 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 => getPosterList(params)">
    <template #i18n_lang_type_code="scope">
      <el-tag type="primary">{{ scope.row.i18n_lang_type_code }}</el-tag>
    </template>

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

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

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

  <!-- 添加海报 -->
  <AddPosterDialog v-model="addPosterDialogVisible" :done="() => tableRef.reload()"></AddPosterDialog>
  <!-- 编辑海报 -->
  <EditPosterDialog v-model="EditPosterDialogVisible" :data="EditPosterDialogRow" :done="() => tableRef.reload()">
  </EditPosterDialog>
  <!-- 海报详情 -->
  <DetailPosterDialog v-model="DetailPosterDialogVisible" :data="DetailPosterDialogRow"></DetailPosterDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getPosterList, deletePoster, getDictionary,editPoster } from '~/service/poster';
import AddPosterDialog from './components/AddPosterDialog.vue';
import EditPosterDialog from './components/EditPosterDialog.vue';
import DetailPosterDialog from './components/DetailPosterDialog.vue';
import { getI18nLangTypeList } from '~/service/i18n_lang_type';

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

const addPosterDialogVisible = ref(false);
const EditPosterDialogVisible = ref(false);
const EditPosterDialogRow = ref({});
const DetailPosterDialogVisible = ref(false);
const DetailPosterDialogRow = ref({});

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

// 查询参数
const params = reactive({
  poster_location: "",
    i18n_lang_type_guid: ""
});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
    {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
    width: "150",
  },
  {
    prop: "poster_location",
    label: '位置',
    width: '150'
  },
  {
    prop: "poster_img",
    label: '图片',
    width: '150'
  },
  // {
  //   prop: "poster_title",
  //   label: '标题',
  //   width: '200'
  // },
  // {
  //   prop: "poster_describe",
  //   label: '描述',
  //   width: '250'
  // },
  {
    prop: "poster_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 deletePoster({
      poster_guid: data.map(v => v.poster_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditPosterDialogVisible.value = true
  EditPosterDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailPosterDialogVisible.value = true
  DetailPosterDialogRow.value = row
}

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


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


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