<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="danger" size="small" @click="openRevoke()">撤回</el-button>
        </div>
      </div>
      <div class="">
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="8">
              <div class="span-sty">申请人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createByName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.dept }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">申请日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.applyDate }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <div class="span-sty">反结算原因</div>
              <el-form-item><span class="addinputInfo">{{ formobj.reason }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">备注</div>
              <el-form-item><span class="addinputInfo">{{ formobj.remarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title">维修单信息</div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">维修单编号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.sourceBillNo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">单据日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.sourceBillDate }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">服务顾问</div>
              <el-form-item><span class="addinputInfo">{{ formobj.waitorName }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">维修单类型</div>
              <el-form-item><span class="addinputInfo">{{ formobj.billType }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">科目</div>
              <el-form-item><span class="addinputInfo">{{ formobj.subject }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">是否外出</div>
              <el-form-item><span class="addinputInfo">{{ formobj.isGoOut == '1' ? '是' : '否' }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">客户名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.customerName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">联系电话</div>
              <el-form-item><span class="addinputInfo">{{ formobj.mobile }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">车牌号/车架号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.vehMark }}/{{ formobj.vinNo }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">应收合计</div>
              <el-form-item><span class="addinputInfo">{{ formobj.receivableAmount }} = 工时费：{{ formobj.hourAmount }} + 材料费：{{ formobj.goodsAmount }} + 附加费：{{ formobj.addAmount }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">反结算金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.actualAmount }} = 合计金额：{{ formobj.receivableAmount }} - 优惠：{{ formobj.discountAmount }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title">维修项目</div>
          <el-table :key="serviceKey" :data="formobj.sitemVos" :index="index" border style="width: 100%">
            <el-table-column fixed width="60" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="serviceItem" label="维修项目" align="center" width="200" />
            <el-table-column prop="serviceType" label="工种" align="center" width="150" />
            <el-table-column prop="hourPrice" label="工时单价" align="center" width="150" />
            <el-table-column prop="hours" label="工时数" align="center" width="100" />
            <el-table-column prop="price" label="销售价" align="center" width="150" />
            <el-table-column prop="sitemReceivableAmount" label="应收金额" align="center" width="150" />
            <el-table-column prop="discount" label="折扣" align="center" width="100" />
            <el-table-column prop="discountAmount" label="优惠" align="center" width="150" />
            <el-table-column prop="sitemSettleAmount" label="结算金额" align="center" width="150" />
            <el-table-column v-if="conceal" prop="sitemCost" label="成本小计" align="center" width="150" />
            <el-table-column v-if="conceal" prop="sitemPrint" label="利润额" align="center" width="150" />
            <el-table-column prop="repairerName" label="维修技师" align="center" width="200" />
            <el-table-column prop="remarks" label="备注" align="center" min-width="300" />
          </el-table>
          <div class="title">商品</div>
          <el-table :key="tableKey" :data="formobj.goodsDetailsVos" :index="index" border style="width: 100%">
            <el-table-column fixed width="60" label="序号" type="index" :index="index + 1" align="center" />
            <el-table-column prop="goodsID" label="商品ID" align="center" width="200" />
            <el-table-column prop="goodsSpuName" label="商品名称" width="100" align="center" />
            <el-table-column prop="goodsSkuCode" label="图号" width="100" align="center" />
            <el-table-column prop="count" label="数量" align="center" width="100" />
            <el-table-column prop="price" label="销售价" align="center" width="150" />
            <el-table-column prop="goodsReceivableAmount" label="应收金额" align="center" width="150" />
            <el-table-column prop="discount" label="折扣" align="center" width="100" />
            <el-table-column prop="discountAmount" label="优惠" align="center" width="150" />
            <el-table-column prop="goodsSettleAmount" label="结算金额" align="center" width="150" />
            <el-table-column v-if="conceal" prop="goodsCost" label="成本小计" align="center" width="150" />
            <el-table-column v-if="conceal" prop="goodsPrint" label="利润额" align="center" width="150" />
            <el-table-column prop="partsSellers" label="配件销售人" align="center" min-width="150" />
            <el-table-column prop="remarks" label="备注" align="center" min-width="300" />
          </el-table>
          <div class="title">附加项目</div>
          <el-row>
            <el-col :span="6">
              <div class="span-sty">外出费(元)</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.outAmount }}</span>
                  <span v-show="formobj.outKPAmount !== ''">开票金额：{{ formobj.outKPAmount }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">成本</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.outCost }}</span>
                  <span>利润额：{{ formobj.outPrint }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="span-sty">备注</div>
              <el-form-item><span class="addinputInfo">{{ formobj.outRemarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <div class="span-sty">厂家补助(元)</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.subsidyAmount }}</span>
                  <span v-show="formobj.subsidyKPAmount !== ''">开票金额：{{ formobj.subsidyKPAmount }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">成本</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.subsidyCost }}</span>
                  <span>利润额：{{ formobj.subsidyPrint }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="span-sty">备注</div>
              <el-form-item><span class="addinputInfo">{{ formobj.subsidyRemarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <div class="span-sty">施救费(元)</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.rescueAmount }}</span>
                  <span v-show="formobj.rescueKPAmount !== ''">开票金额：{{ formobj.rescueKPAmount }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">成本</div>
              <el-form-item>
                <div style="display: flex;flex-direction: row;justify-content: space-between;align-items: center">
                  <span class="addinputInfo">{{ formobj.rescueCost }}</span>
                  <span>利润额：{{ formobj.rescuePrint }}</span>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">备注</div>
              <el-form-item><span class="addinputInfo">{{ formobj.rescueRemarks }}</span></el-form-item>
            </el-col>
            <el-col :span="6" class="tlineheightb">
              <el-form-item>
                <div style="display: flex;flex-direction: column;justify-content:flex-start;align-items: flex-start;">
                  <el-button type="text" v-for="(item, index) in formobj.fileName" :key="index" @click="handleOpen(item.url)">{{ item.name }}</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-table :key="subjoinKey" :data="formobj.aitemVos" :index="index" border style="width: 100%">
            <el-table-column fixed width="60" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="aitemName" label="其它附加项目" align="center" width="200" />
            <el-table-column prop="aitemReceivableAmount" label="应收金额" align="center" width="150" />
            <el-table-column v-if="conceal" prop="aitemCost" label="成本" align="center" width="150" />
            <el-table-column v-if="conceal" prop="aitemPrint" label="利润额" align="center" width="150" />
            <el-table-column prop="remarks" label="备注" align="center" min-width="200" />
          </el-table>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import req from '@/api/operation/workslrsettlement'

export default {
  name: 'WorkSLRSettlementYiBan',
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      conceal: false,
      tableKey: 0,
      index: 0,
      // service服务
      serviceKey: 1,
      // subjoin附加项目
      subjoinKey: 2,
      formobj: {
        sid: '',
        createByName: '',
        createBySid: '',
        billNo: '',
        billType: '',
        subject: '',
        waitorName: '',
        customerName: '',
        mobile: '',
        customerOrg: '',
        vehMark: '',
        vinNo: '',
        sitemVos: [],
        aitemVos: [],
        goodsDetailsVos: [],
        orgPath: '',
        deptSid: '',
        dept: '',
        useSid: '',
        useOrgSid: '',
        useOrgName: '',
        reason: '',
        remarks: '',
        applyDate: '',
        sourceSid: '',
        amount: '',
        coupon: '',
        scoreDeduct: '',
        sourceBillDate: '',
        isGoOut: '',
        taxRate: '',
        taskId: '',
        instanceId: '',
        receivableAmount: '',
        hourAmount: '',
        goodsAmount: '',
        addAmount: '',
        discountAmount: '',
        actualAmount: '',
        outAmount: '',
        outKPAmount: '',
        outCost: '',
        outPrint: '',
        outRemarks: '',
        subsidyAmount: '',
        subsidyKPAmount: '',
        subsidyCost: '',
        subsidyPrint: '',
        subsidyRemarks: '',
        rescueAmount: '',
        rescueKPAmount: '',
        rescueCost: '',
        rescuePrint: '',
        rescueRemarks: '',
        fileName: []
      },
      rules: {},
      // 环节所需参数
      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))
    console.log('iframe页面获取的obj:', obj)
    // 点击（同意、终止、驳回、驳回）操作时所需的参数
    this.linkByParameter.businessSid = obj.businessSid
    this.linkByParameter.instanceId = obj.instanceId
    this.linkByParameter.taskId = obj.taskId
    this.linkByParameter.taskDefKey = obj.taskDefKey
    this.linkByParameter.userSid = window.sessionStorage.getItem('userSid')
    // 加载表单数据
    this.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 500 + 'px'
      }
    }, '*')
  },
  methods: {
    showInfo(sid) {
      this.viewTitle = '维修单反结算申请详情'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      if (this.formobj.createBySid !== window.sessionStorage.getItem('userSid')) {
        this.conceal = true
      }
      req.fetchBySid(sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          if (this.formobj.sitemVos.length > 0) {
            this.formobj.sitemVos.forEach((e) => {
              if (e.staffNameList.length > 0) {
                e.repairerName = e.staffNameList.join(',')
              }
            })
          }
        }
      })
    },
    handleOpen(value) {
      window.open(value, '_blank')
    },
    /** 确认撤回任务 */
    openRevoke() {
      this.$confirm('是否确认执行撤回操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRevoke()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消撤回'
        })
      })
    },
    /** 撤回任务 */
    handleRevoke() {
      req.revokeProcess(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
            }
          }, '*')
        }
      })
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 130px !important;
}
.addinputInfo {
  margin-left: 120px !important;
}
</style>
