<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>产品管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/product/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="product_type_parent_guid">
      <el-cascader class="w100" filterable :options="dataList"
        :props="{ checkStrictly: false, value: 'product_type_guid', label: 'product_type_name', emitPath: false }"
        placeholder="请选择商品类目" clearable v-model="params.product_type_guid">
        <template #default="{ node, data }">
          <span>{{ data.product_type_name }}</span>
          <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
        </template>
      </el-cascader>
    </el-form-item>
    <el-form-item label="产品名称">
      <el-input v-model='params.product_name' 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="addProductDialogVisible = 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 => getProductList(params)">

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


    <template #product_sort="scope">
      <el-input-number v-model='scope.row.product_sort' controls-position="right"
        @change="handleEditProduct(scope.row)"></el-input-number>
    </template>

    <template #i18n_lang_type_code="scope">
      <el-tag type="primary">{{ scope.row.i18n_lang_type_code }}</el-tag>
    </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: 'copy', row: scope.row }">
                复制
              </el-dropdown-item>
              <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>

  <!-- 添加产品 -->
  <AddProductDialog v-model="addProductDialogVisible" :done="() => tableRef.reload()"></AddProductDialog>
  <!-- 编辑产品 -->
  <EditProductDialog v-model="EditProductDialogVisible" :data="EditProductDialogRow" :done="() => tableRef.reload()">
  </EditProductDialog>
  <!-- 产品详情 -->
  <DetailProductDialog v-model="DetailProductDialogVisible" :data="DetailProductDialogRow"></DetailProductDialog>
  <!-- 编辑产品 -->
  <CopyProductDialog v-model="CopyProductDialogVisible" :data="CopyProductDialogRow" :done="() => tableRef.reload()">
  </CopyProductDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getProductList, editProduct, deleteProduct, exportExcel, downloadTemplate, importExcel } from '~/service/product';
import { getProductTypeTree } from "~/service/product_type";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddProductDialog from './components/AddProductDialog.vue';
import EditProductDialog from './components/EditProductDialog.vue';
import DetailProductDialog from './components/DetailProductDialog.vue';
import CopyProductDialog from './components/CopyProductDialog.vue';

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

const addProductDialogVisible = ref(false);
const EditProductDialogVisible = ref(false);
const EditProductDialogRow = ref({});
const DetailProductDialogVisible = ref(false);
const DetailProductDialogRow = ref({});
const CopyProductDialogVisible = ref(false);
const CopyProductDialogRow = ref({});

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

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

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "product_id",
    label: '产品ID',
    width: "100",
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言类型code',
    width: "150",
  },
  {
    prop: "product_type_name",
    label: '产品类目',
    width: "150",
  },
  {
    prop: "product_name",
    label: '产品名称',
    width: "400",
    showOverflowTooltip: true,
  },
  {
    prop: "product_img",
    label: '产品图片',
    width: "200"
  },
  {
    prop: "product_description",
    label: '产品描述',
    showOverflowTooltip: true,
  },
  {
    prop: "product_sort",
    label: '产品排序',
    width: "200",
    sortable: true
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250',
    fixed: 'right'
  }
];


const handleCommand = ({ type, row }) => {
  switch (type) {
    case "copy":
      handleCopy(row);
      break;
    case "detail":
      handleDetail(row);
      break;
    case 'delete':
      handleDelete([row]);
      break;
  }
};

// 删除数据
const handleDelete = data => {
  ElMessageBox.confirm(`您确定要删除该产品吗？`).then(async () => {
    const res = await deleteProduct({
      product_guid: data.map(v => v.product_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditProductDialogVisible.value = true
  EditProductDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailProductDialogVisible.value = true
  DetailProductDialogRow.value = row
}

// 复制
function handleCopy(row) {
  CopyProductDialogVisible.value = true
  CopyProductDialogRow.value = row
}

//排序
async function handleEditProduct(data) {
  await editProduct(data);
  tableRef.value.reload();
}

// 获取产品类目树形列表
const dataList = ref();
async function getList() {
  getProductTypeTree().then((res) => {
    if (res.code == 0) {
      dataList.value = res.data
    }
  })
}
getList()

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()

// 导入方法
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();
};






</script>
