<!--
 * @Descripttion: (商品类目/tb_goods_category)
 * @version: (1.0)
 * @Author: (黎文豪)
 * @Date: (2023-06-12)
 * @LastEditors: (黎文豪)
 * @LastEditTime: (2023-06-12)
-->
<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!-- 搜索框 queryParams.需要搜索的字段 -->
      <el-form :model="queryParams" label-position="left" style="margin:15px;"  inline ref="queryForm" v-show="showSearch"
        @submit.prevent>
      <el-form-item label="名称" prop="goodsCategoryName">
        <el-input v-model="queryParams.goodsCategoryName" placeholder="请输入名称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="显示状态" prop="goodsCategoryDisplayStatus">
        <el-radio-group v-model="queryParams.goodsCategoryDisplayStatus">
          <el-radio v-for="item in  display_status " :key="item.dictValue" :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否为热门" prop="goodsCategoryIsPopular">
        <el-radio-group v-model="queryParams.goodsCategoryIsPopular">
          <el-radio v-for="item in  is_popular " :key="item.dictValue" :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
      </el-form>
    </el-row>

    <!-- 工具按钮 -->
    <el-row :gutter="10" class="mb8">
	  <el-col :span="1.5">
        <el-button type="info" plain icon="sort" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:goodscategory:addOrUpdate']" plain icon="plus" @click="AddDialogVisible = true">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:goodscategory:delete']" plain icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['business:goodscategory:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格渲染  prop="对应的字段"-->
	<el-table v-loading="loading" :data="dataList" ref="tableRef" highlight-current-row @selection-change="handleSelectionChange" v-if="refreshTable" :default-expand-all="isExpandAll" row-key="goodsCategoryGuid" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="selection" width="50" align="center" />
	  
      <el-table-column prop="goodsCategoryName" label="名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsCategoryImg" label="图片" align="center">
        <template #default="scope">
          <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain" :src="scope.row.goodsCategoryImg?.split(',')[0]" :preview-src-list="scope.row.goodsCategoryImg?.split(',')">
            <div><el-icon :size="15"><document /></el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsCategoryDisplayStatus" label="显示状态" align="center">
        <template #default="scope">
          <dict-tag :options=" display_status " :value="scope.row.goodsCategoryDisplayStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsCategoryIsPopular" label="是否为热门" align="center">
        <template #default="scope">
          <dict-tag :options=" is_popular " :value="scope.row.goodsCategoryIsPopular" />
        </template>
      </el-table-column>
      <el-table-column prop="productCategorySort" label="排序" align="center" sortable />

      <el-table-column label="操作" width="350" fixed="right">
        <template #default="scope">
          <el-button type="primary" size="small" icon="edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:goodscategory:addOrUpdate']">编辑</el-button>
          <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:goodscategory:delete']">删除</el-button>
          <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
  
  <!-- 添加 -->
  <AddDialog v-model="AddDialogVisible" :done="() => resetQuery()"></AddDialog>
  <!-- 编辑 -->
  <EditDialog v-model="EditDialogVisible" :data="EditDialogRow" :done="() => resetQuery()"></EditDialog>
  <!-- 详情 -->
  <DetailDialog v-model="DetailDialogVisible" :data="DetailDialogRow" :done="() => resetQuery()"></DetailDialog>
</template>
<script setup name="goodscategory">
import { ElMessageBox } from 'element-plus'
import modal from '@/plugins/modal.js'
import {  exportGoodsCategory,    goodsCategoryTreeList , delGoodsCategory } from '@/api/business/GoodsManager/GoodsCategorys/goodsCategory.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";

const AddDialogVisible = ref(false);
const EditDialogVisible = ref(false);
const EditDialogRow = ref({});
const DetailDialogVisible = ref(false);
const DetailDialogRow = ref({});

const { proxy } = getCurrentInstance()

// 选中categoryId数组数组
const ids = ref([])
// 非单选禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 数据列表
const dataList = ref([])
// 总记录数
const total = ref(0)
// 是否加载
const loading = ref(true)
// 是否展开，默认全部折叠
const isExpandAll = ref(false)
const refreshTable = ref(true)

const data = reactive({
  form: {},
  queryParams: {
  },
})
const { queryParams } = toRefs(data)

// 业务参数


// 业务方法

// 字典获取
const display_status = ref([]);
async function getdisplay_status() {
  await proxy.getDicts('display_status').then((res) => {
    display_status.value = res.data
  })
}
getdisplay_status()
// 字典获取
const is_popular = ref([]);
async function getis_popular() {
  await proxy.getDicts('is_popular').then((res) => {
    is_popular.value = res.data
  })
}
getis_popular()




//基础方法

// 树形搜索
function selectChild(str, tag, res, parent) {
  isExpandAll.value = true
  for (let item of tag) {
    if (item.goodsCategoryName.indexOf(str) !== -1) {
      if (parent) {
        res.push(parent)
      } else {
        res.push(item)
      }
    } else if (item.children && item.children.length) {
      selectChild(str, item.children, res, parent || item)
    }
  }
}

// 查询数据
function getList() {
  loading.value = true
  goodsCategoryTreeList(queryParams.value).then((res) => {
    if (res.code == 200) {
      loading.value = false

      if (queryParams.value.goodsCategoryName == undefined) {
        isExpandAll.value = false
        dataList.value = res.data
      } else {
        dataList.value = []
        selectChild(queryParams.value.goodsCategoryName, res.data, dataList.value)
      }

      total.value = res.data.totalNum
    }
  })
}
// 折叠/展开
function toggleExpandAll() {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

  // goodsCategoryList(queryParams.value).then((res) => {
  //   if (res.code == 200) {
  //     loading.value = false;
  //     dataList.value = res.data.result;
  //     total.value = res.data.totalNum;
  //   }
  // });


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.goodsCategoryId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置查询操作 */
function resetQuery() {
  proxy.resetForm('queryForm')
  handleQuery()
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.goodsCategoryId || ids.value

    ElMessageBox.confirm("是否确认删除？", "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
    .then(function () {
      return delGoodsCategory(Ids)
    })
    .then(() => {
      handleQuery()
      modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}

/** 导出按钮操作 */
function handleExport(row) {
  const Ids = row.goodsCategoryId || ids.value
  const name = ref("所有")

  if (Ids.length != 0) {
    let str = ''
    for (const key in Ids) {
      str += Ids[key] + ','
    }
    str = str.slice(0, str.length - 1)
    queryParams.value.ids = str
    name.value = "选中"
  }

  ElMessageBox.confirm('是否确认导出' + name.value + '数据?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(function () {
      return exportGoodsCategory(queryParams.value)
    })
    .then((response) => {
      proxy.download(response.data.path)
    })
}


// 修改
function handleUpdate(row) {
  EditDialogVisible.value = true
  EditDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailDialogVisible.value = true
  DetailDialogRow.value = row
}

handleQuery()
</script>
