<template>
  <div>
    <!-- <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="160px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="供应商分类" prop="category_id">
                            <el-select class="w-[280px]" v-model="queryParams.category_id" clearable placeholder="请选择供应商分类">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.mer_category_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="供应商类型" prop="type_id">
                            <el-select class="w-[280px]" v-model="queryParams.type_id" clearable placeholder="请选择供应商类型">
                                <el-option label="全部" value=""></el-option>
                                <el-option v-for="(item, index) in dictData.merchat_type" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="resetPage">查询</el-button>
                            <el-button @click="resetParams">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card> -->
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button
        v-perms="['merchat.merchant/add']"
        type="primary"
        @click="showDialog = true"
      >
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        商品绑定
      </el-button>
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column label="ID" prop="id" width="55" />
          <el-table-column label="商品图片" prop="imgs">
            <template #default="{ row }">
              <el-image style="width: 50px; height: 50px" :src="row.imgs" />
            </template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop="goods_name"
            show-overflow-tooltip
          />
          <el-table-column label="规格型号" prop="spec" show-overflow-tooltip />
          <el-table-column
            label="商品分类"
            prop="class_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="商品单价"
            prop="price"
            show-overflow-tooltip
          />
          <el-table-column label="操作" width="170" fixed="right">
            <template #default="{ row }">
              <el-button
                v-perms="['supplier.supplier/delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <el-dialog v-model="showDialog" title="绑定商品" width="70%">
      <supplierGoodsPop
        @offPop="(showDialog = false), getLists()"
        :sid="id"
      ></supplierGoodsPop>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="merchantLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiSupplierBindGoods, apiSupplierBindGoodsDelete } from "@/api/supplier";
import { useRoute } from "vue-router";
import feedback from "@/utils/feedback";
const route = useRoute();
const showDialog = ref(false);
let id = route.query.id;

// 查询条件
const queryParams = reactive({
    supplier_id: id,
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData(
  "mer_category_type,merchat_type,show_status,off_status,financial_type"
);

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiSupplierBindGoods,
  params: queryParams,
});

getLists();

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiSupplierBindGoodsDelete({ id });
  getLists();
};

</script>
