<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"
      :row-style="rowStyle"
      :cell-style="cellStyle"
      @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 @material-info="materialInfo" />
    </el-dialog>
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
    <CommonTable
        ref="houseRef"
        tableTitle="选择仓库位置"
        tableType="HOUSE_POSITION"
        @selected-sure="onSelectedFinish"
    />
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";

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/stock",
  });
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 dialog1 = reactive({
  title: "物料选择",
  visible: false,
});
const materialInfo = (value) => {
  form.value.materialNo = value.materialCode;
  form.value.materialName = value.materialName;
  form.value.materialModel = value.spec;
  form.value.unit = value.unitDictValue;
  dialog1.visible = false;
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  delBtn: false,
  selection: true,
  column: [
    /*{
      label: "入库单号",
      prop: "taskNo",
      search: true,
      rules: [
        {
          required: true,
          message: "入库单号不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "计划单号",
      prop: "planNo",
      search: true,
      rules: [
        {
          required: true,
          message: "计划单号不能为空",
          trigger: "trigger",
        },
      ],
    },*/
    /*{
      label: "仓库编码",
      prop: "houseType",
      type: "select",
      search: true,
      width: 90,
      overHidden: 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: "taskNo",
      //type: "select",
      search: true,
      width: 90,
      overHidden: true,
      //dicUrl: dictDataUtil.request_url + "stock_material_type",
     /* props: {
        label: "dictLabel",
        value: "dictValue",
      },*/
      rules: [
        {
          required: true,
          message: "入库单号不能为空",
          trigger: "trigger",
        },
      ],
    },
   /* {
      label: "料箱编号",
      prop: "vehicleCode",
      editDisabled: true,
      search: true,
      rules: [
        {
          required: true,
          message: "料箱编号不能为空",
          trigger: "trigger",
        },
      ],
    },*/
    {
      label: "仓库编号",
      prop: "houseNo",
      readonly: "true",
      click: () => {
        selectHouse();
      },
      rules: [
        {
          required: true,
          message: "仓库编号",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "仓库名称",
      prop: "houseName",
      disabled: true,
      rules: [
        {
          required: true,
          message: "仓库名称",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "库位",
      prop: "coordinate",
      disabled: true,
      rules: [
        {
          required: true,
          message: "仓库名称",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "层级",
      prop: "position",
      disabled: true,
      rules: [
        {
          required: true,
          message: "仓库名称",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "二维码",
      width: 120,
      overHidden: true,
      prop: "batchCode",
      search: true,
    },
    /*{
      label: "流转卡号",
      width: 130,
      overHidden: true,
      prop: "seqNo",
    },*/
    {
      label: "物料名称",
      prop: "materialName",
      width: 130,
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          message: "物料名称不能为空",
          trigger: "trigger",
        },
      ],
      click: ({ value, column }) => {
        if (column.boxType) {
          dialog1.visible = true;
        }
      },
      /*rules: [
        {
          required: true,
          message: "物料名称不能为空",
          trigger: "trigger",
        },
      ],*/
    },
    {
      label: "数量",
      prop: "num",
      type: "number",
      min: 0,
      max: 99999,
      width: 150,

      overHidden: true,
      formatter: (val, value, label) => {
        if (val.warningMsg) {
          return val.num + "(" + val.warningMsg + ")";
        } else {
          return val.num;
        }
      },
    },
    {
      label: "单位",
      disabled: true,
      display: false,
      dicUrl: dictDataUtil.request_url + "danwei_type",
       props: {
         label: "dictLabel",
         value: "dictValue",
       },
      prop: "unit",
    },
  ],
});
const houseRef=ref(null);
const selectHouse =()=>{
  houseRef.value.startSelect();
}
const onSelectedFinish =(val)=>{
  form.value.houseNo=val.houseNo;
  form.value.houseName=val.houseName;
  form.value.coordinate=val.coordinate;
  form.value.position=val.layer;
}
const rowStyle = ({ row, column, rowIndex }) => {
  if (row.warningMsg) {
    return {
      backgroundColor: "#f3d2d2",
      color: "#6c6a6a",
    };
  }
};
const cellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 10) {
    if (row.warningMsg) {
      return {
        color: "red",
        fontWeight: "bold",
        fontSize: "20",
      };
    } else {
      return {
        color: "#60fc56",
        fontWeight: "bold",
        fontSize: "20",
      };
    }
  }
};
onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataList();
};
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/device/import");
  }
};
</script>
