<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="submit()">提交</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <div class="listconadd">
        <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="24">
              <div class="span-sty">备注</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.remarks" clearable placeholder="" /></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' ? '是' : formobj.isGoOut == '2' ? '否' : '' }}</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="8">
              <div class="span-sty">是否开票</div>
              <el-form-item><span class="addinputInfo">{{ formobj.isInvoicing == '1' ? '是' : formobj.isInvoicing == '0' ? '否' : '' }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">税率</div>
              <el-form-item><span class="addinputInfo">{{ formobj.taxRate }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">加税点项目</div>
              <el-form-item><span class="addinputInfo">{{ formobj.taxItems }}</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.settleAmount }} = 合计金额：{{ formobj.receivableAmount }} - 优惠：<span>{{ formobj.discountAmount }}</span></span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <div class="span-sty">成本合计</div>
              <el-form-item><span class="addinputInfo">{{ totalCost }}</span></el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="span-sty">利润额</div>
              <el-form-item><span class="addinputInfo">{{ amountOfProfit }}</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 prop="sitemCost" label="成本小计" align="center" width="150" />
            <el-table-column 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 prop="goodsCost" label="成本小计" align="center" width="150" />
            <el-table-column 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">
                  <el-input class="addinputInfo" style="width: 30%" @input="outPrintInput" @keyup.native="formobj.outCost = getNumber(formobj.outCost, 2)" v-model="formobj.outCost" clearable placeholder="" />
                  <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">
                  <el-input class="addinputInfo" style="width: 30%" @input="subsidyPrintInput" @keyup.native="formobj.subsidyCost = getNumber(formobj.subsidyCost, 2)" v-model="formobj.subsidyCost" clearable placeholder="" />
                  <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">
                  <el-input class="addinputInfo" style="width: 30%" @input="rescuePrintInput" @keyup.native="formobj.rescueCost = getNumber(formobj.rescueCost, 2)" v-model="formobj.rescueCost" clearable placeholder="" />
                  <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 prop="aitemCost" label="成本" align="center" width="150" />
            <el-table-column 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/settleAccounts'

export default {
  name: 'SettleAccountsAdd',
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      tableKey: 0,
      index: 0,
      // service服务
      serviceKey: 1,
      // subjoin附加项目
      subjoinKey: 2,
      formobj: {
        sid: '',
        orgPath: '',
        useOrgSid: '',
        useOrgName: '',
        createBySid: '',
        createByName: '',
        deptSid: '',
        dept: '',
        billNo: '',
        remarks: '',
        applyDate: '',
        sourceSid: '',
        sourceBillNo: '',
        sourceBillDate: '',
        waitorName: '',
        billType: '',
        subject: '',
        isGoOut: '',
        customerName: '',
        mobile: '',
        vinNo: '',
        vehMark: '',
        isInvoicing: '',
        taxRate: '',
        taxItems: '',
        receivableAmount: '',
        hourAmount: '',
        goodsAmount: '',
        addAmount: '',
        discountAmount: '',
        settleAmount: '',
        costAmount: '',
        profit: '',
        sitemVos: [],
        goodsDetailsVos: [],
        aitemVos: [],
        outAmount: '',
        outKPAmount: '',
        outCost: '',
        outPrint: '',
        outRemarks: '',
        subsidyAmount: '',
        subsidyKPAmount: '',
        subsidyCost: '',
        subsidyPrint: '',
        subsidyRemarks: '',
        rescueAmount: '',
        rescueKPAmount: '',
        rescueCost: '',
        rescuePrint: '',
        rescueRemarks: '',
        fileName: [],
        taskId: '',
        instanceId: ''
      },
      rules: {}
    }
  },
  computed: {
    // 单据信息模块 -- 成本合计 = 维修项目列表成本合计 + 维修用料列表成本合计 + 其他附加项目成本合计(附加项目列表成本合计 + 外出费成本 + 厂家补助成本 + 施救费成本)
    totalCost() {
      let cost = '0'
      // 维修项目列表成本合计
      if (this.formobj.sitemVos.length > 0) {
        this.formobj.sitemVos.forEach((e) => {
          cost = Math.round((parseFloat(cost) + parseFloat(e.sitemCost !== '' ? e.sitemCost : 0)) * 100) / 100
        })
      }
      // 维修用料列表成本合计
      if (this.formobj.goodsDetailsVos.length > 0) {
        this.formobj.goodsDetailsVos.forEach((e) => {
          cost = Math.round((parseFloat(cost) + parseFloat(e.goodsCost !== '' ? e.goodsCost : 0)) * 100) / 100
        })
      }
      // 其他附加项目列表成本合计
      if (this.formobj.aitemVos.length > 0) {
        this.formobj.aitemVos.forEach((e) => {
          cost = Math.round((parseFloat(cost) + parseFloat(e.aitemCost !== '' ? e.aitemCost : 0)) * 100) / 100
        })
      }
      cost = Math.round((parseFloat(cost) + parseFloat(this.formobj.outCost !== '' ? this.formobj.outCost : 0) + parseFloat(this.formobj.subsidyCost !== '' ? this.formobj.subsidyCost : 0) + parseFloat(this.formobj.rescueCost !== '' ? this.formobj.rescueCost : 0)) * 100) / 100
      return cost
    },
    // 单据信息模块 --  利润额 = 维修项目列表利润额合计 + 维修用料列表利润额合计 + 其他附加项目利润额合计(附加项目列表利润额合计 + 外出费利润额 + 厂家补助利润额 + 施救费利润额)
    amountOfProfit() {
      let print = '0'
      // 维修项目列表成本合计
      if (this.formobj.sitemVos.length > 0) {
        this.formobj.sitemVos.forEach((e) => {
          print = Math.round((parseFloat(print) + parseFloat(e.sitemPrint !== '' ? e.sitemPrint : 0)) * 100) / 100
        })
      }
      // 维修用料列表成本合计
      if (this.formobj.goodsDetailsVos.length > 0) {
        this.formobj.goodsDetailsVos.forEach((e) => {
          print = Math.round((parseFloat(print) + parseFloat(e.goodsPrint !== '' ? e.goodsPrint : 0)) * 100) / 100
        })
      }
      // 其他附加项目列表成本合计
      if (this.formobj.aitemVos.length > 0) {
        this.formobj.aitemVos.forEach((e) => {
          print = Math.round((parseFloat(print) + parseFloat(e.aitemPrint !== '' ? e.aitemPrint : 0)) * 100) / 100
        })
      }
      print = Math.round((parseFloat(print) + parseFloat(this.formobj.outPrint !== '' ? this.formobj.outPrint : 0) + parseFloat(this.formobj.subsidyPrint !== '' ? this.formobj.subsidyPrint : 0) + parseFloat(this.formobj.rescuePrint !== '' ? this.formobj.rescuePrint : 0)) * 100) / 100
      return print
    }
  },
  methods: {
    getNumber(val, limit) {
      val = val.replace(/[^0-9.]/g, '') // 保留数字
      val = val.replace(/^00/, '0.') // 开头不能有两个0
      val = val.replace(/^\./g, '0.') // 开头为小数点转换为0.
      val = val.replace(/\.{2,}/g, '.') // 两个以上的小数点转换成一个
      val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.'); // 只保留一个小数点
      /^0\d+/.test(val) ? val = val.slice(1) : '' // 两位以上数字开头不能为0
      const str = '^(\\d+)\\.(\\d{' + limit + '}).*$'
      const reg = new RegExp(str)
      if (limit === 0) {
        // 不需要小数点
        val = val.replace(reg, '$1')
      } else {
        // 通过正则保留小数点后指定的位数
        val = val.replace(reg, '$1.$2')
      }
      return val
    },
    showAdd(row) {
      this.viewTitle = '结算'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.busrepairbillInit({ sid: row.sid, userSid: window.sessionStorage.getItem('userSid'), orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.formobj = res.data
        }
      })
    },
    showEdit(row) {
      this.viewTitle = '结算'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.fetchBySid(row.sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
        }
      })
    },
    // 计算附加项目 -- 外出费 -- 利润额 = 不开票金额 - 成本
    outPrintInput() {
      this.formobj.outPrint = Math.round((parseFloat(this.formobj.outAmount !== '' ? this.formobj.outAmount : 0) - parseFloat(this.formobj.outCost !== '' ? this.formobj.outCost : 0)) * 100) / 100
    },
    // 计算附加项目 -- 厂家补助 -- 利润额 = 不开票金额 - 成本
    subsidyPrintInput() {
      this.formobj.subsidyPrint = Math.round((parseFloat(this.formobj.subsidyAmount !== '' ? this.formobj.subsidyAmount : 0) - parseFloat(this.formobj.subsidyCost !== '' ? this.formobj.subsidyCost : 0)) * 100) / 100
    },
    // 计算附加项目 -- 施救费 -- 利润额 = 不开票金额 - 成本
    rescuePrintInput() {
      this.formobj.rescuePrint = Math.round((parseFloat(this.formobj.rescueAmount !== '' ? this.formobj.rescueAmount : 0) - parseFloat(this.formobj.rescueCost !== '' ? this.formobj.rescueCost : 0)) * 100) / 100
    },
    handleOpen(value) {
      window.open(value, '_blank')
    },
    submit() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.formobj.costAmount = this.totalCost
          this.formobj.profit = this.amountOfProfit
          this.submitdisabled = true
          req.submit(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '提交成功' })
              this.handleReturn('true')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        sid: '',
        orgPath: '',
        useOrgSid: '',
        useOrgName: '',
        createBySid: '',
        createByName: '',
        deptSid: '',
        dept: '',
        billNo: '',
        remarks: '',
        applyDate: '',
        sourceSid: '',
        sourceBillNo: '',
        sourceBillDate: '',
        waitorName: '',
        billType: '',
        subject: '',
        isGoOut: '',
        customerName: '',
        mobile: '',
        vinNo: '',
        vehMark: '',
        isInvoicing: '',
        taxRate: '',
        taxItems: '',
        receivableAmount: '',
        hourAmount: '',
        goodsAmount: '',
        addAmount: '',
        discountAmount: '',
        settleAmount: '',
        costAmount: '',
        profit: '',
        sitemVos: [],
        goodsDetailsVos: [],
        aitemVos: [],
        outAmount: '',
        outKPAmount: '',
        outCost: '',
        outPrint: '',
        outRemarks: '',
        subsidyAmount: '',
        subsidyKPAmount: '',
        subsidyCost: '',
        subsidyPrint: '',
        subsidyRemarks: '',
        rescueAmount: '',
        rescueKPAmount: '',
        rescueCost: '',
        rescuePrint: '',
        rescueRemarks: '',
        fileName: [],
        taskId: '',
        instanceId: ''
      }
      this.submitdisabled = false
      this.$refs['form_obj'].resetFields()
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 110px !important;
}
.addinputInfo {
  margin-left: 100px !important;
}
.tlineheightb {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
/deep/ .tlineheightb .el-form-item .el-form-item__content .addinputInfo {
  line-height: 15px !important;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
</style>
