<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="toSelect">选择车辆</el-button>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate()">保存</el-button>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="submit()">提交</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.deptName }}</span></el-form-item>
            </el-col>
            <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.createTime }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">备注</div>
              <el-form-item><el-input v-model="formobj.remarks" clearable placeholder="" class="addinputInfo addinputw" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">附件</div>
              <el-form-item>
                <upload-img ref="uploadImg" class="addinputInfo" v-model="formobj.files" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="title">
            <div>逾期车辆列表</div>
          </div>
          <el-table :key="tableKey" :data="formobj.loanVehClearanceVehList" :index="index" border style="width: 100%">
            <el-table-column fixed width="60" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column fixed label="操作" align="center" width="80">
              <template slot-scope="scope">
                <el-button type="danger" size="small" @click="toDelete(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="loanContractNo" label="贷款合同编号" align="center" width="160" />
            <el-table-column prop="vinNo" label="车架号" align="center" width="100"/>
            <el-table-column prop="carNum" label="车牌号" align="center" width="100" />
            <el-table-column prop="bankName" label="资方" align="center" width="100" />
            <el-table-column prop="bankContractNo" label="资方合同编号" align="center" width="160"/>
            <el-table-column prop="customerName" label="客户名称" align="center" width="100" />
            <el-table-column prop="borrowerName" label="贷款人" align="center" width="100"/>
            <el-table-column prop="firstBeDate" label="首次逾期日期" align="center" width="120"/>
            <el-table-column prop="beOverduePeriod" label="逾期期数" align="center" width="100" />
            <el-table-column prop="beOverdueMoney" label="逾期金额" align="center" width="100" />
            <el-table-column prop="beOverdueMoneyAndPeriod" label="换算期数" align="center" width="100" />
            <el-table-column prop="lockCarMoney" label="远程控制费用" align="center" width="120"/>
            <el-table-column label="远程控制费实收" align="center" width="120">
              <template slot-scope="scope">
                <el-input v-model="scope.row.lockCarRealityMoney" clearable placeholder=""/>
              </template>
            </el-table-column>
            <el-table-column label="查看相关资料" align="center" width="150">
              <template slot-scope="scope">
                <el-select v-model="scope.row.name" @change="dataChange($event, scope.row, scope.row.sids)" placeholder="请选择" clearable>
                  <el-option v-for="item in scope.row.sids" :key="item.name" :label="item.name" :value="item.name"></el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
    </div>
    <!-- 查看担保人详情 -->
    <cautionerInfo v-show="viewState == 2" ref="divCautioner" @doback="resetState" />
    <!-- 查看月还明细 -->
    <monthlyreturnInfo v-show="viewState == 4" ref="divMonthlyReturn" @doback="resetState"/>
    <!-- 查看销售订单--金融方案 -->
    <financialschemeInfo v-show="viewState == 5" ref="divFinancial" @doback="resetState"/>
    <!-- 查看家访考察报告 -->
    <homevisitreport v-show="viewState == 6" ref="divReport" @doback="resetState"/>
    <!-- 查看家访资料 -->
    <materialInfo v-show="viewState == 7" ref="divMterial" @doback="resetState"/>
    <!-- 批量选择车辆 -->
    <selectvehicle v-show="viewState == 8" ref="divVehicle" @backData="backData" @doback="resetState"/>
    <!-- 还款协议查看 -->
    <el-dialog :visible.sync="dialogVisible">
      <el-image style="width: 150px; height: 150px" v-for="(item, index) in list" :key="index" :src="item" :preview-src-list="list"/>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/remotedecontrol/remotedecontrol'
import uploadImg from '@/components/uploadFile/uploadImg'
import { getOrgSidByPath } from '@/api/Common/dictcommons'
import cautionerInfo from '../cuishoucuoshiFlow/relation/cautionerInfo'
import monthlyreturnInfo from '../cuishoucuoshiFlow/relation/monthlyreturnInfo'
import financialschemeInfo from '../publicPage/financialschemeInfo'
import homevisitreport from '../cuishoucuoshiFlow/relation/homevisitreport'
import materialInfo from '../cuishoucuoshiFlow/relation/materialInfo'
import selectvehicle from './relation/selectvehicle'

export default {
  name: 'RemotedeControlEdit',
  components: {
    uploadImg,
    cautionerInfo,
    monthlyreturnInfo,
    financialschemeInfo,
    homevisitreport,
    materialInfo,
    selectvehicle
  },
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      dialogVisible: false,
      list: [],
      tableKey: 0,
      index: 0,
      formobj: {
        billNo: '',
        createByName: '',
        createBySid: '',
        createTime: '',
        deptName: '',
        deptSid: '',
        files: [],
        loanVehClearanceVehList: [],
        nodeSid: '',
        nodeState: '',
        orgPath: '',
        procDefId: '',
        procInstSid: '',
        remarks: '',
        sid: '',
        taskId: '',
        useOrgName: '',
        useOrgSid: ''
      },
      rules: {}
    }
  },
  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.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()
      })
      req.fetchBySid(sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          this.formobj.instanceId = res.data.procInstSid
        }
      })
    },
    toSelect() {
      this.viewState = 8
      this.$refs['divVehicle'].showData(this.formobj.loanVehClearanceVehList, this.formobj.useOrgSid)
    },
    toDelete(index) {
      this.formobj.loanVehClearanceVehList.splice(index, 1)
    },
    // 批量选择车辆后返回数据
    backData(value) {
      this.viewState = 1
      const params = {
        saleVehSids: [],
        orgPath: '',
        userSid: ''
      }
      if (value.length > 0) {
        params.saleVehSids = value
        params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
        params.userSid = window.sessionStorage.getItem('userSid')
        req.init(params).then((res) => {
          if (res.success) {
            this.formobj = res.data
          }
        })
      }
    },
    dataChange(value, row, list) {
      const choose = list.filter((item) => item.name === value)
      if (value === '担保人' && choose[0].sid !== '') {
        this.viewState = 2
        this.$refs['divCautioner'].showInfo(choose[0].sid)
      } else if (value === '还款协议' && choose[0].files.length > 0  && choose[0].files !== null) {
        this.dialogVisible = true
        this.list = []
        this.list = choose[0].files
      } else if (value === '月还明细' && choose[0].sid !== '') {
        this.viewState = 4
        this.$refs['divMonthlyReturn'].showInfo({ saleVehSid: choose[0].sid })
      } else if (value === '金融方案' && choose[0].sid !== '') {
        req.selectDetailss({ businessSid: choose[0].sid }).then((res) => {
          if (res.success) {
            this.viewState = 5
            this.$refs['divFinancial'].showInfo(res.data, '03')
          }
        })
      } else if (value === '家访考察报告' && choose[0].sid !== '') {
        this.viewState = 6
        this.$refs['divReport'].showInfo({ kcbgSid: choose[0].sid })
      } else if (value === '家访考察资料' && choose[0].sid !== '') {
        this.viewState = 7
        this.$refs['divMterial'].showInfo({ kczlSid: choose[0].sid })
      } else {}
    },
    resetState() {
      this.viewState = 1
    },
    saveOrUpdate() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          if (this.formobj.loanVehClearanceVehList.length === 0) {
            this.$message({ showClose: true, type: 'error', message: '逾期车辆列表不能为空' })
            return
          }
          this.submitdisabled = true
          req.saveOrUpdate(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '保存成功' })
              // 子页面向父级页面传递值(关闭弹框)
              window.parent.postMessage({
                cmd: 'returnHeight',
                params: {
                  // 操作成功，告诉父级页面关闭弹框
                  code: 1
                }
              }, '*')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    submit() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          if (this.formobj.loanVehClearanceVehList.length === 0) {
            this.$message({ showClose: true, type: 'error', message: '逾期车辆列表不能为空' })
            return
          }
          this.submitdisabled = true
          req.submit(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '操作成功' })
              // 子页面向父级页面传递值(关闭弹框)
              window.parent.postMessage({
                cmd: 'returnHeight',
                params: {
                  // 操作成功，告诉父级页面关闭弹框
                  code: 1
                }
              }, '*')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.addinputInfo {
  margin-left: 60px !important;
}
</style>
