<template>
  <el-drawer
    :title="title"
    :modal-append-to-body="false"
    :modal="false"
    :wrapper-closable="false"
    size="99%"
    :visible.sync="dialogVisible"
  >
    <div class="content-container">
      <vuescroll :ops="ops" style="height: calc(100vh - 300px)">
        <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="210px">
          <el-form-item label="标题" prop="exampleTitle">
            <el-input v-model="formData.exampleTitle" />
          </el-form-item>

          <el-form-item label="描述" prop="exampleDesc">
            <el-input v-model="formData.exampleDesc" type="textarea" :rows="4" placeholder="描述" />
          </el-form-item>

          <el-form-item label="附件" prop="attachList">
            <FileUpload
              :is-multiple="true"
              :file-type="1"
              :delault-file-list="formData.attachList"
              @fileListChange="attchListChange"
            />
          </el-form-item>

          <p style="color: #FFFFFF">下一关：评审任务</p>

          <el-form-item label="执行人" prop="accountIdTo">
            <user-selector :default-val="formData.accountIdTo" @setUserInfo="handleSelectUser" />
          </el-form-item>

          <el-form-item label="说明" prop="actionRemark">
            <el-input v-model="formData.actionRemark" type="textarea" :rows="4" placeholder="说明" />
          </el-form-item>
        </el-form>
      </vuescroll>
      <el-row>
        <el-button
          v-for="(item,index) in flowData.taskIns.actionList"
          :key="index"
          type="primary"
          @click="submitForm('ruleForm', item.actionId, item.actionCode)"
        >{{ item.actionTitle }} </el-button>
        <el-button class="cancel-btn" @click="dialogVisible = false">取 消</el-button>
      </el-row>
    </div>

  </el-drawer>
</template>

<script>
import { startWfExample, submitWfExample, getWfExampleById } from '@/api/system/wfExampleApi'
import FileUpload from '@/components/FileUpload/index'
import UserSelector from '@/components/UserSelector/index'
import Vuescroll from 'vuescroll'

export default {
  name: 'SubmitTask',
  components: { Vuescroll, FileUpload, UserSelector },
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ops: {
        bar: {
          keepShow: false,
          background: 'rgba(144, 147, 153, 0.4)',
          onlyShowBarOnScroll: false
        }
      },
      dialogVisible: false,

      formData: {
        exampleId: undefined,
        exampleTitle: '', // 预警标题
        exampleDesc: '', // 描述

        wfDefId: undefined,
        wfInsId: undefined,
        taskDefId: undefined,
        taskInsId: undefined,
        taskCode: '',
        actionId: undefined,
        actionCode: '',

        groupIdTo: undefined,
        positionIdTo: undefined,
        accountIdTo: undefined, // 执行人

        actionRemark: '', // 说明
        attachList: []
      },

      flowData: {
        taskIns: {
          actionList: []
        }
      },

      rules: {
        exampleTitle: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        groupId: [
          { required: true, message: '请输入所在部门', trigger: 'blur' }
        ],
        accountIdTo: [
          { required: true, message: '请选择执行人', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
  },
  methods: {

    // 附件
    attchListChange(dataList) {
      this.formData.attachList = dataList
    },

    // Task Start View
    start() {
      this.resetFormData()
      this.dialogVisible = true

      startWfExample().then((resp) => {
        const { code, data, msg } = resp
        if (code === 0) {
          this.flowData = data
          this.formData.wfDefId = data.wfDefId
          this.formData.wfInsId = data.wfInsId
          this.formData.taskDefId = data.taskIns.taskDefId
          this.formData.taskInsId = data.taskIns.taskInsId
          this.formData.taskCode = data.taskIns.taskCode
        } else {
          this.$message.error(msg)
        }
      }).catch((error) => {
        console.log(error)
      })
    },

    // Task Handling View
    handle(exampleId) {
      this.resetFormData()
      this.dialogVisible = true
      getWfExampleById(exampleId).then((resp) => {
        const { code, data, msg } = resp
        if (code === 0) {
          this.formData = data.data
          const flow = data.flow
          this.flowData = flow
          this.formData.wfDefId = flow.wfDefId
          this.formData.wfInsId = flow.wfInsId
          this.formData.taskDefId = flow.taskIns.taskDefId
          this.formData.taskInsId = flow.taskIns.taskInsId
          this.formData.taskCode = flow.taskIns.taskCode
        } else {
          this.$message.error(msg)
        }
      }).catch((error) => {
        console.log(error)
      })
    },

    // Select User
    handleSelectUser(item) {
      this.formData.accountIdTo = item.accountId
      this.formData.groupIdTo = item.groupId
      this.formData.positionIdTo = item.positionId
    },

    // Reset Form Data
    resetFormData() {
      this.formData = {
        exampleId: undefined,
        exampleTitle: '', // 预警标题
        exampleDesc: '', // 描述
        actionRemark: '', // 说明
        accountIdTo: undefined, // 执行人
        groupIdTo: undefined, // 执行人班组
        positionIdTo: undefined // 执行人岗位
      }
    },

    // Submit Flow
    submitForm(formName, actionId, actionCode) {
      this.formData.actionId = actionId
      this.formData.actionCode = actionCode
      this.$refs[formName].validate((valid) => {
        if (valid) {
          submitWfExample(this.formData).then((resp) => {
            const { code, msg } = resp
            if (code === 0) {
              this.dialogVisible = false
              this.formSuccess()
              this.$message.success(msg)
            } else {
              this.$message.error(msg)
            }
          }).catch((error) => {
            console.log(error)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    formSuccess() {
      this.$emit('formSuccess')
    },

    resetForm(formName) {
      this.$refs[formName].resetFields()
    }

  }
}
</script>

<style lang="scss">
</style>
<style lang="scss" scoped>
.content-container {
  position: relative;
  height: calc(100% - 15px);
  .thisform {
    margin: 0 100px;
  }

  .btn-group {
    width: 100%;
    text-align: center;

    .el-button {
      margin: 0 15px;
    }

    .cancel-btn {
      background: #004F7B;
      border-color: #004F7B;
      color: #FFF;

      &:hover {
        background: #026197;
        border-color: #026197;
      }
    }
  }

}

</style>
