
<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="`共${auditList.length}条反馈`" extra=""></SectionTitle>

    <el-timeline v-if="auditList.length > 0" style="margin-top: 20px;">
      <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 { getDateStr } from "@/utils/util";

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.getList();
    },
    closeVisible() {
      this.dialogVisible = false;
    },
    getList() {
      this.auditList = [];
      if (!this.info.id) {
        return;
      }
      this.loading = true;
      listFeedback({ 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: ''
            });
          }
        } else {
          if (!lastItem || lastItem.status === '-1') {
            list.push({
              taskId: this.info.taskId,
              taskBranchId: this.info.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.auditList = list;
      }).catch(err => {
        this.loading = false;
      });
    },
    auditChange() {
      this.isAuditChange = true;
      this.getList();
    },
    itemDate(item) {
      if (item.status === '0' || item.status === '1') {
        return item.feedbackTime;
      }
      return item.checkDate;
    }
  }
}
</script>

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