<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="{ size, row, index }">
        <el-button
          icon="el-icon-info"
          text
          @click="openDialog(row.id)"
          type="primary"
          :size="size"
        >
          详情</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog1.visible"
      :title="dialog1.title"
      width="950px"
      @close="dialog1.visible = false"
    >
      <work-order-page
        :queryType="2"
        :workOrderState="6"
        @order-info="materialInfo1"
      />
    </el-dialog>
    <el-dialog
      v-model="dialog2.visible"
      :title="dialog2.title"
      width="950px"
      @close="dialog2.visible = false"
    >
      <work-order-seqNos-page
        :workOrderCode="form.workOrderCode"
        @order-info="materialInfo2"
      />
    </el-dialog>
    <el-dialog
      v-model="dialog3.visible"
      :title="dialog3.title"
      width="950px"
      @close="dialog3.visible = false"
    >
      <Details
        :id="selectId"
        @close="
          dialog3.visible = false;
          dataList();
        "
      />
    </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";
import dictDataUtil from "@/common/configs/dictDataUtil";
import { useDictionaryStore } from "@/store";
import Details from "./components/details.vue";
const { dicts } = useDictionaryStore();
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/semiFinishedStock",
  });
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 openDialog = (id) => {
  selectId.value = id;
  dialog3.visible = true;
};
const dialog1 = reactive({
  title: "工单号选择",
  visible: false,
});
const dialog2 = reactive({
  title: "流转卡号选择",
  visible: false,
});
const dialog3 = reactive({
  title: "详情",
  visible: false,
});
const selectId = ref(null);
const materialInfo1 = (value) => {
  if (form.value.stockType == "1") {
    form.value.inWorkOrderId = value.workOrderCode;
  } else if (form.value.stockType == "2") {
    form.value.outWorkOrderId = value.workOrderCode;
  }
  form.value.workOrderId = value.id;
  form.value.workOrderSeqNos = [];
  dialog1.visible = false;
};
const materialInfo2 = (value) => {
  form.value.workOrderSeqNos = value;
  dialog2.visible = false;
};
const seqDisabled = computed(() => {
  return !form.value.workOrderId;
});
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  labelWidth: 100,
  editBtn: false,
  viewBtn: false,
  column: [
    {
      label: "产品编码",
      prop: "materialCode",
      editDisabled: true,
      addDisplay: false,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },
    {
      label: "工艺路线名称",
      prop: "processRouteName",
      editDisabled: true,
      addDisplay: false,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },
    {
      label: "库存数量",
      prop: "stockNum",
      editDisabled: true,
      addDisplay: false,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },

    {
      label: "出库/入库数量",
      prop: "operateNum",
      addDisplay: false,
      editDisabled: true,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
    },

    {
      label: "出入库类型",
      prop: "stockType",
      editDisabled: true,
      addDisplay: true,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
      type: "select",
      dicData: [
        {
          label: "入库",
          value: "1",
        },
        {
          label: "出库",
          value: "2",
        },
      ],
      control: (val, form) => {
        return {
          inWorkOrderId: {
            display: val == "1",
          },
          outWorkOrderId: {
            display: val == "2",
          },
        };
      },
    },
    {
      label: "入库工单",
      prop: "inWorkOrderId",
      editDisabled: true,
      display: true,
      addDisplay: false,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
      type: "select",
      click: ({ value, column }) => {
        if (column.boxType) {
          dialog1.visible = true;
        }
      },
      change: ({ value, column }) => {
        if (value != form.value.materialCode) {
          dialog1.visible = true;
        }
      },
    },
    {
      label: "出库工单",
      prop: "outWorkOrderId",
      editDisabled: true,
      addDisplay: false,
      display: true,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
      type: "select",
      click: ({ value, column }) => {
        if (column.boxType) {
          dialog1.visible = true;
        }
      },
      change: ({ value, column }) => {
        if (value != form.value.materialCode) {
          dialog1.visible = true;
        }
      },
    },
    {
      label: "流转卡号",
      prop: "workOrderSeqNos",
      editDisabled: true,
      hide: true,
      disabled: seqDisabled,
      addDisplay: true,
      filterable: true,
      rules: [
        {
          required: true,
          trigger: "trigger",
        },
      ],
      click: ({ value, column }) => {
        if (column.boxType && dialog1.visible == false) {
          dialog2.visible = true;
        }
      },
      change: ({ value, column }) => {
        if (
          form.value.stockType == "1"
            ? form.value.inWorkOrderId && dialog1.visible == false
            : form.value.outWorkOrderId && dialog1.visible == false
        ) {
          dialog2.visible = true;
        }
      },
    },
    {
      label: "库位",
      prop: "location",
      addDisplay: true,
      rules: [
        {
          required: true,
          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,
    //   rules: [
    //     {
    //       required: true,
    //       message: "料箱编号不能为空",
    //       trigger: "trigger",
    //     },
    //   ],
    // },
    // {
    //   label: "仓库坐标",
    //   prop: "locationNo",
    //   editDisabled: true,
    //   rules: [
    //     {
    //       required: true,
    //       message: "仓库位置不能为空",
    //       trigger: "trigger",
    //     },
    //   ],
    // },
    // {
    //   label: "二维码",
    //   width: 120,
    //   overHidden: true,
    //   prop: "batchCode",
    //   search: true,
    // },

    // {
    //   label: "物料名称",
    //   prop: "materialName",
    //   width: 130,
    //   overHidden: true,
    //   click: ({ value, column }) => {
    //     if (column.boxType) {
    //       dialog1.visible = true;
    //     }
    //   },

    // },
    // {
    //   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;
    //     }
    //   },
    // },
  ],
});
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(() => {
  dataList();
});
</script>
