<template>
  <div class="add-ticket">
    <div class="breadcrumb">
      <bg-breadcrumb />
    </div>
    <div class="content">
      <div class="add-form-main bg-scroll">
        <add-form ref="add_form" :row="infoData"></add-form>
      </div>
      <div class="add-btns">
        <el-button size="default" @click="Cancle">取消</el-button>
        <el-button type="primary" size="default" @click="Save">保存</el-button>
        <el-button type="success" size="default" @click="Distribute">立即下发</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import addForm from "../modules/add-form.vue";
import axios from "@/request/http.js";
import { ElMessage } from "element-plus";
const router = useRouter();
const route = useRoute();
const { id } = route.query;
const infoData = ref({});
const Cancle = () => {
  router.go(-1);
};
const add_form = ref(null);
const Save = async () => {
  let res = await add_form.value.Submit();
  if (!res) return;
  Save(res, { url: "/v1/api/work_order/work_order_manage/edit" }, () => {
    Cancle();
  });
};
const Distribute = async () => {
  let res = await add_form.value.Submit();
  if (!res) return;
  Save(res, { id, url: "/v1/api/work_order/work_order_manage/edit" }, () => {
    Cancle();
  });
};
const getInfoData = () => {
  const params = {
    id,
  };
  axios.get("/v1/api/work_order/work_order_manage/details", { params }).then((res) => {
    if (res.data.code == 200) {
      let { data } = res.data;
      let ruleObj = {
        1: {},
        2: {
          week_days: data.timing_weekly?.week || [],
          time: data.timing_weekly?.point_time || "",
        },
        3: {
          lists: data.timing_custom?.map((e) => {
            return {
              date: [e.date_from, e.date_to],
              time: e.point_time,
            };
          }),
        },
      };
      infoData.value = {
        ticket_name: data.order_name,
        id: data.id,
        status: data.timing_state,
        // finish: 10,
        // all: 10,
        history: {
          method: data.push_obj.push_method == 3 ? ["1", "2"] : [`${data.push_obj.push_method}`],
          lists: data.push_obj.user_obj.map((e) => {
            return {
              user_id: e.system_account,
              user_name: e.user_name,
              phone: e.phone,
            };
          }),
        },
        ticket_level: data.order_level,
        ticket_desc: data.order_desc,
        timing_rules: +data.timing_type,
        rules: {
          ...ruleObj,
        },
      };
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(() => {
  getInfoData();
});
</script>

<style lang="scss" scoped>
.add-ticket {
  width: 100%;
  height: 100%;
  padding: 0 24px 16px;
  .breadcrumb {
    width: 100%;
    height: 46px;
  }
  .content {
    width: 100%;
    height: calc(100% - 46px);
    background-color: #ffffff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    .add-form-main {
      padding: 24px;
      height: calc(100% - 68px);
    }
    .add-btns {
      height: 68px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      border-top: 1px solid #ddd;
      padding: 0 24px;
    }
  }
}
</style>
