<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="{ row, index, type }">
        <el-button type="primary" link size="small" @click="viewData(row, 0)"
          ><i-ep-view />查看
        </el-button>
        <el-button
          v-if="row.state == 0"
          type="primary"
          link
          size="small"
          @click="handleEdit(row, 0)"
          ><i-ep-edit />编辑
        </el-button>
        <el-button
          v-if="row.state == 0 && userStore.user.username == row.manager"
          type="primary"
          link
          size="small"
          @click="handle(row, 2)"
          ><i-ep-edit />确认接收
        </el-button>
        <el-button
          v-if="
            row.state == 1 &&
            (userStore.user.username == row.alarmManager ||
              userStore.user.username == row.manager)
          "
          type="primary"
          link
          size="small"
          @click="handle(row, 2)"
          ><i-ep-edit />确认接收
        </el-button>
      </template>
    </avue-crud>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="900px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <shortage-detail :shortage="shortage" :dialog="dialog" />
    </el-dialog>
    <el-dialog
      v-model="dialog2.visible"
      :title="dialog2.title"
      width="900px"
      @close="dialog2.visible = false"
      :destroy-on-close="true"
    >
      <shortage-view :shortage="shortage" :dialog="dialog" />
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import {
  useCommonStoreHook,
  useDictionaryStore,
  useUserStoreHook,
} from "@/store";
import ShortageDetail from "@/views/plan/shortage/components/shortage-detail.vue";
import { handleShortage } from "@/api/order/index";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const { dicts } = useDictionaryStore();
const userStore = useUserStoreHook();
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const shortage = ref({});
const handleEdit = (row, index) => {
  dialog.visible = true;
  shortage.value = row;
};
const viewData = (row, index) => {
  dialog2.visible = true;
  shortage.value = row;
};
const dialog = reactive({
  title: "编辑",
  visible: false,
});

const dialog2 = reactive({
  title: "缺料明细",
  visible: false,
});
const drawingDetail = ref(null);
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/planShortage",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
// checkBtnPerm(ButtonPermKeys.PLAN.BTNS.order_add) :permission="permission"
// const permission = reactive({
//   delBtn: checkPerm(buttonPermission.PLAN.BTNS.order_del),
//   addBtn: checkPerm(buttonPermission.PLAN.BTNS.order_add),
//   editBtn: checkPerm(buttonPermission.PLAN.BTNS.order_edit),
//   menu: true,
// });

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const router = useRouter();
const binding = (row) => {
  router.push(`/base/bom/${row.materialCode}`);
};
onMounted(() => {
  // 设置表格列或者其他自定义的option

  dataList();
});

/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataList();
};
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/base/material/import");
  }
};

const importBomExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/base/materialBom/bom/import");
  }
};
const handleData = ref({});
const handle = (row, state) => {
  handleData.value.id = row.id;
  handleData.value.state = state;
  handleShortage(handleData.value).then((data) => {
    if (data.code == "200") {
      dataList();
      ElMessage.success("操作成功");
    } else {
      ElMessage.error(data.message);
    }
  });
};
option.value = Object.assign(option.value, {
  addBtn: false,
  viewBtn: false,
  editBtn: false,
  menuWidth: 240,
  column: [
    {
      label: "订单号",
      prop: "orderCode",
      overHidden: true,
      search: true,
    },
    {
      label: "工单号",
      prop: "workOrderCode",
      overHidden: true,
      search: true,
    },
    {
      label: "物料编码",
      prop: "materialCode",
      overHidden: true,
      search: true,
    },
    {
      label: "物料名称",
      prop: "materialName",
      overHidden: true,
      search: true,
      rules: [
        {
          required: true,
          message: "请填写物料名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "物料规格",
      prop: "materialModel",
      overHidden: true,
    },
    {
      label: "生产数量",
      prop: "workOrderNum",
      width: 120,
    },
    {
      label: "处理人",
      prop: "handleUser",
      width: 120,
    },
    {
      label: "处理时间",
      prop: "handleTime",
      format: "YYYY-MM-DD HH:mm:ss",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      width: 120,
    },

    {
      label: "状态",
      prop: "state",
      type: "select",
      dicData: [
        {
          label: "待处理",
          value: 0,
        },
        {
          label: "已转发上级",
          value: 1,
        },
        {
          label: "确认接收",
          value: 2,
        },
      ],
    },
  ],
});
</script>
