<template>
  <div class="mainContentBox">
    <avue-crud
        ref="crudRef"
        v-model:search="search"
        v-model="form"
        :data="data"
        :option="option"
        v-model:page="page"
        @row-save="createRow"
        @row-update="updateRow"
        @row-del="deleteRow"
        @search-change="searchChange"
        @search-reset="resetChange"
        @size-change="dataList"
        @current-change="dataList"
        @selection-change="selectionChange1"
    >
      <template #menu-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</el-button
        >
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="primary"
          icon="el-icon-view"
          :size="size"
          @click="printCode"
        >打 印</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="1250px"
      @close="dialog.visible = false"
    >
      <div style="display: flex;flex-wrap: wrap;" ref="toPrintRef">
        <div v-for="item of clickRows" style="width: 200px;height:280px;font-size:10px;text-align: center;">
          <vue-qrcode  :value="item.code"  size="45" error-level="H"></vue-qrcode>
          <div>
            <el-text>编码:</el-text><el-text>{{item.code}}</el-text>
          </div>
          <div>
            <el-text>名称:</el-text><el-text>{{item.name}}</el-text>
          </div>
          <div>
            <el-text>类型:</el-text><el-text>{{item.$category}}</el-text>
          </div>
          <div>
            <el-text>容量:</el-text><el-text>{{item.capacity}}</el-text>
          </div>
        </div>
      </div>
      <div class="dialog-footer" align="center">
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="printPage">打印</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { html2CanvasPrint } from "@/utils/common";
import { useCommonStoreHook } from "@/store";
import dictDataUtil from "@/common/configs/dictDataUtil";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const toPrintRef = ref(null);
const dialog = reactive({
  title: "二维码打印",
  visible: false,
});
const printPage = ()=>{
  html2CanvasPrint(toPrintRef.value);
}
const printCode = () =>{
  dialog.visible = true
}
const clickRows =ref([])
const selectionChange1 =(row)=>{
  toDeleteIds.value = [];
  row?.forEach((element) => {
    toDeleteIds.value.push(element.id);
  });
  clickRows.value = row
}
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/wms/vehicle",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  column: [
    {
      label: "载具编码",
      prop: "code",
      search: true,
      rules: [
        {
          required: true,
          message: "载具编码不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "载具名称",
      prop: "name",
      search: true,
      rules: [
        {
          required: true,
          message: "载具名称不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "载具类型",
      prop: "category",
      type: "select",
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.vehicle_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "载具类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "容量",
      prop: "capacity",
      type: "number",
      min:  1,
      max: 99999,
      rules: [
        {
          required: true,
          message: "容量不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "使用次数",
      prop: "maxUseNum",
      type: "number",
      min:  1,
      max: 99999,
      rules: [
        {
          required: true,
          message: "最大使用次数不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "状态",
      prop: "state",
      type: "select",
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.vehicle_state,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "状态不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "创建时间",
      prop: "created",
      display: false
    },
    {
      label: "创建人",
      prop: "creator",
      display: false
    },
  ],
});

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
</script>
