<template>
  <div class="app-container">
    <!-- 列表 -->
    <!-- 搜索 -->
    <page-search ref="searchRef" :search-config="searchConfig" @query-click="handleQueryClick"
      @reset-click="handleResetClick" />

    <!-- 列表 -->
    <page-content ref="contentRef" :content-config="contentConfig" @add-click="handleAddClick"
      @edit-click="handleEditClick" @export-click="handleExportClick" @search-click="handleSearchClick"
      @toolbar-click="handleToolbarClick" @operat-click="handleOperatClick" @filter-change="handleFilterChange">
      <template #status="scope">
        <el-tag :type="scope.row[scope.prop] == 1 ? 'success' : 'info'">
          {{ scope.row[scope.prop] == 1 ? "启用" : "禁用" }}
        </el-tag>
      </template>
      <template #contentType="scope">
        {{ scope.row.contentType == 'yxyPrinter' ? "云想印" : "飞鹅" }}
      </template>
      <template #subType="scope">
        {{ scope.row.subType == 'label' ? "标签" : "小票" }}
      </template>
      <template #caozuo="scope">
        {{ scope }}
      </template>
      <template #gender="scope">
        <DictLabel v-model="scope.row[scope.prop]" code="gender" />
      </template>
      <template #operate="scope">
        <div v-if="scope.row.connectionType != 'USB'">
          <el-button @click="handleEditClick(scope.row)" icon="Edit" type="primary" link>编辑</el-button>
          <el-button @click="handdeleteevent(scope.row)" icon="Delete" type="danger" link>删除</el-button>
        </div>
      </template>
      <!-- <template #mobile="scope">
        <el-text>{{ scope.row[scope.prop] }}</el-text>
        <copy-button v-if="scope.row[scope.prop]" :text="scope.row[scope.prop]" style="margin-left: 2px" />
      </template> -->
    </page-content>

    <!-- 新增 -->
    <page-modal ref="addModalRef" :modal-config="addModalConfig" @submit-click="handleSubmitClick">
      <template #gender="scope">
        <Dict v-model="scope.formData[scope.prop]" code="gender" />
      </template>
      <template #classifyPrintData="scope">
        <template v-if="scope.formData.classifyPrint == 1">
          <el-checkbox-group v-model="scope.formData.categoryIdsArr">
            <template v-for="col in PrinterTypeList" :key="col">
              <el-checkbox :value="col.id * 1" :label="col.name" />
            </template>
          </el-checkbox-group>
        </template>
      </template>
    </page-modal>

    <!-- 编辑 -->
    <page-modal ref="editModalRef" :modal-config="editModalConfig" @submit-click="handleSubmitClick">
      <template #gender="scope">
        <Dict v-model="scope.formData[scope.prop]" code="gender" v-bind="scope.attrs" />
      </template>
      <template #classifyPrintData="scope">
        <template v-if="scope.formData.classifyPrint == 1">
          <el-checkbox-group v-model="scope.formData.categoryIdsArr">
            <template v-for="col in PrinterTypeList" :key="col">
              <el-checkbox :value="col.id * 1" :label="col.name" />
            </template>
          </el-checkbox-group>
        </template>
      </template>
    </page-modal>
  </div>
</template>

<script setup lang="ts">
import UserAPI from "@/api/account/printer";
import type { IObject, IOperatData } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";
import addModalConfig from "./config/add";
import contentConfig from "./config/content";
import editModalConfig from "./config/edit";
import searchConfig from "./config/search";

const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  // handleAddClick,
  // handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();
onMounted(() => {
  getPrinterType();
});
let PrinterTypeList = ref([])
// 获取商品分类
async function getPrinterType() {
  let res = await UserAPI.getPrinterType();
  PrinterTypeList.value = res.records
}
function handdeleteevent(item) {
  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    contentConfig.deleteAction(item.id).then(() => {
      ElMessage.success("删除成功");
      handleQueryClick();
    });

  });
}
// 新增
async function handleAddClick() {
  addModalRef.value?.setModalVisible();
}
// 编辑
async function handleEditClick(row: IObject) {
  editModalRef.value?.handleDisabled(false);
  editModalRef.value?.setModalVisible();
  // 根据id获取数据进行填充
  let data = await UserAPI.get(row.id);
  data.printType = data.printType.split(',');

  if (data.categoryIds) {
    data.categoryIdsArr = JSON.parse(data.categoryIds)
  }

  data.classifyPrint = data.classifyPrint * 1;

  editModalRef.value?.setFormData(data);
}
// 其他工具栏
function handleToolbarClick(name: string) {
  console.log(name);
  if (name === "custom1") {
    ElMessage.success("点击了自定义1按钮");
  }
}
// 其他操作列
async function handleOperatClick(data: IOperatData) {
  console.log(data);
  if (data.name === "detail") {
    // 禁用表单编辑
    editModalRef.value?.handleDisabled(true);
    // 打开抽屉
    editModalRef.value?.setModalVisible();
    // 根据id获取数据进行填充
    const formData = await UserAPI.getFormData(data.row.id);
    // 设置表单数据
    editModalRef.value?.setFormData(formData);
  }
}

// 切换示例
const isA = ref(true);
</script>
