<template>
  <div class="app-container">
    <!-- 标题按钮部分开始 -->
    <div class="tab-header webtop">
      <!-- 标题 -->
      <div>外采申请表详情</div>
      <!-- start 详情按钮 -->
      <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>
      <!-- end 详情按钮 -->
    </div>
    <!-- 标题按钮部分结束 -->
    <div>
      <div class="titwu">外采申请表</div>
      <el-form ref="form_obj" :model="formobj" label-position="right" class="formadd">
        <div class="wlInfo" style="text-align: right;"><span style="font-size:14px;">金额单位：元</span></div>
        <el-row style="border-top: 1px solid #e0e3eb">
          <el-col :span="3" class="tleftb">分公司名称</el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.useOrgName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">申请人姓名</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.createByName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">申请日期</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.applicationDate }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">车型名称</el-col>
          <el-col :span="21">
            <el-form-item><span>{{ formobj.modelName }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">常用配置</el-col>
          <el-col :span="21" class="trightb tlineheightb">
            <el-form-item><span>{{ formobj.configName }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">
            <span>内部编码</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.insideCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">销售指导价</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.guidedPrice }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">厂家合同价</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.manufactorSettlementPrice }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">外采供应商</el-col>
          <el-col :span="13">
            <el-form-item>
              <span>{{ formobj.purchasingUnitName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">是否先付款</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.isBeforePayment === '1' ? '是' : '否' }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">
            <span>采购价格</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.purchasePrice }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>台数</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.num }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>订金</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.deposit }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">车款账户名称</el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.accountName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>开户行</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.depositBankName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>银行账号</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.bankAccount }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formobj.isBeforePayment === '0'">
          <el-col :span="3" class="tleftb">订金账户名称</el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.modelAccountName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>开户行</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.modelBankName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">
            <span>银行账号</span>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <span>{{ formobj.modelBankAccount }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">其他费用名称</el-col>
          <el-col :span="13">
            <el-form-item><span>{{ formobj.expenseName }}</span></el-form-item>
          </el-col>
          <el-col :span="3" class="tleftb">费用金额合计</el-col>
          <el-col :span="5">
            <el-form-item><span>{{ formobj.totalExpenseAmount }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">
            <span>备注</span>
          </el-col>
          <el-col :span="21">
            <el-form-item>
              <span>{{ formobj.remarks }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3" class="tleftb">
            <span>合同附件</span>
          </el-col>
          <el-col :span="21">
            <el-form-item>
              <span v-if="formobj.contract != null" class="bluezi" @click="handleDown(formobj.contract)">{{ formobj.contract.fileName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="listcon" v-show="formobj.externalMiningTypeKey==2">
          <div style="border:1px solid #c0c0c0;padding:5px 5px 5px 5px;">
            车辆列表
          </div>
          <el-table :data="formobj.baseOutsourcingApplicationVehicleDtos" border style="width: 100%">‘
            <el-table-column width="60px" label="序号" type="index" align="center"/>
            <el-table-column prop="vinNo" label="车架号" align="center"/>
          </el-table>
        </div>
      </el-form>
    </div>
    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisible" width="80%">
      <el-form label-position="right" class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="4" class="tleftb">当前环节</el-col>
          <el-col :span="20">
            <el-form-item><span>{{ formobj.taskName }}->{{ nextNode.name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4" class="tleftb">意见</el-col>
          <el-col :span="20">
            <el-form-item>
              <el-input v-model="formobj.comment" placeholder="审批意见" class="addinputw" 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="agree">确 定</el-button>
          <el-button type="info " size="mini" @click="closeNodeDialog">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisibleForReject" width="80%">
      <el-form label-position="right" class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="4" class="tleftb">当前环节</el-col>
          <el-col :span="20">
            <el-form-item><span>{{ formobj.taskName }}->{{ nextNode.name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4" class="tleftb">意见</el-col>
          <el-col :span="20">
            <el-form-item>
              <el-input v-model="formobj.comment" placeholder="审批意见" class="addinputw" 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="closeNodeDialog">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisibleForstop" width="80%">
      <el-form label-position="right" class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="4" class="tleftb">意见</el-col>
          <el-col :span="20">
            <el-form-item>
              <el-input v-model="formobj.comment" placeholder="审批意见" class="addinputw" 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="handleStop()">确 定</el-button>
          <el-button type="info " size="mini" @click="closeNodeDialog">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/baseoutsourcingapplication/baseoutsourcingapplication'
import { getStorage } from '@/utils/auth'

export default {
  name: 'BaseOutsourcingApplicationInfo',
  components: {},
  data() {
    return {
      accessToken: {
        token: getStorage()
      },
      obj: {},
      // 角色sid
      roleSid: '',
      // 选择待办人弹框
      nodeDialogVisible: false,
      nodeDialogVisibleForReject: false,
      nodeDialogVisibleForstop: false,
      // 选择用户弹框
      nodeUserDialogVisible: false,
      // 模型xml数据
      xmlData: '',
      // 根据角色查询的用户数组
      users: [],
      // 选择的用户的姓名
      userName: '',
      // 选择的用户sid
      userSid: '',
      taskList: [],
      // 获取环节时默认是最后一个环节
      endTask: true,
      startTask: true,
      // 下一环节
      nextNode: {},
      formobj: {
        remarks: '',
        useOrgName: '',
        accountName: '',
        depositBankName: '',
        bankAccount: '',
        contract: {},
        sid: '',
        insideCode: '',
        comment: '',
        applicationCode: '123456', // 申请编号
        createByName: window.sessionStorage.getItem('name'), // 申请人姓名
        applicationDate: '', // 申请日期
        externalMiningTypeKey: '1', // 外采类型key
        externalMiningTypeValue: '', // 外采类型value
        purchasingUnitName: '', // 采购单位名称
        modelSid: '', // 车型sid
        modelName: '', // 车型名称
        configName: '', // 常用配置名称
        configSid: '', // 常用配置sid
        purchasePrice: '', // 采购价格
        guidedPrice: '', // 销售指导价
        manufactorSettlementPrice: '', // 厂家合同价
        num: '0', // 台数
        deposit: '', // 订金
        expenseName: '', // 费用名称
        totalExpenseAmount: '', // 费用金额合计
        orgSid: '', // 部门sid
        orgSidPath: window.sessionStorage.getItem('orgSidPath'),
        baseOutsourcingApplicationVehicleDtos: [],
        businessSid: '', // 同意办理列表
        instanceId: '',
        taskId: '',
        taskDefKey: '',
        taskName: '',
        nextNodeSid: '',
        nextUserSid: '',
        userSid: window.sessionStorage.getItem('userSid')
      },
      regectList: { // 驳回列表
        businessSid: '',
        comment: '',
        instanceId: '',
        taskId: '',
        userSid: window.sessionStorage.getItem('userSid')
      },
      stopList: { // 终止列表
        businessSid: '',
        comment: '',
        instanceId: '',
        taskId: '',
        userSid: window.sessionStorage.getItem('userSid')
      }
    }
  },
  created() {
    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.obj = obj
    // ===获取sid
    this.showInfo(obj.businessSid)
    // ===获取参数
    // 同意列表  父级页面传递过来的流程的相关的属性参数
    this.formobj.businessSid = obj.businessSid
    this.formobj.instanceId = obj.instanceId
    this.formobj.taskId = obj.taskId
    this.formobj.taskDefKey = obj.taskDefKey
    this.formobj.taskName = obj.taskName
    // 驳回列表
    this.regectList.businessSid = obj.businessSid
    this.regectList.instanceId = obj.instanceId
    this.regectList.taskId = obj.taskId
    // 终止列表
    this.stopList.businessSid = obj.businessSid
    this.stopList.instanceId = obj.instanceId
    this.stopList.taskId = obj.taskId
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 550 + 'px'
      }
    }, '*')
  },
  methods: {
    // 下载附件
    handleDown(row) {
      console.log(row)
      var xhr = new XMLHttpRequest()
      xhr.open('GET', process.env.VUE_APP_BASE_API + '/base/file/download?filePath=' + row.filePath + '&outFileName=' + row.name, true)
      xhr.setRequestHeader('token', getStorage())
      xhr.responseType = 'blob'
      xhr.onload = function(e) {
        // 如果请求执行成功
        var blob = this.response
        var filename = row.fileName;
        var a = document.createElement('a')
        // blob.type="application/octet-stream";
        // 创键临时url对象
        var url = URL.createObjectURL(blob)
        a.href = url
        a.download = filename
        a.click()
        // 释放之前创建的URL对象
        window.URL.revokeObjectURL(url)
      }
      // 发送请求
      xhr.send()
    },
    // 关闭弹出框 选择待办人弹框
    closeNodeDialog() {
      this.nodeDialogVisible = false
      this.nodeDialogVisibleForReject = false
      this.nodeDialogVisibleForstop = false
    },

    // 终止
    openStop() {
      this.nodeDialogVisibleForstop = true
    },
    // 驳回
    openReject() {
      this.formobj.comment = ''
      req.getPreviousNodesForReject(this.formobj).then(resp => {
        if (resp.success) {
          var arr = resp.data
          // 循环获取当前环节的下一个环节信息
          var startTask = arr[0].startTask
          if (startTask) {
            alert('当前环节不能驳回!')
            return
          }
          this.nextNode = arr[0]
          this.nodeDialogVisibleForReject = true
        }
      })
    },
    // 同意
    openAgree() {
      this.$set(this.formobj, 'comment', '同意')
      // this.formobj.comment = "同意"
      // 查验下一环节要用到，流程定义的id
      req.getNextNodesForSubmit(this.formobj).then(resp => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
      })
    },
    handleReturn() {
      this.$emit('doback')
    },
    showInfo(sid) {
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.fetchBySid(sid).then(resp => {
        if (resp.success) {
          const vdata = resp.data
          this.formobj.insideCode = vdata.insideCode
          this.formobj.contract = vdata.contract
          this.formobj.remarks = vdata.remarks
          this.formobj.useOrgName = vdata.useOrgName
          this.formobj.accountName = vdata.accountName
          this.formobj.depositBankName = vdata.depositBankName
          this.formobj.bankAccount = vdata.bankAccount
          this.formobj.modelAccountName = vdata.modelAccountName
          this.formobj.isBeforePayment = vdata.isBeforePayment
          this.formobj.modelBankName = vdata.modelBankName
          this.formobj.modelBankAccount = vdata.modelBankAccount
          this.formobj.applicationCode = vdata.applicationCode // 申请编号
          this.formobj.createByName = vdata.createByName // 申请人姓名
          this.formobj.applicationDate = vdata.applicationDate // 申请日期
          this.formobj.externalMiningTypeKey = vdata.externalMiningTypeKey // 外采类型key
          this.formobj.externalMiningTypeValue = vdata.externalMiningTypeValue // 外采类型value
          this.formobj.purchasingUnitSid = vdata.purchasingUnitSid // 采购单位sid
          this.formobj.purchasingUnitName = vdata.purchasingUnitName // 采购单位名称
          this.formobj.modelSid = vdata.modelSid // 车型sid
          this.formobj.modelName = vdata.modelName // 车型名称
          this.formobj.configName = vdata.configName // 常用配置名称
          this.formobj.purchasePrice = vdata.purchasePrice // 采购价格
          this.formobj.guidedPrice = vdata.guidedPrice // 销售指导价
          this.formobj.manufactorSettlementPrice = vdata.manufactorSettlementPrice // 厂家合同价
          this.formobj.num = vdata.num // 台数
          this.formobj.deposit = vdata.deposit // 订金
          this.formobj.expenseName = vdata.expenseName // 费用名称
          this.formobj.totalExpenseAmount = vdata.totalExpenseAmount // 费用金额合计
          this.formobj.orgSid = vdata.orgSid // 部门sid
          this.formobj.baseOutsourcingApplicationVehicleDtos = vdata.baseOutsourcingApplicationVehicleVos
        }
      }).catch(e => {
        this.formobj = {}
      })
    },
    reject() {
      if (this.formobj.comment === '') {
        alert('请填写审批意见!')
        return false
      }
      this.handleReject()
    },
    // 同意任务
    agree() {
      if (this.formobj.comment === '') {
        alert('请填写审批意见!')
        return false
      }
      // 意见
      // 下一环节的用户sid
      // 调用后台接口流转
      req.agreeTask(this.formobj).then((response) => {
        if (response.code === '200') {
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    /** 驳回任务 */
    handleReject() {
      this.regectList.comment = this.formobj.comment
      req.rejectTask(this.regectList).then((response) => {
        if (response.code === '200') {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              code: 1
            }
          }, '*')
          // this.refreshIt()
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    /** 终止任务 */
    handleStop() {
      if (this.formobj.comment === '') {
        alert('请填写审批意见!')
        return false
      }
      this.stopList.comment = this.formobj.comment
      req.breakTask(this.stopList).then((response) => {
        if (response.code === '200') {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              code: 1,
              data: document.body.scrollHeight + 'px'
            }
          }, '*')
          // this.refreshIt()
        } else {
          this.$notify({
            title: '提示',
            message: '执行失败',
            type: 'error',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.titwu {
  font-size: 28px;
  text-align: center;
  padding: 30px 0 20px 0;
}
.tlineheightb {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
/deep/ .tlineheightb .el-form-item .el-form-item__content {
  line-height: 15px !important;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
</style>
