<template>
  <div class="task-sub">
    <div class="subtasks-content"
         v-clickoutside="subtasksSubmit">
      <div class="checkbox-box subtasks-box">
        <el-checkbox disabled
                     v-model="checkboxData"></el-checkbox>
      </div>
      <div class="subtasks-content-box">
        <el-input type="textarea"
                  autosize
                  :maxlength="50"
                  placeholder="请输入内容"
                  @keyup.enter.native="subtasksSubmit"
                  v-model="subtasksTextarea">
        </el-input>
      </div>
      <flexbox class="subtasks-content-png">
        <div class="time">
          <el-date-picker v-model="subtasksDate"
                          type="date"
                          @change="subtasksDateClick(subtasksDate)"
                          ref="subtasksDate"
                          format="yyyy 年 MM 月 dd 日"
                          value-format="yyyy-MM-dd"
                          :style="{'width': subtasksDataText ? '54px': '28px'}"
                          placeholder="选择日期">
          </el-date-picker>
          <span class="bg-color"
                v-if="subtasksDataText && subtasksDataText != 0">{{subtasksDataText | moment("MM-DD")}}</span>
          <i v-else
             class="wukong wukong-time-task"
             @click="subtasksDateFun"></i>
        </div>
        <!-- 选择负责人 -->
        <el-popover placement="bottom-end"
                    width="280"
                    trigger="click">
          <xh-user ref="xhuser"
                   radio
                   :selectedData="xhUserData"
                   @changeCheckout="xhUserCheckout">
          </xh-user>
          <div class="select-box"
               slot="reference">
            <template v-if="subtaskChange">
              <span v-for="(item, index) in xhUserData"
                    :key="index">
                <el-tooltip placement="bottom"
                            effect="light"
                            popper-class="tooltip-change-border">
                  <div slot="content">
                    <span>{{item.realname}}</span>
                  </div>
                  <div v-photo="item"
                       v-lazy:background-image="$options.filters.filterUserLazyImg(item.thumb_img)"
                       :key="item.thumb_img"
                       class="div-photo"></div>
                </el-tooltip>
              </span>
            </template>
            <i v-else
               class="wukong wukong-user"></i>
          </div>
        </el-popover>
      </flexbox>
    </div>
  </div>
</template>

<script>
import { addTask, editTask } from '@/api/oamanagement/task'
import XhUser from '@/components/CreateCom/XhUser'

export default {
  components: {
    XhUser
  },
  data() {
    return {
      // 子任务选择时间
      subtasksDate: '',
      // 子任务数据
      xhUserData: [],
      subtasksDataText: null,
      // 子任务人员
      subtaskChange: false,
      num: 0,
      subtasksTextarea: '',
      isNum: 0
    }
  },
  created() {
    if (this.subTaskCom == 'edit') {
      this.subtasksTextarea = this.text ? this.text : ''
      this.subtasksDataText = this.time
      if (JSON.stringify(this.subData) !== '{}') {
        if (this.subData.main_user_id) {
          this.subData.id = this.subData.main_user_id
          this.subtaskChange = true
          this.xhUserData = [this.subData]
        }
      } else {
        this.xhUserData = []
        this.subtaskChange = false
      }
    }
  },
  props: {
    taskData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // 子任务输入内容
    text: String,
    time: [String, Number],
    subData: Object,
    subTaskCom: {
      type: String,
      default: ''
    },
    taskId: [Number, String],
    checkboxData: {
      type: Boolean,
      default: false
    }
  },
  watch: {},
  methods: {
    // 子任务 -- 选中时间
    subtasksDateClick(val) {
      this.subtasksDataText = val.substring(5)
    },
    subtasksSubmit(event) {
      this.subtasksTextarea = this.subtasksTextarea.split(/[\n]/).join('')
      if (this.subtasksTextarea && this.num == 0) {
        this.num = 1
        if (this.subTaskCom == 'new') {
          let timeData = this.subtasksDate
            ? new Date(this.subtasksDate).getTime() / 1000
            : ''
          this.$emit('on-handle', { type: 'add', result: 'success' })
          addTask({
            pid: this.taskData.task_id,
            name: this.subtasksTextarea,
            stop_time: timeData,
            main_user_id:
              this.xhUserData.length != 0 ? this.xhUserData[0].id : ''
          })
            .then(res => {
              this.taskData.subTaskList.push({
                name: this.subtasksTextarea,
                stop_time: timeData,
                task_id: res.data,
                thumb_img:
                  this.xhUserData.length > 0
                    ? this.xhUserData[0].thumb_img
                    : '',
                main_user_id:
                  this.xhUserData.length > 0 ? this.xhUserData[0].id : '',
                realname:
                  this.xhUserData.length > 0 ? this.xhUserData[0].realname : ''
              })
              this.$message.success('子任务创建成功')
              // 创建成功 -- 清除选择
              this.subtasksTextarea = ''
              this.subtasksDataText = null
              this.subtaskChange = false
              this.num = 0
            })
            .catch(err => {
              this.$emit('on-handle', { type: 'add', result: 'error' })
              this.$message.error('子任务创建失败')
              this.num = 0
            })
        } else if (this.subTaskCom == 'edit') {
          let ids = this.xhUserData.length > 0 ? this.xhUserData[0].id : ''

          if (
            this.isNum == 1 ||
            this.text != this.subtasksTextarea ||
            this.subtasksDataText != this.time
          ) {
            let timeData = this.subtasksDate
              ? new Date(this.subtasksDate).getTime() / 1000
              : this.time
            this.$emit('on-handle', { type: 'edit', result: 'success' })
            editTask({
              task_id: this.taskId,
              stop_time: timeData ? timeData : '',
              main_user_id: ids,
              name: this.subtasksTextarea
            })
              .then(res => {
                let dataList = this.taskData.subTaskList
                for (let i in dataList) {
                  if (dataList[i].task_id == this.taskId) {
                    let list = dataList[i]
                    list.name = this.subtasksTextarea
                    list.stop_time = timeData ? timeData : ''
                    list.thumb_img =
                      this.xhUserData.length > 0
                        ? this.xhUserData[0].thumb_img
                        : ''
                    list.main_user_id =
                      this.xhUserData.length > 0 ? this.xhUserData[0].id : ''
                    list.realname =
                      this.xhUserData.length > 0
                        ? this.xhUserData[0].realname
                        : ''
                    dataList.splice(i, 1, list)
                    break
                  }
                }
                this.num = 0
                this.$message.success('子任务编辑成功')
              })
              .catch(err => {
                this.$emit('on-handle', { type: 'edit', result: 'error' })
                this.$message.error('子任务编辑失败')
                this.num = 0
              })
          } else {
            this.$emit('on-handle', { type: 'cancel' })
          }
        }
      } else {
        this.$emit('on-handle', { type: 'cancel' })
      }
    },
    xhUserCheckout(data) {
      if (data.data.length != 0) {
        this.subtaskChange = true
        if (this.xhUserData.length != 0) {
          this.$set(this.xhUserData[0], 'thumb_img', data.data[0].thumb_img)
          this.$set(this.xhUserData[0], 'realname', data.data[0].realname)
        } else {
          this.xhUserData = data.data
        }
        this.isNum = 1
      } else {
        this.isNum = 0
        this.subtaskChange = false
      }
    },
    // 子任务 -- 时间弹框
    subtasksDateFun() {
      this.$refs.subtasksDate.change()
    }
  }
}
</script>

<style scoped lang="scss">
.wukong {
  color: #666;
}
.subtasks-content {
  border: 1px solid #3e84e9;
  border-radius: 3px;
  display: flex;
  flex-direction: row;
  align-items: center;
  // margin-top: 10px;
  height: 40px;
  .subtasks-box {
    line-height: 40px;
  }
  .subtasks-content-box {
    flex: 1;
    .el-textarea /deep/ .el-textarea__inner {
      border-width: 0;
      resize: none;
    }
  }
  .subtasks-content-png {
    width: auto;
    img {
      margin-right: 10px;
      cursor: pointer;
      vertical-align: middle;
    }
    .time {
      display: inline-block;
      position: relative;
      vertical-align: middle;
      .el-date-editor {
        position: absolute;
        right: 0;
        width: 28px;
        opacity: 0;
        bottom: 0;
        top: 0;
      }
      .el-date-editor /deep/ .el-input__inner {
        padding: 0;
        height: 100%;
      }
      .el-date-editor /deep/ .el-input__suffix {
        display: none;
      }
      .el-date-editor /deep/ .el-input__prefix {
        left: 0;
        right: 0;
      }
      .bg-color {
        padding: 2px 10px;
      }
      .wukong-time-task {
        margin-right: 10px;
        cursor: pointer;
        vertical-align: middle;
      }
    }
    .select-box {
      display: inline-block;
      .div-photo {
        width: 24px;
        height: 24px;
        border-radius: 12px;
      }
      .wukong-user {
        margin-right: 10px;
        cursor: pointer;
        vertical-align: middle;
      }
    }
  }
  .checkbox-box {
    margin-left: 10px;
  }
  .subtask-popover-span {
    padding: 0 10px 0 5px;
  }
}
</style>
