<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="{ size, row, index }">
        <el-button
          icon="el-icon-edit"
          text
          @click="openDialog(0, row.id, row.outsourceId)"
          type="primary"
          :size="size"
          >详情</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="1200px"
      @close="dialog.visible = false"
    >
      <el-form
        ref="queryFormRef"
        label-width="100"
        :inline="true"
        style="width: 100%"
      >
        <el-row :gutter="24">
          <el-col :lg="12" :xs="12">
            <el-form-item label="产品名称">
              <el-text>{{ form.materialName }}</el-text>
            </el-form-item>
          </el-col>

          <el-col :lg="12" :xs="12">
            <el-form-item label="产品编码">
              <el-text>{{ form.materialCode }}</el-text>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :lg="12" :xs="12">
            <el-form-item label="委外工序">
              <el-text>{{ form.operationName }}</el-text>
            </el-form-item>
          </el-col>

          <el-col :lg="12" :xs="12">
            <el-form-item label="产品数量">
              <el-text>{{ form.outNum }}</el-text>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :lg="24" :xs="24">
            <el-form-item label=" ">
              <el-table
                :border="true"
                class="gray-header-table"
                v-loading="loading"
                :data="form.details"
                highlight-current-row
              >
                <el-table-column
                  label="工卡编号"
                  align="left"
                  width="350"
                  prop="seqNo"
                />
                <el-table-column
                  label="状态"
                  width="250"
                  align="left"
                  prop="state"
                >
                  <template #default="scope">
                    <el-tag v-if="scope.row.state === 0" type="success"
                      >合格</el-tag
                    >
                    <el-tag v-if="scope.row.state === 1" type="error"
                      >不合格</el-tag
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" v-if="dialog.visible">
          <el-form-item label="Bom">
            <Boms :apply-id="bomId" />
          </el-form-item>
        </el-row>
      </el-form>

      <div
        class="dialog-footer"
        align="center"
        v-if="dialog.type === 1 && checkPerm('outsource:audit')"
      >
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="audit">审 核</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import Boms from "./boms.vue";
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { checkPerm } from "@/directive/permission";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { queryOutSourceDetails, updateOutSourceApply } from "@/api/process";
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;
};
const radio = ref(0);
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/process/tdmOrder",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const loading = ref(false);
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const dialog = reactive({
  title: "委外详情",
  visible: false,
  type: 0,
});
const bomId = ref(""); //打开详情需要传入applyid
const openDialog = (type, id, outId) => {
  bomId.value = id;
  dialog.title = type === 0 ? "委外详情" : "委外审核";
  queryOutSourceDetails(outId).then((data) => {
    form.value = data.data;
    radio.value = 0;
    if (form.value) {
      if (form.value.state === "2") {
        radio.value = 1;
      }
    }
    dialog.visible = true;
    dialog.type = type;
  });
};
const audit = () => {
  form.value.state = radio.value === 0 ? 1 : 2;
  updateOutSourceApply(form.value).then((data) => {
    if (data.code === "200") {
      ElMessage.success("操作成功");
      dialog.visible = false;
      dataList();
    } else {
      ElMessage.error(data.msg);
    }
  });
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  addBtn: false,
  editBtn: false,
  viewBtn: false,
  delBtn: false,
  selection: true,
  column: [
    {
      label: "计划编号",
      prop: "orderCode",
      width: 150,
      overHidden: true,
      search: true,
    },

    {
      label: "物料编码",
      prop: "materialCode",
      search: true,
      width: 150,
      overHidden: true,
    },
    {
      label: "物料名称",
      width: 150,
      overHidden: true,
      prop: "materialName",
    },
    // {
    //   label: "工单数量",
    //   width: 120,
    //   overHidden: true,
    //   prop: "workOrderNum",
    // },

    {
      label: "委外工序",
      width: 150,
      overHidden: true,
      prop: "operationName",
    },

    {
      label: "状态",
      prop: "state",
      type: "select",
      search: true,
      width: 90,
      dicUrl: dictDataUtil.request_url + dictDataUtil.TYPE_CODE.outsource_state,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "申请人",
      width: 120,
      overHidden: true,
      prop: "creator",
    },
    {
      label: "申请时间",
      width: 120,
      overHidden: true,
      prop: "created",
    },
    {
      label: "审核人",
      width: 120,
      overHidden: true,
      prop: "auditUser",
    },
    {
      label: "审核时间",
      width: 120,
      overHidden: true,
      prop: "auditTime",
    },
    {
      label: "接收人",
      width: 120,
      overHidden: true,
      prop: "receiveUser",
    },
    {
      label: "接收时间",
      width: 120,
      overHidden: true,
      prop: "receiveTime",
    },
  ],
});

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
</script>
<style>
/* 添加自定义类名以区分不同表格的样式 */
.gray-header-table .el-table__header-wrapper {
  background-color: #f2f2f2; /* 灰色背景 */
}
</style>
