<template>
  <div class="processApproval">
    <div class="left-card">
      <div class="top">
        <div class="commons-title title">
          {{ route.query.flowName || "流程标题（发起）" }}
        </div>
        <SendSubscribe
          ref="makeDom"
          v-if="flowForm.flowKey == 'subscribe_flow'"
        ></SendSubscribe>
        <SendPurchase
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'purchase_flow'"
          :queryData="queryData.data"
        ></SendPurchase>
        <SendFunds
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'account_request_funds_flow'"
          :queryData="queryData.data"
        ></SendFunds>
        <ReturnGood
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'sales_return_flow'"
          :queryData="queryData.data"
        ></ReturnGood>
        <PurchaseRefund
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'refund_flow'"
          :queryData="queryData.data"
        ></PurchaseRefund>
        <PurchasePayment
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'pay_flow'"
          :queryData="queryData.data"
        ></PurchasePayment>
        <PriceSheet
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'sale_quotation_flow'"
          :queryData="queryData.data"
        ></PriceSheet>
        <Contract
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'contract_flow'"
          :queryData="queryData.data"
        ></Contract>
        <ServiceContract
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'service_contract_flow'"
          :queryData="queryData.data"
        ></ServiceContract>
        <!-- 维多利亚 -->
        <SendSubscribeWDLY
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'wdly_apply_purchase'"
        ></SendSubscribeWDLY>
        <SendPurchaseWDLY
          ref="makeDom"
          v-else-if="flowForm.flowKey == 'wdly_purchase'"
          :queryData="queryData.data"
        ></SendPurchaseWDLY>
      </div>
      <div class="bottom">
        <div class="commons-title title">处理意见</div>
        <el-form :model="flowForm" :rules="flowRules" ref="flowFormDom">
          <el-form-item prop="remark" label-width="0px" label="">
            <el-input
              type="textarea"
              placeholder="请输入"
              v-model="flowForm.remark"
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSubmit">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="right-card">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane label="审批记录" name="first">
          <ul class="flow-chart">
            <li v-for="item in recordList" :key="item.id">
              <div class="left-icon">
                <i class="iconfont icon-iconm_daick"></i>
                <i class="iconfont icon-icomx_quertj1 right-btm-status"></i>
              </div>
              <div class="right-conetnt">
                <div class="name">发起人:{{ item.processedUser }}</div>
                <div class="remark">
                  <div class="label">{{ item.nodeName }}</div>
                  {{ item.remark }}
                </div>
              </div>
              <div class="line"></div>
            </li>
          </ul>
        </el-tab-pane>

        <!-- <el-tab-pane label="决策辅助" name="second">
          <auxiliary></auxiliary>
        </el-tab-pane> -->
      </el-tabs>
    </div>

    <el-dialog title="下一处理人" width="400" v-model="dialogVisible">
      <el-form :model="flowForm">
        <el-form-item prop="remark" label="处理人">
          <el-select
            v-model="flowForm.handleUserId"
            placeholder="请选择"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in nextHandleUser"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div style="width: 100%; text-align: center">
            <el-button type="primary" @click="handleSelectUser">提交</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name='ProcessApproval'>
import useTagsViewStore from "@/store/modules/tagsView.js";
import { useRouter, useRoute } from "vue-router";
//决策辅助
import auxiliary from "./auxiliary";
//申购发起
import SendSubscribe from "@/components/process/SendSubscribe";
//采购发起
import SendPurchase from "@/components/process/SendPurchase";
//请款发起
import SendFunds from "@/components/process/SendFunds";
//退货
import ReturnGood from "@/components/process/ReturnGood";
// 消息提示
import { ElMessage, ElMessageBox } from "element-plus";
//退款
import PurchaseRefund from "@/components/process/PurchaseRefund";
// 付款
import PurchasePayment from "@/components/process/PurchasePayment";
// 报价单
import PriceSheet from "@/components/process/PriceSheet";
// 销售合同
import Contract from "@/components/process/Contract";
// 服务合同
import ServiceContract from "@/components/process/ServiceContract";
// /``````````````````````````````维多利亚专属 /
//申购发起
import SendSubscribeWDLY from "@/components/WDLY/process/SendSubscribeWDLY";
//采购发起
import SendPurchaseWDLY from "@/components/WDLY/process/SendPurchaseWDLY";

const router = useRouter();
const route = useRoute();
// tab切换逻辑
const activeName = ref("first");
const handleClick = (tab, event) => {
  // console.log(tab, event);
};
// 意见表单
const flowForm = reactive({
  flowKey: "",
  handleUserId: "",
  remark: "",
  data: {},
});
const flowRules = reactive({
  // remark: [{ required: true, message: "请输入处理意见", trigger: "blur" }],
});
//组件实例
const { proxy } = getCurrentInstance();
const makeDom = ref(null);
const flowFormDom = ref(null);
let dialogVisible = ref(false);
const nextHandleUser = ref([]);
const handleSelectUser = () => {
  if (!flowForm.handleUserId) {
    return ElMessage({
      message: "请选择下一节点处理人！",
      type: "info",
    });
  }
  handleSubmit();
};
const handleResult = (res) => {
  if (res !== null && res.success) {
    skipPage();
  } else {
    dialogVisible.value = true;
    nextHandleUser.value = res.userList;
  }
};
// 提交逻辑
const handleSubmit = async () => {
  try {
    // 调用发起组件的提交事件
    const flag = await makeDom.value.handleSubmit();
    if (flag) {
      flowFormDom.value.validate((valid) => {
        if (valid) {
          const data = { ...makeDom.value.submitData };
          if (flowForm.flowKey == "subscribe_flow") {
            data.subscribeDetailList = data.subscribeDetailList.map((x) => ({
              bussinessId: x.bussinessId,
              count: x.count,
              remark: x.remark,
            }));
          } else if (flowForm.flowKey == "purchase_flow") {
            data.purchaseDetailList = data.purchaseDetailList.map((x) => ({
              bussinessId: x.bussinessId,
              subscribeDetailId: x.id,
              count: x.count,
              price: x.price,
              amount: x.amount,
            }));
          } else if (flowForm.flowKey == "account_request_funds_flow") {
          } else if (flowForm.flowKey == "sales_return_flow") {
          } else if (flowForm.flowKey == "refund_flow") {
          } else if (flowForm.flowKey == "wdly_apply_purchase") {
            data.subscribeDetailList = data.subscribeDetailList.map((x) => ({
              bussinessId: x.bussinessId,
              count: x.count,
              remark: x.remark,
            }));
            const victoriatouristJson = {
              planArrivalTime: data.planArrivalTime,
              receiptWarehouseId: data.receiptWarehouseId,
            };
            data.victoriatouristJson = JSON.stringify(victoriatouristJson);
          } else if (flowForm.flowKey == "wdly_purchase") {
            data.purchaseDetailList = data.purchaseDetailList.map((x) => ({
              bussinessId: x.bussinessId,
              subscribeDetailId: x.id,
              count: x.count,
              price: x.price,
              amount: x.amount,
            }));
            const victoriatouristJson = {
              isAgreement: data.isAgreement,
              paymentMethod: data.paymentMethod,
              otherFeeList: data.otherFeeList,
              contractCode: data.contractCode,
            };
            data.victoriatouristJson = JSON.stringify(victoriatouristJson);
          }
          proxy
            .post("/flowProcess/initiate", {
              ...flowForm,
              data,
            })
            .then((res) => {
              handleResult(res);
            });
        }
      });
    }
  } catch (err) {
    console.log("数据未填完整！", err);
  }
};
// 页面跳转
const skipPage = () => {
  const useTagsStore = useTagsViewStore();
  useTagsStore.delVisitedView(router.currentRoute.value);
  ElMessage({
    message: "操作成功！",
    type: "success",
  });
  if (flowForm.flowKey == "subscribe_flow") {
    router.replace({
      path: "/purchaseManage/purchaseManage/subscribe",
    });
  } else if (flowForm.flowKey == "purchase_flow") {
    router.replace({
      path: "/purchaseManage/purchaseManage/purchase",
    });
  } else if (flowForm.flowKey == "sales_return_flow") {
    router.replace({
      path: "/purchaseManage/purchaseManage/returnGoods",
    });
  } else if (flowForm.flowKey == "account_request_funds_flow") {
    router.replace({
      path: "/finance/fundManage/funds",
    });
  } else if (flowForm.flowKey == "refund_flow") {
    router.replace({
      path: "/purchaseManage/purchasePayment/invoice",
    });
  } else if (flowForm.flowKey == "pay_flow") {
    router.replace({
      path: "/purchaseManage/purchasePayment/payment",
    });
  } else if (flowForm.flowKey == "sale_quotation_flow") {
    router.replace({
      path: "/salesMange/saleContract/priceSheet",
    });
  } else if (flowForm.flowKey == "contract_flow") {
    router.replace({
      path: "/salesMange/saleContract/contract",
    });
  } else if (flowForm.flowKey == "service_contract_flow") {
    router.replace({
      path: "/salesMange/saleContract/serviceContract",
    });
  } else if (flowForm.flowKey == "wdly_purchase") {
    router.replace({
      path: "/WDLY/purchaseManage/alreadyPurchase_wdly",
    });
  } else if (flowForm.flowKey == "wdly_apply_purchase") {
    router.replace({
      path: "/WDLY/purchaseManage/subscribe_wdly",
    });
  }
};
let queryData = reactive({
  data: {},
});
// 记录
const recordList = ref([]);
const getRecords = () => {
  proxy
    .post("/flowExample/getFlowNode", {
      flowKey: flowForm.flowKey,
    })
    .then((res) => {
      console.log(res, "ass");
      recordList.value = res;
    });
};
onMounted(() => {
  queryData.data = { ...route.query };
  flowForm.flowKey = route.query.flowKey;
  getRecords();
});
</script>

<style lang="scss" scoped>
.processApproval {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  padding: 0 20px;
  height: calc(100vh - 130px);
  .left-card {
    // background: #fff;
    border-radius: 4px;
    // padding: 20px;
    flex: 1;
    margin-right: 20px;
    display: flex;
    flex-direction: column;
    .top {
      flex: 1;
      overflow-y: auto;
      background: #fff;
      padding: 20px 20px 0px 20px;
    }
    .bottom {
      margin-top: 10px;
      height: 170px;
      background: #fff;
      padding: 20px 20px 0px 20px;
    }
  }
  .right-card {
    background: #fff;
    border-radius: 4px;
    padding: 0 20px 20px;
    width: 600px;
    box-sizing: border-box;
    .flow-chart {
      overflow: auto;
      padding: 0;
      margin: 0;
      li {
        margin: 0;
        padding: 0 0 20px;
        list-style: none;
        display: flex;
        justify-content: space-between;
        position: relative;
        .right-conetnt {
          flex: 1;
          .name {
            font-size: 12px;
            color: #333;
            margin-bottom: 10px;
            span {
              color: #999;
              float: right;
            }
          }
          .remark {
            padding: 10px;
            color: #666666;
            font-size: 12px;
            background: #f1f1f1;
            border-radius: 2px;
            .label {
              color: #39c55a;
              margin-bottom: 10px;
            }
          }
        }
        .left-icon {
          width: 40px;
          height: 40px;
          text-align: center;
          line-height: 40px;
          background: #0084ff;
          border-radius: 10px;
          color: #fff;
          font-size: 20px;
          position: relative;
          margin-right: 27px;
          z-index: 2;
          .right-btm-status {
            position: absolute;
            bottom: 0px;
            right: -10px;
            height: 20px;
            width: 20px;
            line-height: 16px;
            border-radius: 10px;
            background: #39c55a;
            border: 2px solid #fff;
            font-size: 12px;
            box-sizing: border-box;
          }
        }
      }
      li::before {
        content: "";
        position: absolute;
        top: 0;
        left: 20px;
        width: 2px;
        height: 100%;
        background: #ddd;
        z-index: 1;
      }
      li:last-child::before {
        display: none;
      }
    }
  }
}
</style>
