<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="primary"
          :size="size"
          @click="dialog3.visible = true"
        >出库
        </el-button
        >
      </template>
    </avue-crud>

    <el-dialog
      v-model="dialog3.visible"
      :title="dialog3.title"
      width="550px"
      @close="dialog3.visible = false"
    >
      <el-input
        v-model="outWorkOrderCode"
        placeholder="选择出库工单号"
        :prefix-icon="Search"
        @click="dialog4.visible = true"
      >
      </el-input>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog3.visible = false">取消</el-button>
          <el-button type="primary" @click="outInventory">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      v-model="dialog4.visible"
      :title="dialog4.title"
      width="950px"
      @close="dialog4.visible = false"
    >
      <work-order-page @order-info="materialInfo2" :queryType="99"/>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook } from "@/store";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
    useCrud({
      src: "/api/v1/semiFinishedStock/detail",
    });
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对象
import {getSemi, oprerate, outInv} from "@/api/storage/index";
import { useDictionaryStore } from "@/store";
import {Search} from "@element-plus/icons-vue";
const { dicts } = useDictionaryStore();
const emits = defineEmits(["close"]);
const ruleFormRef = ref(null);
const outWorkOrderCode = ref(null);
const dialog1 = reactive({
  title: "工单号选择",
  visible: false,
});
const materialInfo1 = (value) => {
  formData.value.outWorkOrderCode = value.workOrderCode;
  dialog1.visible = false;
};

const materialInfo2 = async (value) => {
  outWorkOrderCode.value = value.workOrderCode;
  dialog4.visible = false;
};
const dialog3 = reactive({
  title: "出库",
  visible: false,
});

const dialog4 = reactive({
  visible: false,
  title: "工单编码选择",
});
const outInventory = () => {

  outInv({
    inStockId: props.id,
    ids: toDeleteIds.value,
    outNum: toDeleteIds.value.length,
    outWorkOrderCode: outWorkOrderCode.value
  }).then((res) => {
    if (res.code == "200") {
      ElMessage.success("出库成功！");
    } else {
      ElMessage.error("出库失败！");
    }
    dialog3.visible = false;
    outWorkOrderCode.value = null;
    search.value.inStockId = props.id;
    dataList();
  });

};
const formData = ref({
  stockId: "",
  stockType: "2",
  inWorkOrderCode: "",
  workOrderSeqNos: [],
});
const props = defineProps({
  id: {
    type: String,
  },
});
const detailsData = ref();
const tableData = ref([]);
const getData = async () => {
  const { data } = await getSemi(props.id);
  detailsData.value = data;
  formData.value.inWorkOrderCode = data.inWorkOrderCode;
  tableData.value = detailsData.value.detailsList;
};
const submit = async () => {
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      const { code } = await oprerate({ ...formData.value, stockId: props.id });
      if (code == "200") {
        ElMessage.success("出库成功！");
        ruleFormRef.value.resetFields();
        getData();
        emits("close");
      }
    } else {
    }
  });
};
onMounted(() => {
  search.value.inStockId=props.id;
  dataList();
});

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  delBtn: false,
  selection: true,
  labelWidth: 100,
  editBtn: false,
  viewBtn: false,
  menu: false,
  addBtn: false,
  selectable: (row) => {
    // 返回 true 表示允许勾选，false 表示禁用勾选
    return row.inventoryStatus === 1; // 例如：状态不是 "已审核" 的行才能勾选
  },
  column: [
    {
      label: "管号",
      prop: "workOrderSeqNo",
      editDisabled: true,
      addDisplay: false,
      search: true,
    },
    {
      label: "生产批号",
      prop: "workOrderCode",
      editDisabled: true,
      addDisplay: false,
      search: true,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },
    {
      label: "出库工单号",
      prop: "outWorkOrderCode",
      editDisabled: true,
      addDisplay: false,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },
    {
      label: "是否已出库",
      prop: "inventoryStatus",
      editDisabled: true,
      addDisplay: false,
      html: true,
      formatter: (val) => {
        if (val.inventoryStatus === 2) {
          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>';
        }
      },
    },

  ]
});
watch(
  () => props.id,
  () => {
    search.value.inStockId=props.id;
    dataList();
  }
);
</script>
