<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="800px"
      @confirm="handleSubmit"
      @close="handleClose"
      :button="mode == 'edit'"
      :clickModalClose="mode == 'details'"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="120px"
      >
        <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="task.title">
              <el-input readonly v-model="formData.task.title" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务金额" prop="task.money">
              <el-input readonly v-model="formData.task.money" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属公司" prop="company_name">
              <el-input readonly v-model="formData.company_name" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务描述" prop="task.content">
              <el-input
                readonly
                v-model="formData.task.content"
                clearable
                type="textarea"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务提交时间" prop="id">
              <el-input readonly v-model="formData.task.update_time" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="片区经理" prop="flow_cate">
              <el-input readonly v-model="formData.area_manager" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider />
        <el-row>
          <el-col :span="24">
            <el-form-item label="详情描述" prop="remark">
              <el-input
                readonly
                :value="formData.task?.extend.other.note"
                clearable
                type="textarea"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片附件">
              <el-image
                class="attachment"
                v-for="(item, index) in formData.task.extend.other.annex"
                :key="index"
                :src="item"
                :preview-src-list="formData.task.extend.other.annex"
                :initial-index="index"
                fit="cover"
              ></el-image>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="视频附件">
              <video
                class="video"
                :src="item"
                v-for="(item, index) in formData.task.extend.other.video_annex"
                :key="index"
                controls
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider />
        <!-- <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="check_status"
            clearable
            :style="{ width: '100%' }"
          >
            <el-radio-group
              :disabled="mode == 'details'"
              v-model="formData.check_status"
            >
              <el-radio :label="2">通过</el-radio>
              <el-radio :label="3">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col>
            <el-form-item label="备注" prop="remark">
              <el-input
                :readonly="mode == 'details'"
                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 { apiApproveAudit } 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: "",
  task: {},
  company_name: "",
  area_manager: "",
  check_status: 1,
  remark: "",
});
// 表单验证
const formRules = reactive<any>({
  check_status: [
    {
      required: true,
      validator: (rules: any, value: any, callback: any) => {
        if (value != 2 && value != 3) callback("请选择审核状态");
        else callback();
      },
      trigger: ["blur"],
    },
  ],
  // remark: [
  //   {
  //     required: false,
  //     validator: (rules: any, value: any, callback: any) => {
  //       if (formData.check_status == 3) callback("请填写备注");
  //       else callback();
  //     },
  //     trigger: ["blur"],
  //   },
  // ],
});
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] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  // const data = await apiFlowDetail({
  //     id: row.id,
  // });
  // setFormData(data);
};
// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  if (formData.check_status == 3 && formData.remark == "")
    return ElMessage.error("驳回必须填写备注");
  await apiApproveAudit({
    id: formData.id,
    check_status: formData.check_status,
    remark: formData.remark,
  });
  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;
}
.attachment {
  width: 150px;
  height: 150px;
  /* background-color: red; */
  margin-right: 10px;
  margin-bottom: 10px;
}
.video {
  width: 300px;
  height: 150px;
  /* background-color: red; */
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>