<!--
 * @Descripttion: (商品/tb_goods)
 * @version: (1.0)
 * @Author: (黎文豪)
 * @Date: (2023-06-19)
 * @LastEditors: (黎文豪)
 * @LastEditTime: (2023-06-19)
-->
<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="店铺guid" prop="shopGuid">
          <el-input v-model.number="queryParams.shopGuid" placeholder="请输入店铺guid" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="商品类目guid" prop="goodsCategoryGuid">
          <el-input v-model.number="queryParams.goodsCategoryGuid" placeholder="请输入商品类目guid" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="名称" prop="goodsName">
          <el-input v-model="queryParams.goodsName" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="编码" prop="goodsCoding">
          <el-input v-model="queryParams.goodsCoding" placeholder="请输入编码" clearable @keyup.enter="handleQuery" />
        </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="primary" v-hasPermi="['business:goods: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:goods:delete']" plain icon="delete"
          @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Upload" @click="UploadDialogVisible = true"
          v-hasPermi="['business:goods:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['business:goods: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" border highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />

      <el-table-column prop="goodsName" label="名称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsCoding" label="编码" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsMainImageVideo" label="主图视频" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsVideoCover" 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.goodsVideoCover?.split(',')[0]" :preview-src-list="scope.row.goodsVideoCover?.split(',')">
            <div><el-icon :size="15">
                <document />
              </el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsPicture" 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.goodsPicture?.split(',')[0]" :preview-src-list="scope.row.goodsPicture?.split(',')">
            <div><el-icon :size="15">
                <document />
              </el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsPriceLowest" label="价格(最低)" align="center" />
      <el-table-column prop="goodsPriceHighest" label="价格(最高)" align="center" />
      <el-table-column prop="goodsDashedPriceLowest" label="划线价格(最低)" align="center" />
      <el-table-column prop="goodsTotalInventory" label="库存总量" align="center" />
      <el-table-column prop="goodsSalesInitial" label="初始销量" align="center" />
      <el-table-column prop="goodsSalesActual" label="实际销量" align="center" />
      <el-table-column prop="goodsDetails" label="详情" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsDeductStockType" label="库存计算方式(1下单立减库存 2付款立减库存)" align="center">
        <template #default="scope">
          <dict-tag :options="deduct_stock_type" :value="scope.row.goodsDeductStockType" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsIsPointsGift" label="是否赠送积分(1开启 0关闭)" align="center">
        <template #default="scope">
          <dict-tag :options="is_points_gift" :value="scope.row.goodsIsPointsGift" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsIsPointsDiscount" label="是否允许使用积分抵扣(1允许 0不允许)" align="center">
        <template #default="scope">
          <dict-tag :options="is_points_discount" :value="scope.row.goodsIsPointsDiscount" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsIsAlonePointsDiscount" label="积分抵扣设置(0默认抵扣 1单独设置抵扣)" align="center">
        <template #default="scope">
          <dict-tag :options="is_alone_points_discount" :value="scope.row.goodsIsAlonePointsDiscount" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsPointsDiscountConfig" label="单独设置积分抵扣的配置" align="center"
        :show-overflow-tooltip="true" />
      <el-table-column prop="goodsIsEnableGrade" label="是否开启会员折扣(1开启 0关闭)" align="center">
        <template #default="scope">
          <dict-tag :options="is_enable_grade" :value="scope.row.goodsIsEnableGrade" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsIsAloneGrade" label="会员折扣设置(0默认等级折扣 1单独设置折扣)" align="center">
        <template #default="scope">
          <dict-tag :options="is_alone_grade" :value="scope.row.goodsIsAloneGrade" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsAloneGradeEquity" label="单独设置折扣的配置" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="goodsShelfStatus" label="上下架状态(1上架  2下架)" align="center">
        <template #default="scope">
          <dict-tag :options="shelf_status" :value="scope.row.goodsShelfStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="goodsSort" label="排序" align="center" />

      <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:goods:addOrUpdate']">编辑</el-button>
          <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:goods:delete']">删除</el-button>
          <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </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>
  <!-- 导入 -->
  <UploadDialog v-model="UploadDialogVisible" :done="() => resetQuery()"></UploadDialog>
</template>
<script setup name="goods">
import { ElMessageBox } from 'element-plus'
import modal from '@/plugins/modal.js'
import { exportGoods, goodsList, delGoods } from '@/api/business/GoodsManager/Goodss/goods.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";
import UploadDialog from "./components/UploadDialog.vue";

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

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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
})
const { queryParams } = toRefs(data)

// 业务参数


// 业务方法

// 字典获取
const deduct_stock_type = ref([]);
async function getdeduct_stock_type() {
  await proxy.getDicts('deduct_stock_type').then((res) => {
    deduct_stock_type.value = res.data
  })
}
getdeduct_stock_type()
// 字典获取
const is_points_gift = ref([]);
async function getis_points_gift() {
  await proxy.getDicts('is_points_gift').then((res) => {
    is_points_gift.value = res.data
  })
}
getis_points_gift()
// 字典获取
const is_points_discount = ref([]);
async function getis_points_discount() {
  await proxy.getDicts('is_points_discount').then((res) => {
    is_points_discount.value = res.data
  })
}
getis_points_discount()
// 字典获取
const is_alone_points_discount = ref([]);
async function getis_alone_points_discount() {
  await proxy.getDicts('is_alone_points_discount').then((res) => {
    is_alone_points_discount.value = res.data
  })
}
getis_alone_points_discount()
// 字典获取
const is_enable_grade = ref([]);
async function getis_enable_grade() {
  await proxy.getDicts('is_enable_grade').then((res) => {
    is_enable_grade.value = res.data
  })
}
getis_enable_grade()
// 字典获取
const is_alone_grade = ref([]);
async function getis_alone_grade() {
  await proxy.getDicts('is_alone_grade').then((res) => {
    is_alone_grade.value = res.data
  })
}
getis_alone_grade()
// 字典获取
const shelf_status = ref([]);
async function getshelf_status() {
  await proxy.getDicts('shelf_status').then((res) => {
    shelf_status.value = res.data
  })
}
getshelf_status()




//基础方法

// 查询数据
function getList() {
  loading.value = true


  goodsList(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.goodsId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

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

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

/** 导出按钮操作 */
function handleExport(row) {
  const Ids = row.goodsId || 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 exportGoods(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>
