<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-left>
        <el-radio-group
          v-model="radio1"
          size="large"
          @change="searchChangeState"
        >
          <el-radio-button label="未完成" value="1" />
          <el-radio-button label="已完成" value="3" />
        </el-radio-group>
      </template>
      <template #menu="{ row, index, type }">
        <el-button
          @click="viewFault(row)"
          icon="el-icon-view"
          text
          type="primary"
          >查看</el-button
        >
        <el-button
          @click="faultEdit(row)"
          icon="el-icon-link"
          text
          type="primary"
          v-if="row.currentState == 6"
          v-hasPerm="[ButtonPermKeys.FAULT.BTNS.faultHandle_handle]"
          >处理</el-button
        >

        <el-button
          @click="faultThaw(row)"
          text
          type="primary"
          v-hasPerm="[ButtonPermKeys.FAULT.BTNS.faultHandle_thaw]"
          v-if="row.currentState == 4"
          >解冻
          <template #icon>
            <svg-icon icon-class="jiedong"></svg-icon>
          </template>
        </el-button>
        <el-button
          @click="faultExamine(row)"
          icon="el-icon-reading"
          text
          type="primary"
          v-hasPerm="[ButtonPermKeys.FAULT.BTNS.faultHandle_examine]"
          v-if="row.currentState == 1"
          >审核</el-button
        >
      </template>
    </avue-crud>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="900px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <fault-detail :escalationFaultId="fault.id" :dialog="dialog" />
    </el-dialog>

    <el-dialog
      v-model="dialog1.visible"
      :title="dialog1.title"
      width="900px"
      @close="closeFaultDetail"
      :destroy-on-close="true"
    >
      <fault-handle :escalationId="faultInfo.id" :dia="dialog1" />
    </el-dialog>

    <el-dialog
      v-model="dialog2.visible"
      :title="dialog2.title"
      width="900px"
      @close="closeFaultHandel"
      :destroy-on-close="true"
    >
      <fault-page
        :faultId="faultEs.id"
        :dial="dialog2"
        :workOrderCode="faultEs.workOrderCode"
      />
    </el-dialog>

    <el-dialog
      v-model="dialog3.visible"
      :title="dialog3.title"
      width="900px"
      @close="closeFault"
      :destroy-on-close="true"
    >
      <FaultExamine :escFaultId="faultExa.id" :dialogPs="dialog3" />
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import dictDataUtil from "@/common/configs/dictDataUtil";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import FaultExamine from "@/views/quality/faultHandle/components/fault-examine.vue";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
// 数据字典相关
const { dicts } = useDictionaryStore();

const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const dialog = ref({
  title: "报故详情",
  visible: false,
});
const dialog1 = ref({
  title: "报故处理",
  visible: false,
});
const dialog2 = ref({
  title: "解冻",
  visible: false,
});
const dialog3 = ref({
  title: "审核",
  visible: false,
});

const closeFaultDetail = () => {
  dialog1.visible = false;
  dataList();
};
const closeFaultHandel = () => {
  dialog2.visible = false;
  dataList();
};
const fault = ref({});
const faultInfo = ref({});
const faultEs = ref({});
const faultExa = ref({});
const faultEdit = (row) => {
  faultInfo.value.id = row.id;

  dialog1.value.visible = true;
};
const faultThaw = (row) => {
  faultEs.value = row;
  dialog2.value.visible = true;
};

const faultExamine = (row) => {
  faultExa.value.id = row.id;
  dialog3.value.visible = true;
};
const closeFault = () => {
  dialog3.value.visible = false;
  dataList();
};
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/process/escalationFault",
  });
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 route = useRoute();
const viewFault = (row) => {
  fault.value.id = row.id;
  console.info(fault);
  dialog.value.visible = true;
};
onMounted(() => {
  // console.log("crudRef", crudRef)

  search.value.id = route.query.id;
  search.value.state = radio1.value;
  dataList();
});

const searchChangeState = () => {
  search.value.state = radio1.value;
  dataList();
};

const radio1 = ref("1");

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  addBtn: false,
  editBtn: false,
  delBtn: false,
  viewBtn: false,
  column: [
    {
      label: "审理单号",
      prop: "trackingNumber",
      overHidden: true,
      search: true,
    },
    {
      label: "工单号",
      prop: "workOrderCode",
      overHidden: true,
      search: true,
    },
    {
      label: "订单编号",
      prop: "orderCode",
      overHidden: true,
      width: 120,
      search: true,
    },
    {
      label: "不合格数量",
      overHidden: true,
      width: 100,
      prop: "unqualifiedNum",
    },
    {
      label: "物料编码",
      prop: "materialCode",
      overHidden: true,
    },
    {
      label: "产品名称",
      prop: "materialName",
      overHidden: true,
      width: 100,
    },

    {
      label: "流转卡号",
      prop: "seqNo",
      overHidden: true,
      search: true,
    },
    {
      label: "状态",
      prop: "state",
      overHidden: true,
      width: 120,
      dicUrl:
        dictDataUtil.request_url +
        dictDataUtil.TYPE_CODE.escalation_fault_state,
      props: { label: "dictLabel", value: "dictValue" },
    },
    {
      label: "跟踪状态",
      prop: "currentState",
      overHidden: true,
      width: 100,
      dicUrl:
        dictDataUtil.request_url + dictDataUtil.TYPE_CODE.fault_current_state,
      props: { label: "dictLabel", value: "dictValue" },
    },
    {
      label: "报故人",
      prop: "creator",
      overHidden: true,
      width: 100,
    },
  ],
});
</script>
