<template>
  <div class="app-container">
    <!--标题按钮部分开始-->
    <div class="tab-header webtop">
      <!--标题-->
      <div>{{ viewTitle }}</div>
      <!--start 新增修改按钮-->
      <div>
        <el-button type="primary" size="small" :disabled="submitdisabled" @click="handleSave()">保存
        </el-button>
        <el-button type="primary" size="small" :disabled="submitdisabled" @click="handleSubmit()">提交
        </el-button>
        <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
      </div>
      <!--end 添加修改按钮-->
    </div>
    <!--按钮部分结束-->
    <div class="listconadd">
      <div class="titwu">欠款领票及领手续</div>
      <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.deptName }}</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="8">
            <div class="span-sty">销售专员</div>
            <el-form-item><span class="addinputInfo">{{ formobj.staffName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">销售部门</div>
            <el-form-item><span class="addinputInfo">{{ formobj.saleDeptName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">销售类型</div>
            <el-form-item><span class="addinputInfo">{{ formobj.saleTypeValue }}</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.contractNo }}</span></el-form-item>
          </el-col>
          <el-col :span="8" class="tlineheightb">
            <div class="span-sty">开票单位</div>
            <el-form-item><span class="addinputInfo">{{ formobj.openTickUnitName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">客户名称</div>
            <el-form-item><span class="addinputInfo">{{ formobj.customerName }}</span></el-form-item>
          </el-col>
        </el-row>
        <div class="title">主车开票信息</div>
        <el-table :key="tableKey" :data="formobj.finBillVehicles" :index="index" border style="width: 100%">
          <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
          <el-table-column label="车架号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.vinNo }}</span>
            </template>
          </el-table-column>
          <el-table-column label="开票类型" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.billTypeValue }}</span>
            </template>
          </el-table-column>
          <el-table-column label="开票名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.openTickName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="开票信息" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.openTickRemarks }}</span>
            </template>
          </el-table-column>
          <el-table-column label="开票金额" align="center">
            <template>
              <span>{{ formobj.oneBillMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column label="合格证状态" align="center">
            <template slot-scope="scope">
              <span class="bluezi" @click="handleLook(scope.row.certificatePhoto)">{{ scope.row.certificateStateValue }}</span>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
            <div class="span-sty">开票金额合计</div>
            <el-form-item><span class="addinputInfo">{{ totalBillMoney() }}</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">{{ formobj.arrearsMoney }}</span></el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="span-sty">承诺回款日期</div>
            <el-form-item><span class="addinputInfo">{{ formobj.returnedMoneyDate }}</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.arrearsOpenYYValue }}</span></el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item><span class="addinputInfo" style="margin-left: 0px !important;">{{ formobj.remarks }}</span></el-form-item>
          </el-col>
        </el-row>
        <div v-if="formobj.finBillTrailers !== null">
          <div class="title">挂车开票信息</div>
          <el-table :key="billingKey" :data="formobj.finBillTrailers" :index="index" border style="width: 100%">
            <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column label="挂车车架号" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.gcVinNo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="主车车架号" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.vinNo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票类型" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.billTypeValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票名称" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.openTickName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票信息" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.invoiceInfo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票金额" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.oneBillMoney }}</span>
              </template>
            </el-table-column>
            <el-table-column label="合格证" align="center">
              <template slot-scope="scope">
                <span class="bluezi" @click="handleLook(scope.row.hgzImageUrl)">查看</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-row>
          <el-col :span="24">
            <div class="span-sty">提前领取发票原因</div>
            <el-form-item><el-input v-model="formobj.reason" class="addinputInfo addinputw" clearable placeholder=""/></el-form-item>
          </el-col>
        </el-row>
        <div class="title">《欠款承诺书》(欠款人、担保人同签)</div>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <upload ref="uploadImg" v-model="image_list1" :accept="accept" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title">《公司股东会对外担保决议》(公司担保)</div>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <upload ref="uploadImg" v-model="image_list2" :accept="accept" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title">《责任人担保承诺书》</div>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <upload ref="uploadImg" v-model="image_list3" :accept="accept" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title">资方信审截图</div>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <upload ref="uploadImg" v-model="image_list4" :accept="accept" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-dialog :visible.sync="dialogVisible" :append-to-body="true">
      <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in srcList" :key="index" :src="item" :preview-src-list="srcList"></el-image>
    </el-dialog>
  </div>
</template>

<script>
import { fetchDetailsBySid, submit, saveOrUpdate } from '@/api/anruifinmanagement/arrearscollection'
import upload from '@/components/uploadFile/upload'

export default {
  name: 'ArrearsScollectionAdd',
  components: {
    upload
  },
  data() {
    return {
      submitdisabled: false,
      dialogVisible: false,
      srcList: [],
      viewTitle: '',
      tableKey: 0,
      billingKey: 1,
      index: 0,
      accept: '.jpg,.jpeg,.png,.JPG,.JPEG',
      image_list1: [],
      image_list2: [],
      image_list3: [],
      image_list4: [],
      formobj: {
        applyDate: '',
        arrearsMoney: '',
        arrearsOpenYYKey: '',
        arrearsOpenYYValue: '',
        billNo: '',
        billStateKey: '',
        billStateValue: '',
        cldjhtApplicationAppendxs: [],
        contractNo: '',
        contractSid: '',
        createByName: '',
        customerName: '',
        customerSid: '',
        deptName: '',
        deptSid: '',
        fjApplicationAppendxs: [],
        finBillTrailers: [],
        finBillVehicles: [],
        isDelayBill: '',
        isHighAndLowOpen: '',
        isLQOrigInvoice: '',
        khkpsqsApplicationAppendxs: [],
        kpxxqrsApplicationAppendxs: [],
        modelName: '',
        modelSid: '',
        nodeSid: '',
        nodeState: '',
        oneBillMoney: '',
        openTickName: '',
        openTickSid: '',
        openTickTypeKey: '',
        openTickTypeValue: '',
        openTickUnitName: '',
        openTickUnitSid: '',
        orgSidPath: '',
        pcgsgdhdwdbjy: [],
        pczfxsjt: [],
        pczrrdbcns: [],
        procDefId: '',
        procInstSid: '',
        remarks: '',
        reason: '',
        returnedMoneyDate: '',
        saleDeptName: '',
        saleDeptSid: '',
        saleTypeKey: '',
        saleTypeValue: '',
        sid: '',
        staffName: '',
        staffSid: '',
        totalBillMoney: '',
        useOrgSid: '',
        createBySid: '',
        qkkpBillNo: '',
        zjApplicationAppendxs: []
      },
      rules: {}
    }
  },

  methods: {
    showAdd(sid) {
      this.viewTitle = '【新增】欠款领票及领手续'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      fetchDetailsBySid(sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          this.formobj.sid = ''
          this.formobj.qkkpBillNo = res.data.billNo
        }
      })
    },
    showEdit(row) {
      this.viewTitle = '【编辑】欠款领票及领手续'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      fetchDetailsBySid(row.sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          this.formobj.instanceId = res.data.procInstSid
          if (this.formobj.fjApplicationAppendxs.length > 0) {
            this.formobj.fjApplicationAppendxs.forEach((e) => {
              this.image_list1.push({
                name: e.fileName,
                url: e.fileUrl
              })
            })
          }
          if (this.formobj.pcgsgdhdwdbjy.length > 0) {
            this.formobj.pcgsgdhdwdbjy.forEach((e) => {
              this.image_list2.push({
                name: e.fileName,
                url: e.fileUrl
              })
            })
          }
          if (this.formobj.pczrrdbcns.length > 0) {
            this.formobj.pczrrdbcns.forEach((e) => {
              this.image_list3.push({
                name: e.fileName,
                url: e.fileUrl
              })
            })
          }
          if (this.formobj.pczfxsjt.length > 0) {
            this.formobj.pczfxsjt.forEach((e) => {
              this.image_list4.push({
                name: e.fileName,
                url: e.fileUrl
              })
            })
          }
        }
      })
    },
    handleLook(row) {
      this.srcList = []
      this.dialogVisible = true
      this.srcList = row
    },
    totalBillMoney() {
      var aa = 0
      if (this.formobj.finBillVehicles.length > 0) {
        aa = parseInt(this.formobj.finBillVehicles.length) * parseInt(this.formobj.oneBillMoney)
      }
      return aa
    },
    handleSave() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.getUrl()
          this.submitdisabled = true
          saveOrUpdate(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
          })
        }
      })
    },
    handleSubmit() {
      if (this.image_list1.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '请上传欠款承诺书' })
        return
      }
      if (this.image_list3.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '请上传责任人担保承诺书' })
        return
      }
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.getUrl()
          this.submitdisabled = true
          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
          })
        }
      })
    },
    getUrl() {
      if (this.image_list1.length > 0) {
        const aa = []
        for (var i = 0; i < this.image_list1.length; i++) {
          aa.push({
            attachType: '',
            billAppSid: '',
            createByName: window.sessionStorage.getItem('name'),
            fileSid: '',
            fileName: this.image_list1[i].name,
            fileType: this.image_list1[i].name.substr(this.image_list1[i].name.lastIndexOf('.')),
            fileUrl: this.image_list1[i].url,
            sid: ''
          })
        }
        this.formobj.fjApplicationAppendxs = aa
      } else {
        this.formobj.fjApplicationAppendxs = []
      }
      if (this.image_list2.length > 0) {
        const aa = []
        for (var k = 0; k < this.image_list2.length; k++) {
          aa.push({
            attachType: '',
            billAppSid: '',
            createByName: window.sessionStorage.getItem('name'),
            fileSid: '',
            fileName: this.image_list2[k].name,
            fileType: this.image_list2[k].name.substr(this.image_list2[k].name.lastIndexOf('.')),
            fileUrl: this.image_list2[k].url,
            sid: ''
          })
        }
        this.formobj.pcgsgdhdwdbjy = aa
      } else {
        this.formobj.pcgsgdhdwdbjy = []
      }
      if (this.image_list3.length > 0) {
        const aa = []
        for (var p = 0; p < this.image_list3.length; p++) {
          aa.push({
            attachType: '',
            billAppSid: '',
            createByName: window.sessionStorage.getItem('name'),
            fileSid: '',
            fileName: this.image_list3[p].name,
            fileType: this.image_list3[p].name.substr(this.image_list3[p].name.lastIndexOf('.')),
            fileUrl: this.image_list3[p].url,
            sid: ''
          })
        }
        this.formobj.pczrrdbcns = aa
      } else {
        this.formobj.pczrrdbcns = []
      }
      if (this.image_list4.length > 0) {
        const aa = []
        for (var q = 0; q < this.image_list4.length; q++) {
          aa.push({
            attachType: '',
            billAppSid: '',
            createByName: window.sessionStorage.getItem('name'),
            fileSid: '',
            fileName: this.image_list4[q].name,
            fileType: this.image_list4[q].name.substr(this.image_list4[q].name.lastIndexOf('.')),
            fileUrl: this.image_list4[q].url,
            sid: ''
          })
        }
        this.formobj.pczfxsjt = aa
      } else {
        this.formobj.pczfxsjt = []
      }
    },
    // 关闭、返回到列表页面
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        applyDate: '',
        arrearsMoney: '',
        arrearsOpenYYKey: '',
        arrearsOpenYYValue: '',
        billNo: '',
        billStateKey: '',
        billStateValue: '',
        cldjhtApplicationAppendxs: [],
        contractNo: '',
        contractSid: '',
        createByName: '',
        customerName: '',
        customerSid: '',
        deptName: '',
        deptSid: '',
        fjApplicationAppendxs: [],
        finBillTrailers: [],
        finBillVehicles: [],
        isDelayBill: '',
        isHighAndLowOpen: '',
        isLQOrigInvoice: '',
        khkpsqsApplicationAppendxs: [],
        kpxxqrsApplicationAppendxs: [],
        modelName: '',
        modelSid: '',
        nodeSid: '',
        nodeState: '',
        oneBillMoney: '',
        openTickName: '',
        openTickSid: '',
        openTickTypeKey: '',
        openTickTypeValue: '',
        openTickUnitName: '',
        openTickUnitSid: '',
        orgSidPath: '',
        pcgsgdhdwdbjy: [],
        pczfxsjt: [],
        pczrrdbcns: [],
        procDefId: '',
        procInstSid: '',
        remarks: '',
        reason: '',
        returnedMoneyDate: '',
        saleDeptName: '',
        saleDeptSid: '',
        saleTypeKey: '',
        saleTypeValue: '',
        sid: '',
        staffName: '',
        staffSid: '',
        totalBillMoney: '',
        useOrgSid: '',
        createBySid: '',
        qkkpBillNo: '',
        zjApplicationAppendxs: []
      }
      this.submitdisabled = false
      this.$refs['form_obj'].resetFields()
      this.image_list1 = []
      this.image_list2 = []
      this.image_list3 = []
      this.image_list4 = []
      this.$emit('doback')
    }
  }
}
</script>

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