<template>
  <!-- 面包屑 -->
  <el-breadcrumb>
    <el-breadcrumb-item>产品类型多语言管理</el-breadcrumb-item>
    <el-breadcrumb-item to="/product_type_lang/list">产品类型多语言列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 搜索 -->
  <el-form inline :model="params">


    <el-form-item :label-width="labelWidth" label="产品类型" prop="product_type_guid">
      <el-cascader class="w100" filterable :options="dataList"
        :props="{ checkStrictly: true, 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-width="labelWidth" label="语言类型" prop="i18n_lang_type">
      <el-select style="width:200px" 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-input v-model='params.product_type_lang_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="addProductTypeLangDialogVisible = 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 => getProductTypeLangList(params)">


    <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: '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>

  <!-- 添加产品类型多语言 -->
  <AddProductTypeLangDialog v-model="addProductTypeLangDialogVisible" :done="() => tableRef.reload()">
  </AddProductTypeLangDialog>
  <!-- 编辑产品类型多语言 -->
  <EditProductTypeLangDialog v-model="EditProductTypeLangDialogVisible" :data="EditProductTypeLangDialogRow"
    :done="() => tableRef.reload()"></EditProductTypeLangDialog>
  <!-- 产品类型多语言详情 -->
  <DetailProductTypeLangDialog v-model="DetailProductTypeLangDialogVisible" :data="DetailProductTypeLangDialogRow">
  </DetailProductTypeLangDialog>
</template>
<script setup>
import { ArrowDown } from '@element-plus/icons-vue';
import { ref, reactive, watch } from 'vue';
import { useLoginStore } from "~/store";
import { getProductTypeLangList, editProductTypeLang, deleteProductTypeLang, downloadTemplate, importExcel, exportExcel } from '~/service/product_type_lang';
import { getProductTypeTree } from "~/service/product_type";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import AddProductTypeLangDialog from './components/AddProductTypeLangDialog.vue';
import EditProductTypeLangDialog from './components/EditProductTypeLangDialog.vue';
import DetailProductTypeLangDialog from './components/DetailProductTypeLangDialog.vue';

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

const addProductTypeLangDialogVisible = ref(false);
const EditProductTypeLangDialogVisible = ref(false);
const EditProductTypeLangDialogRow = ref({});
const DetailProductTypeLangDialogVisible = ref(false);
const DetailProductTypeLangDialogRow = ref({});

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

// 查询参数
const params = reactive({
  product_type_guid: "",
  i18n_lang_type_guid: "",
  product_type_lang_name: "",

});
const column = [

  {
    fixed: true,
    type: 'selection'
  },
  {
    prop: "i18n_lang_type_code",
    label: '语言code',
  },
  {
    prop: "product_type_name",
    label: '所属产品类型',
  },
  {
    prop: "product_type_lang_name",
    label: '产品类型多语言名称',
  },
  {
    label: '操作',
    prop: 'chaoz',
    width: '250',
    fixed: 'right'
  }
];

// --业务参数
const dataList = ref();
const i18n_lang_type = ref();


// --业务方法
async function getList() {
  getProductTypeTree().then((res) => {
    if (res.code == 0) {
      dataList.value = res.data
    }
  })
}

async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}

getList()
geti18n_lang_typeList()


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 deleteProductTypeLang({
      product_type_lang_guid: data.map(v => v.product_type_lang_guid).join()
    });
    if (res) {
      tableRef.value.reload();
    }
  });
};

// 修改
function handleUpdate(row) {
  EditProductTypeLangDialogVisible.value = true
  EditProductTypeLangDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailProductTypeLangDialogVisible.value = true
  DetailProductTypeLangDialogRow.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();
};






</script>
