<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
        >
        <el-button
            type="primary"
            :size="size"
            @click="syncPosition"
        >立体仓同步
          <template #icon>
            <svg-icon icon-class="paicheng" />
          </template>
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
        v-model="dialog1.visible"
        :title="dialog1.title"
        width="950px"
        @close="dialog1.visible = false"
    >
      <choice-item-page @materialInfo="materialInfo"/>
    </el-dialog>
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { syncPositionVehicle} from "@/api/storage";
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
}
const syncPosition = () => {
  ElMessageBox.confirm("当前非同步任务,预计5秒内完成,确定同步库存吗?")
      .then(() => {
        syncPositionVehicle({}).then((data) => {
          ElMessage.success(data.msg);
        });
      })
      .catch(() => {
        // catch error
      });
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  column: [
    {
      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: "vehicleCode",
      editDisabled: true,
      rules: [
        {
          required: true,
          message: "料箱编号不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "仓库坐标",
      prop: "coordinate",
      editDisabled: true,
      rules: [
        {
          required: true,
          message: "仓库位置不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "层数",
      prop: "position",
      rules: [
        {
          required: true,
          message: "层数不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "批次号",
      width: 120,
      overHidden: true,
      prop: "batchCode",
      search: true
    },
    /*{
      label: "入库单号",
      prop: "taskNo",
      search: true,
    },*/
    {
      label: "计划单号",
      prop: "planNo",
      search: true,
    },
    {
      label: "流转卡号",
      width: 130,
      overHidden: true,
      prop: "seqNo",
    },
    {
      label: "物料名称",
      prop: "materialName",
      width: 130,
      readonly: true,
      overHidden: true,
      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: "单位",
      prop: "unit",
    },
  ],
});
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>
