<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="dialogVisible"
    :title="`任务${isAudit ? '审核':'反馈'}`"
    width="80%"
  >
    <div style="height: 70vh;width:100%;overflow-y: scroll;overflow-x: hidden;"  v-loading="loading">
      <SectionTitle title="基本信息" desc="" extra=""></SectionTitle>
      <Detail :info="info" style="margin-top: 10px;"></Detail>
      <SectionTitle title="反馈信息" icon="el-icon-s-comment" :desc="`共${info.ctTaskTags.length}个标签任务`" extra="" style="margin-bottom: 20px;"></SectionTitle>
      <el-card v-for="(taskTag, i) in info.ctTaskTags" :key="taskTag.id">
        <div slot="header" class="clearfix">
          <span>标签{{ i + 1 }}：{{taskTag.tagName}}</span>
        </div>
        <el-timeline v-if="taskTag._auditList.length > 0">
          <el-timeline-item v-for="(item, index) in taskTag._auditList" :key="getKey(item, index)" :timestamp="itemDate(item)" placement="top">
            <el-card>
              <AuditItem :info="info" :tag="taskTag" :item="item" @refresh="auditChange(taskTag)"></AuditItem>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-card>
      <el-timeline v-if="auditList.length > 0">
        <el-timeline-item v-for="(item, index) in auditList" :key="getKey(item, index)" :timestamp="itemDate(item)" placement="top">
          <el-card>
            <AuditItem :info="info" :item="item" @refresh="auditChange()"></AuditItem>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closeVisible">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SectionTitle from "@/views/task-distribut/components/SectionTitle.vue";
import Detail from './Detail.vue';
import AuditItem from './AuditItem.vue';
import { listFeedback } from "@/api/task/feedback";
import moment from'moment';

export default {
  name: 'AuditDialog',
  components: {
    SectionTitle,
    Detail,
    AuditItem,
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      info: {},
      auditList: [],
      isAuditChange: false,
    };
  },
  watch: {
    dialogVisible(val) {
      if (!val && this.isAuditChange) {
        this.$emit('refresh');
      }
    }
  },
  methods: {
    getKey(item, index) {
      return `${item.id}_${index}`;
    },
    open(data = {}) {
      this.isAuditChange = false;
      this.dialogVisible = true;
      this.info = { ...data };

      this.info.ctTaskTags?.forEach((val) => {
        this.getList(val);
      });
      this.getAuditFeedback();
    },
    closeVisible() {
      this.dialogVisible = false;
    },
    getAuditFeedback() {
      if (!this.info.id) {
        return;
      }
      this.loading = true;
      listFeedback({ taskTagId: -1, taskId: this.info.taskId, taskBranchId: this.info.id, isPublish: this.isAudit ? '1':'' }).then(res => {
        this.loading = false;
        const list = res.data || [];

        // 办结的不用反馈和审核
        if (this.info.sdTaskOther?.status === '2') {
          this.auditList = list;
          return;
        }
        const currentDate = new Date();
        const lastItem = list.length > 0 ? list[list.length - 1] : null;
        if (this.isAudit) {
          if (!lastItem || lastItem.status === '1') {
            list.push({
              taskId: this.info.taskId,
              taskBranchId: this.info.id,
              branchCode: this.info.branchCode,
              checkUserId: this.$store.getters.userInfo.userId,
              checkUserName: this.$store.getters.userInfo.userName,
              checkUserDept: this.$store.getters.userInfo.dept.deptName,
              checkDate: currentDate,
              checkInfo: ''
            });
          }
        }
        this.auditList = list;

      }).catch(err => {
        this.loading = false;
      });
    },
    getList(taskTag) {
      if (!this.info.id) {
        return;
      }
      this.loading = true;
      listFeedback({ taskTagId: taskTag.id, taskId: this.info.taskId, taskBranchId: this.info.id, isPublish: this.isAudit ? '1':'' }).then(res => {
        this.loading = false;
        const list = res.data || [];

        // 办结的不用反馈和审核
        if (this.info.sdTaskOther?.status === '2') {
          this.$set(taskTag, '_auditList', list);
          return;
        }
        const currentDate = new Date();
        const lastItem = list.length > 0 ? list[list.length - 1] : null;
        if (this.isAudit) {
          // if (!lastItem || lastItem.status === '1') {
          //   list.push({
          //     taskId: this.info.taskId,
          //     taskBranchId: this.info.id,
          //     branchCode: this.info.branchCode,
          //     checkUserId: this.$store.getters.userInfo.userId,
          //     checkUserName: this.$store.getters.userInfo.userName,
          //     checkUserDept: this.$store.getters.userInfo.dept.deptName,
          //     checkDate: currentDate,
          //     checkInfo: ''
          //   });
          // }
        } else {
          if (!lastItem || lastItem.status === '-1') {
            list.push({
              taskId: this.info.taskId,
              taskBranchId: this.info.id,
              taskTagId: taskTag.id,
              branchCode: this.info.branchCode,
              userId: this.$store.getters.userInfo.userId,
              userName: this.$store.getters.userInfo.userName,
              userDept: this.$store.getters.userInfo.dept.deptName,
              feedbackTime: currentDate,
              taskDesc: '',
              status: '0',
            });
          }
        }
        this.$set(taskTag, '_auditList', list);
      }).catch(err => {
        this.loading = false;
      });
    },
    auditChange(taskTag) {
      this.isAuditChange = true;
      if (taskTag) {
        this.getList(taskTag);
      } else {
        this.getAuditFeedback();
      }
    },
    itemDate(item) {
      let date = null;
      if (item.status === '0' || item.status === '1') {
        date = item.feedbackTime;
      } else {
        date = item.checkDate;
      }
      if (date) {
        return moment(date).format('YYYY-MM-DD'); // 使用大写的 YYYY-MM-DD
      }
      return '';
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
