<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="saveOrUpdate()">保存</el-button>
          <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="6">
              <div class="span-sty">分公司</div>
              <el-form-item><span class="addinputInfo">{{ formobj.applyCompany }}</span></el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">申请部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.department }}</span></el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="span-sty">申请人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.applicant }}</span></el-form-item>
            </el-col>
            <el-col :span="6">
              <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 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="image_list1" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="title">
            金融产品政策列表
          </div>
          <el-table :key="tableKey" :data="formobj.policyList" :index="index" border style="width: 100%">
            <el-table-column fixed width="80" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column label="操作" align="center" width="100">
              <template slot-scope="scope">
                <el-button type="danger" size="mini" @click="handleDelete(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
            <el-table-column label="产品政策名称" align="center" width="200">
              <template slot-scope="scope">
                <span class="bluezi" @click="lookChanPin(scope.row)">{{ scope.row.policyName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="车辆功能" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.vehTypeValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="期数" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.period }}</span>
              </template>
            </el-table-column>
            <el-table-column label="标准年利率" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.yearRatio }}</span>
              </template>
            </el-table-column>
            <el-table-column label="服务费类型" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.serviceAmountTypeValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="服务费" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.serviceAmount }}</span>
              </template>
            </el-table-column>
            <el-table-column label="最低首付比例" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ scope.row.downPayRatioLeast }}</span>
              </template>
            </el-table-column>
            <el-table-column label="单车意外险/年" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ scope.row.vehAccidentAmount }}</span>
              </template>
            </el-table-column>
            <el-table-column label="保险保证金" align="center" width="110">
              <template slot-scope="scope">
                <span>{{ scope.row.depositPremium }}</span>
              </template>
            </el-table-column>
            <el-table-column label="落户保证金" align="center" width="110">
              <template slot-scope="scope">
                <span>{{ scope.row.depositSettle }}</span>
              </template>
            </el-table-column>
            <el-table-column label="名义/留购价款" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ scope.row.nominalPrice }}</span>
              </template>
            </el-table-column>
            <el-table-column label="有效期至" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.validDateTo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="其他融" align="center" width="80">
              <template slot-scope="scope">
                <span class="bluezi" v-show="scope.row.showOtherPolicy" @click="lookOther(scope.row)">查看</span>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
    </div>
    <!-- 查看其他融详情 -->
    <otherfinancialpolicymanagementInfo v-show="viewState == 2" ref="divOther" @doback="resetState"/>
    <!-- 查看金融产品政策详情 -->
    <financialpolicymanagementInfo v-show="viewState == 3" ref="divChanPin" @doback="resetState"/>
  </div>
</template>

<script>
import req from '@/api/financialpolicyreporting/financialpolicyreporting'
import { getOrgSidByPath, fetchBySid } from '@/api/Common/dictcommons'
import uploadImg from '@/components/uploadFile/uploadImg'
import otherfinancialpolicymanagementInfo from '../otherfinancialpolicymanagement/otherfinancialpolicymanagementInfo'
import financialpolicymanagementInfo from '../financialpolicymanagement/financialpolicymanagementInfo'

export default {
  name: 'FinancialPolicyReportingAdd',
  components: {
    uploadImg,
    otherfinancialpolicymanagementInfo,
    financialpolicymanagementInfo
  },
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      tableKey: 0,
      index: 0,
      image_list1: [],
      formobj: {
        sid: '',
        userSid: '',
        applyCompany: '',
        department: '',
        applicant: '',
        applyDate: '',
        taskId: '',
        instanceId: '',
        remarks: '',
        orgSidPath: '',
        policySids: [],
        annexPaths: [],
        policyList: []
      },
      rules: {}
    }
  },
  methods: {
    showAdd(sids) {
      this.viewTitle = '【新增】金融产品政策报备'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.policyRecordInit(sids).then((res) => {
        if (res.success) {
          this.formobj = res.data
          this.formobj.userSid = window.sessionStorage.getItem('userSid')
          this.formobj.applicant = window.sessionStorage.getItem('name')
          this.formobj.orgSidPath = window.sessionStorage.getItem('defaultOrgPath')
          this.formobj.department = window.sessionStorage.getItem('defaultOrgPathName').substring(window.sessionStorage.getItem('defaultOrgPathName').lastIndexOf('/') + 1)
          getOrgSidByPath({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((resp) => {
            if (resp.success) {
              fetchBySid(resp.data).then((respsone) => {
                if (respsone.success) {
                  this.formobj.applyCompany = respsone.data.name
                }
              })
            }
          })
          var newDate = new Date()
          var date = {
            year: newDate.getFullYear(),
            month: newDate.getMonth() + 1,
            day: newDate.getDate()
          }
          this.formobj.applyDate = date.year + '-' + (date.month >= 10 ? date.month : '0' + date.month) + '-' + (date.day >= 10 ? date.day : '0' + date.day)
        }
      })
    },
    showEdit(row) {
      this.viewTitle = '【编辑】金融产品政策报备'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.fetchBySid(row.sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          if (this.formobj.annexPaths.length > 0) {
            this.formobj.annexPaths.forEach((e) => {
              this.image_list1.push({
                name: '',
                url: e
              })
            })
          }
        }
      })
    },
    handleDelete(index) {
      this.formobj.policyList.splice(index, 1)
    },
    lookChanPin(row) {
      this.viewState = 3
      this.$refs['divChanPin'].showInfo({ sid: row.policySid })
    },
    lookOther(row) {
      this.viewState = 2
      this.$refs['divOther'].showInfo(row.otherPolicySid)
    },
    saveOrUpdate() {
      if (this.formobj.policyList.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '金融产品政策列表不能为空' })
        return
      } else {
        this.formobj.policyList.forEach((e) => {
          this.formobj.policySids.push(e.policySid)
        })
      }
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          this.formobj.annexPaths = []
          if (this.image_list1.length > 0) {
            this.image_list1.forEach((e) => {
              this.formobj.annexPaths.push(e.url)
            })
          }
          req.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
          })
        }
      })
    },
    submit() {
      if (this.formobj.policyList.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '金融产品政策列表不能为空' })
        return
      } else {
        this.formobj.policyList.forEach((e) => {
          this.formobj.policySids.push(e.policySid)
        })
      }
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          this.formobj.annexPaths = []
          if (this.image_list1.length > 0) {
            this.image_list1.forEach((e) => {
              this.formobj.annexPaths.push(e.url)
            })
          }
          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
          })
        }
      })
    },
    resetState() {
      this.viewState = 1
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        sid: '',
        userSid: '',
        applyCompany: '',
        department: '',
        applicant: '',
        applyDate: '',
        taskId: '',
        instanceId: '',
        remarks: '',
        orgSidPath: '',
        policySids: [],
        annexPaths: [],
        policyList: []
      }
      this.image_list1 = []
      this.submitdisabled = false
      this.$emit('doback')
    }
  }
}
</script>

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