<template>
  <div class="app-container">
    <div>
      <div class="tab-header webtop">
        <div>{{ title }}</div>
        <div>
          <el-button type="primary" size="small" @click="openAgree('同意')">同 意</el-button>
          <!-- <el-button type="danger" size="small" @click="openReject('驳回')">驳 回</el-button> -->
          <el-button type="danger" size="small" @click="openStop('终止')">终 止</el-button>
        </div>
      </div>
      <div class="" style="padding-bottom: 50px;">
        <el-form ref="dataForm" :model="formobj" label-position="top" label-width="190px" class="formadd">
          <div class="titwu">生产经营用款</div>
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">项目名称</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <span>{{ formobj.projectName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">申请日期</span>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="trightb_item">
              <el-form-item class="trightb_item">
                <span>{{ formobj.createTime }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">用款名称</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <span>{{ formobj.enterpriseName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">申请人</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <span>{{ formobj.createByName }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">用款申请</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <span>{{ formobj.applyUse }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">用款金额</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <span>{{ formobj.money }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisible" width="80%">
      <el-form class="formadd" >
        <el-row v-show="currentLink" style="border-top: 1px solid #e0e3eb">
          <el-col :span="4" class="tleftb">
            <span>当前环节:</span>
          </el-col>
          <el-col :span="20">
            <el-form-item><span>{{ current.taskName }}->{{ nextNode.name }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row :class="{rowClass:!currentLink}">
          <el-col :span="4" class="tleftb">
            <span>意见:</span>
          </el-col>
          <el-col :span="20">
            <el-form-item><el-input size="small" v-model="dialogList.comment" placeholder="审批意见" class="addinputw" type="textarea" :autosize="{ minRows: 1, maxRows: 10}" clearable ></el-input></el-form-item>
          </el-col>
        </el-row>
        <div style="text-align:center;margin-top: 20px;">
          <el-button type="primary" size="mini" @click="reject">确 定</el-button>
          <el-button type="info " size="mini" @click="nodeDialogVisible = false">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/Yksq/yksq'

export default {
  name: 'ykDaiBan',
  data() {
    return {
      title: '',
      submitdisabled: false,
      formobj: {
        sid: '',
        userSid: '',
        createByName: '',
        projectName: '',
        projectSid: '',
        enterpriseName: '',
        enterpriseSid: '',
        applyUse: '',
        money: '',
        voucherUrlList: []
      },
      operation: '', // 点击操作按钮
      dialogList: {
        comment: ''
      },
      startTask: true,
      current: {
        taskDefKey: '',
        taskName: '' // 当前环节名称
      },
      nextNode: {}, // 下一环节
      nodeDialogVisible: false,
      currentLink: true,
      // 环节所需参数
      linkByParameter: {
        businessSid: '',
        comment: '',
        instanceId: '',
        taskId: '',
        orgSidPath: '',
        taskDefKey: '',
        userSid: ''
      }
    }
  },
  created() {
    console.log('url:' + window.location.href)
    // ===获取参数
    var one = window.location.href.indexOf('&data') + 6
    const data = window.location.href.substr(one) // url解码unescape()已从web中移除,尽量不使用
    const obj = JSON.parse(decodeURIComponent(data))
    // 点击（同意、终止、驳回、驳回）操作时所需的参数
    this.linkByParameter.businessSid = obj.businessSid
    this.linkByParameter.instanceId = obj.instanceId
    this.linkByParameter.taskId = obj.taskId
    this.linkByParameter.taskDefKey = obj.taskDefKey
    this.linkByParameter.orgSidPath = window.sessionStorage.getItem('orgSidPath')
    this.linkByParameter.userSid = window.sessionStorage.getItem('userSid')
    this.current.taskDefKey = obj.taskDefKey
    this.current.taskName = obj.taskName
    // 加载表单数据
    this.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 480 + 'px'
      }
    }, '*')
  },
  methods: {
    showInfo(sid) {
      this.title = '生产经营用款'
      req.fetchDetailsBySid({ sid: sid }).then((resp) => {
        if (resp.success) {
          this.formobj = resp.data
        }
      })
    },
    // 同意
    openAgree(val) {
      this.operation = val
      this.currentLink = true
      this.dialogList.comment = '同意'
      req.getNextNodesForSubmit({ taskDefKey: this.current.taskDefKey, businessSid: this.linkByParameter.businessSid }).then((resp) => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
        this.submitdisabled = false
      })
    },
    // 驳回
    openReject(val) {
      this.operation = val
      this.currentLink = true
      this.dialogList.comment = ''
      req.getPreviousNodesForReject({ taskDefKey: this.current.taskDefKey, businessSid: this.linkByParameter.businessSid }).then((resp) => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
        this.submitdisabled = false
      })
    },
    // 终止
    openStop(val) {
      this.operation = val
      this.dialogList.comment = ''
      this.currentLink = false
      this.nodeDialogVisible = true
    },
    reject() {
      if (this.operation === '同意') {
        this.handleAgree()
      } else if (this.operation === '驳回') {
        if (this.dialogList.comment === '') {
          this.$message({ showClose: true, type: 'error', message: '请填写审批意见' })
        } else {
          this.handleReject()
        }
      } else if (this.operation === '终止') {
        if (this.dialogList.comment === '') {
          this.$message({ showClose: true, type: 'error', message: '请填写审批意见' })
        } else {
          this.handleStop()
        }
      }
    },
    /** 同意任务 */
    handleAgree() {
      this.linkByParameter.comment = this.dialogList.comment
      req.complete(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    /** 驳回任务 */
    handleReject() {
      this.linkByParameter.comment = this.dialogList.comment
      req.reject(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    /** 终止任务 */
    handleStop() {
      this.linkByParameter.comment = this.dialogList.comment
      req.breakProcess(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.trightb {
  display: flex;
  align-items: center;
  justify-content: center;
}

.trightb_item {
  padding-top: 5px;
}

.span {
  margin-left: 50px;
  font-size: 15px;
  font-weight: 400;
}

.rowClass{
  border-top: 1px solid #E0E3EB;
}

.titwu {
  font-size: 28px;
  text-align: center;
  padding: 30px 0 20px 0;
}
</style>
