<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="800px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="120px"
      >
        <el-col>
          <el-row>
            <el-col :span="12">
              <el-form-item label="ID" prop="id">
                <el-input readonly v-model="formData.id" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务名称" prop="name">
                <el-input readonly v-model="formData.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务金额" prop="flow_cate">
                <el-input readonly v-model="formData.flow_cate" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属公司" prop="name">
                <el-input readonly v-model="formData.name" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="任务描述" prop="remark">
                <el-input
                  readonly
                  v-model="formData.remark"
                  clearable
                  type="textarea"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="任务完成时间" prop="id">
                <el-input readonly v-model="formData.id" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="片区经理" prop="flow_cate">
                <el-input readonly v-model="formData.flow_cate" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-form-item label="">
            <el-timeline>
              <el-timeline-item
                v-for="(activity, index) in activities"
                :key="index"
                :type="activity.type"
                :hollow="activity.hollow"
                :timestamp="activity.timestamp"
              >
                {{ activity.content }}
              </el-timeline-item>
            </el-timeline>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="审核"
            prop="flow_detail.check_type"
            clearable
            :style="{ width: '100%' }"
          >
            <el-radio-group v-model="formData.flow_detail.type">
              <el-radio :label="1">通过</el-radio>
              <el-radio :label="2">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" clearable type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </popup>
  </div>
</template>
  
  <script lang="ts" setup name="flowEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { apiFlowCreat, apiFlowDetil } from "@/api/examined";
import { timeFormat } from "@/utils/util";
import type { PropType } from "vue";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const isCompany = ref(false);
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == "edit" ? "审核" : "新增";
});

const activities = ref([
  {
    content: "任务完成",
    timestamp: "2023-8-28 13:30:00",
  },
  {
    content: "张三三审核",
    timestamp: "2023-8-28 13:30:00",
  },
  {
    content: "李思思审核",
    timestamp: "2023-8-28 13:30:00",
    type: "primary",
    hollow: true,
  },
]);

// 表单数据
const formData = reactive({
  id: "",
  name: "",
  check_type: "",
  type: "",
  flow_cate: "",
  remark: "",
  flow_detail: {
    type: 1,
    step: [{ type: "", user: [{ id: "", name: "" }] }],
  },
  copy_to: [{ id: "", name: "" }],
});
// 表单验证
const formRules = reactive<any>({});
function customEvent(data: any) {
  isCompany.value = false;
}
// 获取详情
const setFormData = async (data: Record<any, any>) => {
  let res = await apiFlowDetil(data.id);
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = res[key];
    }
  }
  formData.flow_detail.type = Number(formData.flow_detail.type);
};

const getDetail = async (row: Record<string, any>) => {
  // const data = await apiFlowDetail({
  //     id: row.id,
  // });
  // setFormData(data);
};
// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  mode.value == "edit"
    ? await apiFlowCreat(formData)
    : await apiFlowCreat(formData);
  popupRef.value?.close();
  emit("success");
};

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>

<style lang="scss" scoped>
:deep(.el-timeline-item__wrapper) {
  top: -8px !important;
}
</style>