<template>
  <div slot="title">
    <div class="task_state">
      <div
        class="task_urgent"
        v-if="item.urgentStateValue"
        :style="{
          background: urgentStateColor(item)
            ? urgentStateColor(item)
            : item.urgentStateValue == 1
            ? '#e55555'
            : item.urgentStateValue == 2
            ? '#FFAE44'
            : '#409eff',
        }"
      >
        <i class="icon-jinjizhuangtai"></i>
      </div>
      <div
        v-if="
          isWait
            ? statusDotClassName[item.status]
            : doneStatusDotClassName[item.status]
        "
        :class="
          isWait
            ? statusDotClassName[item.status]
            : doneStatusDotClassName[item.status]
        "
        class="task_status"
        type="success"
      >
        <span v-if="isWait">
          {{ statusArray[item.status] }}
        </span>
        <span v-else>
          {{ doneStatusArray[item.status] }}
        </span>
      </div>
      <div class="task_list_name" v-html="hightLight(searchValue,item.subject)">
      </div>
    </div>
    <div class="task_list_word">
      <div class="task_list_flowname">
        <div class="task_list_flex">
          <div class="task_list_time">
            <i class="icon-time-circle"></i>
            {{
              ['leaderWait', 'wait'].includes(currentActiveTab)
                ? item.createTime
                : item.completeTime
            }}
          </div>
          <div class="task_list_procDefName" style="margin-left: 20px">
            <i class="icon-liucheng"></i>
            {{ item.procDefName }}
          </div>
        </div>
        <div>
          <i class="icon-liuchengbianhao"></i>
          <span v-html="hightLight(searchValue, ['leaderWait', 'wait'].includes(currentActiveTab)
              ? item.procInstId
              : item.id)"></span>
          <i
            v-if="['leaderWait', 'wait'].includes(currentActiveTab)"
            class="icon-fuzhi"
            style="color: #409eff"
            @click.stop="(event) => clipboard(item.procInstId, event)"
          ></i>
          <i
            v-else
            class="icon-fuzhi"
            style="color: #409eff"
            @click.stop="(event) => clipboard(item.id, event)"
          ></i>
        </div>
      </div>
      <div class="task_list_user_box">
        <div class="task_list_user">
          <div :class="!item.imgSrc ? 'left-img-box' : 'left-img-box-image'">
            <img
              v-if="item.imgSrc"
              class="image"
              :src="item.imgSrc"
            />
            <span v-else class="text">
              {{ item.creator.replace(/,/g, '/').substr(0, 1) }}
            </span>
          </div>
          <span>{{ item.creator }}</span>
        </div>
        <div class="task_list_duration" v-if="isWait && !item.deadline">
          <i class="icon-tingliushijian"></i>
          已停留{{ item.createTime | countDate }}
        </div>
        <div
          class="task_list_deadline"
          v-if="isWait && item.deadline"
          :class="currentDateClassName(item.deadline)"
        >
          <i class="icon-tingliushijian"></i>
          {{ calcExpireTime(item.deadline) }}
        </div>
        <div class="task_list_procDefName" v-if="currentActiveTab == 'down'">
          <span v-if="item.status == 'end'" class="task_list_status_end">
            <i class="icon-wancheng1"></i>
            已归档
          </span>
          <span
            v-else-if="item.status == 'manualend'"
            class="task_list_status_manualend"
          >
            <i class="icon-jieshu"></i>
            人工结束
          </span>
          <div v-else>
            <div
              class="task_list_procDefName"
              v-if="extraField[item.id] && extraField[item.id].nodeName"
            >
              <i class="icon-dangqianjiedian"></i>
              <span
                v-for="(name, index) in extraField[item.id].nodeName"
                :key="`node-name_${item.id}_${index}`"
                class="flowlist-node-name"
              >
                {{ name }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import {
    STATUS,
    STATUS_DOT_CLASS_NAME_MAP,
    DONE_STATUS,
    DONE_STATUS_DOT_CLASS_NAME_MAP,
  } from './const'
  import clipboard from '@/utils/clipboard'
  import hightLight from '@/mixins/hightLight.js'
  import moment from 'moment'

  export default {
    name: 'TaskListItem',
    components: {},
    mixins: [hightLight],
    props: ['item', 'currentActiveTab', 'extraField','searchValue'],
    data() {
      return {
        statusArray: STATUS,
        statusDotClassName: STATUS_DOT_CLASS_NAME_MAP,
        doneStatusArray: DONE_STATUS,
        doneStatusDotClassName: DONE_STATUS_DOT_CLASS_NAME_MAP,
      }
    },
    computed: {
      isWait() {
        return (
          this.currentActiveTab == 'wait' ||
          this.currentActiveTab == 'leaderWait'
        )
      },
    },
    methods: {
      urgentStateColor(item) {
        let color = ''
        let urgentStateConf = item.urgentStateConf
        if (item.urgentStateValue && urgentStateConf) {
          try {
            urgentStateConf = JSON.parse(item.urgentStateConf)
          } catch (error) {
            urgentStateConf = eval('(' + item.urgentStateConf + ')')
          }

          const currentEmergency = urgentStateConf.find(
            (it) => it.state === item.urgentStateValue
          )
          color = currentEmergency && currentEmergency.color
        }
        return color
      },
      clipboard,
      calcExpireTime(deadline) {
        if (deadline) {
          const duration = moment(deadline).diff(new Date())
          if (duration > 0) return deadline
          const day = moment(new Date()).diff(deadline, 'day')
          const hour = moment(new Date()).diff(deadline, 'hours') % 24
          const minute = moment(new Date()).diff(deadline, 'minutes') % 60
          var val = '已到期'
          if (day != 0) {
            val += day + '天'
          }
          if (hour != 0) {
            val += hour + '小时'
          }
          if (minute != 0) {
            val += minute + '分钟'
          }
          return val
        }
      },
      currentDateClassName(deadline) {
        const duration = moment(deadline).diff(new Date())
        if (duration > 0) {
          return 'green-color'
        } else {
          return 'red-color'
        }
      },
    },
  }
</script>
<style lang="scss" scoped>
  .task_state {
    display: flex;
    align-items: center;
    justify-self: start;
    white-space: nowrap;
    .task_urgent {
      border-radius: 5px;
      padding: 0 5px;
      height: 18px;
      line-height: 18px;
      transform: skew(345deg, 0deg);
      margin-right: 8px;
      .icon-jinjizhuangtai {
        font-size: $base-font-size-big;
        color: #fff;
        transform: skew(-345deg, 0deg);
      }
    }
    .task_status {
      margin-right: 8px;
      margin-left: 4px;
      color: $base-white-color;
      font-size: 10px;
      border-radius: 5px;
      padding: 0 5px;
      transform: skew(345deg, 0deg);
      height: 18px;
      line-height: 18px;
      span {
        display: block;
        transform: skew(-345deg, 0deg);
      }
    }
    .task_list_name {
      word-break: break-all;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      font-size: $base-font-size-big;
      font-weight: bold;
      color: $base-text-color;
    }
  }
  .task_list_word {
    font-size: $base-font-size-small;
    i {
      font-size: $base-font-size-big;
    }
    .task_list_flowname {
      padding: 7px 0;
      color: $base-gray-text-color;
      border-bottom: 1px solid $base-gray-line-color;
      .task_list_flex {
        display: flex;
        .icon-time-circle,.icon-liucheng{
          font-size: 16px
        }
      }
    }
    .task_list_user_box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      white-space: nowrap;
      padding-top: 12px;
      color: $base-text-color;
      .task_list_duration,
      .task_list_deadline {
        word-break: break-all;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .task_list_user {
        display: flex;
        align-items: center;
        .left-img-box {
          width: 30px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          border-radius: 50%;
          margin-right: 4px;
          background: $--color-primary;
          .text {
            color: $base-white-color;
          }
          .span {
            color: $base-text-color;
          }
          .image {
            border-radius: 50%;
          }
        }

        .left-img-box-image {
          width: 30px;
          height: 30px;
          margin-right: 4px;
          .image {
            border-radius: 50%;
            width: 30px;
            height: 30px;
          }
        }
      }
    }
    .task_list_procDefName {
      word-break: break-all;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      max-width: 140px;
      color: #262626;
      .task_list_status_end,
      .task_list_status_manualend {
        color: #8c8c8c;
      }
    }
  }
  .task_list_flowname {
    .task_list_procDefName {
      color: #8c8c8c;
    }
  }
</style>
