<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="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 toDeleteIds" style="width: 200px;height:280px;font-size:10px;text-align: center;">
          <vue-qrcode  :value="item.locationNo"  size="45" error-level="H"></vue-qrcode>
          <div>
            <el-text>编码:</el-text><el-text>{{item.houseNo}}</el-text>
          </div>
          <div>
            <el-text>货区:</el-text><el-text>{{item.area}}</el-text>
          </div>
          <div>
            <el-text>货位:</el-text><el-text>{{item.coordinate}}</el-text>
          </div>
          <div>
            <el-text>层数:</el-text><el-text>{{item.layer}}</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);
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/wms/position",
  });
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对象
const dialog = reactive({
  title: "二维码打印",
  visible: false,
});
const printPage = ()=>{
  html2CanvasPrint(toPrintRef.value);
}
const printCode = () =>{
  dialog.visible = true
}
const selectionChange1 =(row)=>{
  toDeleteIds.value = row
}
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  addBtn: false,
  column: [
    {
      label: "仓库编码",
      prop: "type",
      type: "select",
      search: true,
      editDisabled: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.warehouse_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "仓库编码不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "货区",
      prop: "area",
      editDisabled: true,
      rules: [
        {
          required: true,
          message: "货区不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "货位",
      prop: "coordinate",
      search: true,
      editDisabled: true,
      rules: [
        {
          required: true,
          message: "货位不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "层数",
      prop: "layer",
      type: "number",
      rules: [
        {
          required: true,
          message: "层数不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "货位类型",
      prop: "materialType",
      type: "select",
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.storage_location_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "货位类型不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "状态",
      prop: "enable",
      type: "select",
      dicData: [{label: '启用' , value: "0"},{label: '禁用' , value: "1"}],
      html: true,
      formatter: (val) => {
        if (val.enable === "0") {
          return '<b class="el-tag el-tag--success el-tag--light">启用</b>';
        }else{
          return '<b class="el-tag el-tag--danger el-tag--light">禁用</b>';
        }
      },
      rules: [
        {
          required: true,
          message: "状态不能为空",
          trigger: "trigger",
        },
      ],
    },
  ],
});

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