<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="selectionChange"
    >
      <template #menu-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
        v-model="dialog1.visible"
        :title="dialog1.title"
        width="950px"
        @close="dialog1.visible = false"
    >
      <choice-item-page enabled="" @materialInfo="materialInfo"/>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";

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;
};

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/wms/alarm",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const dialog1 = reactive({
  title: "物料选择",
  visible: false,
});
const materialInfo = (value) => {
  form.value.code = value.materialCode
  form.value.name = value.materialName
  form.value.unit = value.unitDictValue
  dialog1.visible = false
}
const crudRef = ref(null); //crudRef.value 获取avue-crud对象

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  column: [
    {
      label: "仓库编码",
      prop: "houseNo",
      type: "select",
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.warehouse_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "仓库编码不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "物料编码",
      prop: "code",
      width: 150,
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          message: "物料编码不能为空",
          trigger: "trigger",
        },
      ],
      click: ({ value, column }) => {
        if(column.boxType){
          dialog1.visible = true
        }
      },
    },
    {
      label: "物料名称",
      prop: "name",
      search: true,
      width: 150,
      overHidden: true,
      disabled: true,
      rules: [
        {
          required: true,
          message: "物料名称不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "最小库存",
      prop: "min",
      type: "number",
      min: 0,
      max: 999999,
      rules: [
        {
          required: true,
          message: "最小安全库存不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "最大库存",
      prop: "max",
      type: "number",
      min: 0,
      max: 999999,
      rules: [
        {
          required: true,
          message: "最大安全库存不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "单位",
      prop: "unit",
      disabled: true,
    },
    {
      label: "状态",
      prop: "state",
      type: "select",
      dicData: [{label: '启用' , value: 0},{label: '禁用' , value: 1}],
      html: true,
      formatter: (val) => {
        if (val.state === 0) {
          return '<b class="el-tag el-tag--success el-tag--light">启用</b>';
        }
        return '<b class="el-tag el-tag--danger el-tag--light">禁用</b>';
      },
      rules: [
        {
          required: true,
          message: "状态不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "提示内容",
      prop: "msg",
      width: 200,
      overHidden: true,
      rules: [
        {
          required: true,
          message: "提示内容不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "创建时间",
      prop: "created",
      width: 180,
      display: false
    },
    {
      label: "创建人",
      prop: "creator",
      display: false
    },
  ],
});

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