<template>
  <div class="mainContentBox">
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="待处理" name="no"/>
      <el-tab-pane label="审核完成" name="ok"/>
    </el-tabs>
    <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"
      v-if="!examineStatus"
    >
      <template #menu="{ row }">
        <el-button  link  @click="showSeq(row.seqNoList)">
          查看管号
        </el-button>
        <el-button type="danger" link v-if="row.state == '0' || row.state == '2' || row.state == '-1'"  @click="deleteRecord(row)"
        >删除</el-button>
        <el-button
            link
            icon="el-icon-copy-document"
            v-if="row.state == '0' || row.state == '2' || row.state == '-1'"
            @click="openCheckView(row)"
        >提审</el-button >

        <el-button link v-if="row.state !== '0'" icon="el-icon-copy-document" @click="showFlowSteps(row)"
        >流程</el-button>

        <el-button
            link
            icon="el-icon-copy-document"
            v-if="row.state == '3'"
            @click="onCancelFlow(row)"
        >撤销</el-button
        >
        <el-button  link @click="toShow(row)">
          查看
        </el-button>

      </template>
    </avue-crud>
    <div v-else>
      <Process :key="proccessKey" @get-list="getList" />
    </div>
    <ShowVue v-model="showState" :tableData="tableData" />
<el-dialog
        v-model="dialog2.visible"
        :title="dialog2.title"
        width="950px"
        @close="dialog2.visible = false"
    >
      <el-card
          style="cursor: pointer; font-size: 20px"
          shadow="always"
          :key="item"
          v-for="item in showSeqList"
      >{{ item }}</el-card
      >
    </el-dialog><LookFlowStep ref="LookFlowStepRef"></LookFlowStep>
    <work-flow-check
        ref="workFlowCheckRef"
        @sureToSave="onWFSave"
    ></work-flow-check>  </div>
</template>
<script setup>
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import SingleUpload from "@/components/Upload/SingleUpload.vue";
import Process from "./components/process.vue";
import ShowVue from "./components/show.vue";
import WorkFlowCheck from "@/components/WorkFlows/workFlowCheck.vue";
import LookFlowStep from "@/views/flow/common/LookFlowStep.vue";
import { cancelFlow,submitFlow } from "@/api/flow";
import {ref} from "vue";
const { dicts } = useDictionaryStore();
const showState = ref(false);
const toShow = (row) => {
  tableData.value = setJson(row);
  showState.value = true;
};

const deleteRecord = (row, index, done) => {
  deleteRow(row, index, done);
  dataList();
};
const showSeqList =ref([]);
const tableData = ref();
const proccessKey = ref(false);
const examineStatus = ref(false);
const formData = ref({});
provide("formData", formData);
provide("examineStatus", examineStatus);
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/error",
  });
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 workFlowCheckRef = ref(null);
const openCheckView = (row) => {
  row.businessId = row.id
  workFlowCheckRef.value && workFlowCheckRef.value.openDialog(row, "removeService");
};
const activeName = ref("no");
onMounted(() => {
  search.value.type = "eliminate";
  search.value.isOk = "0";
  dataList();
});
watch(
  () => activeName.value,
  (val) => {
    if (val == "ok") {
      search.value.isOk = "1";
    } else {
      search.value.isOk = "0";
    }
    dataList();
  }
);

const onWFSave = (flowForm) => {
  submitFlow(flowForm).then(() => {
    ElMessage.success("提交成功");
    workFlowCheckRef.value && workFlowCheckRef.value.close();
    dataList();
  });
};

const onCancelFlow=(row)=>{
  cancelFlow(row).then(() => {
    ElMessage.success("提交成功");
    dataList();
  });
}
const LookFlowStepRef=ref(null);
const showFlowSteps = (row) => {
  LookFlowStepRef.value && LookFlowStepRef.value.openFlowStepDrawer(row.flowIns);
};
const getList = () => {
  dataList();
};
const dialog2 = reactive({
  title: "管号列表",
  visible: false,
});
const showSeq = (seqs) => {
  showSeqList.value = seqs;
  dialog2.visible = true;
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  addBtn: false,
  delBtn: false,
  editBtn: false,
  viewBtn: false,
  column: [
   {
      label: "生产批号",
      prop: "workOrderCode",
    },
    {
      label: "产品名称",
      prop: "materialName",
      search: true,
    },
    {
      label: "产品型号",
      prop: "materialModel",
      search: true,
    },
    {
      label: "阶段",
      prop: "stage",
    },

    {
      label: "状态",
      prop: "state",
      search: true,
      type: "select",
      dicData: [
        {
          label: "待处理",
          value: "0",
        },
        {
          label: "审核中",
          value: "3",
        },
        {
          label: "审核驳回",
          value: "-1",
        },
        {
          label: "审核完成",
          value: "1",
        },
        {
          label: "撤回",
          value: "2",
        },
      ],
    },
      {
          label: "问题描述",
          prop: "remark1",
          overHidden: true,
          formatter: (val) => {
              if(val.remark1){
                  return JSON.parse(val.remark1).content;
              }

          }
      },
      {
      label: "备注",
      prop: "remark",
    },

    {
      label: "创建人",
      prop: "creator",
      editDisplay: false,
    },
      {
          label: "创建时间",
          prop: "created",
          overHidden: true,
          width:160
      },

  ],
});
const setJson = (row1) => {
  const row = JSON.parse(JSON.stringify(row1));
  //处理初始值
  let obj = {
    content: "",
    user1: "",
    user2: "",
    user3: "",
    user4: "",
    user5: "",
    user6: "",
    user7: "",
    time: "",
    state: "-1",
  };
  if (row.remark1 == "") {
    row.remark1 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark1)).length == 0) {
      row.remark1 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark1 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark1),
      };
    }
  }
  if (row.remark2 == "") {
    row.remark2 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark2)).length == 0) {
      row.remark2 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark2 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark2),
      };
    }
  }
  if (row.remark3 == "") {
    row.remark3 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark3)).length == 0) {
      row.remark3 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark3 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark3),
      };
    }
  }
  if (row.remark4 == "") {
    row.remark4 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark4)).length == 0) {
      row.remark4 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark4 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark4),
      };
    }
  }
  if (row.remark5 == "") {
    row.remark5 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark5)).length == 0) {
      row.remark5 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark5 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark5),
      };
    }
  }
  if (row.remark6 == "") {
    row.remark6 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark6)).length == 0) {
      row.remark6 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark6 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark6),
      };
    }
  }
  if (row.remark7 == "") {
    row.remark7 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark7)).length == 0) {
      row.remark7 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark7 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark7),
      };
    }
  }
  return row;
};
const toExamine = (row) => {
  formData.value = setJson(row);
  proccessKey.value = !proccessKey.value;
  examineStatus.value = true;
};
</script>
