<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="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.createDept }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.saleDate }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售价格</div>
              <el-form-item><span class="addinputInfo">{{ formobj.salePrice }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title titleOne">
            <div>客户信息<span style="color: red;margin-left: 10px">注：若客户信息不存在请先点击新增客户按钮完善客户信息</span></div>
            <el-button type="primary" size="mini" @click="addCustomer">新增客户</el-button>
          </div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>新车主名称</div>
              <el-form-item>
                <el-select v-model="formobj.customerName" class="addinputInfo" filterable clearable placeholder="" @change="changeCustomer">
                  <el-option v-for="item in customer_list" :key="item.newCustomerSid" :label="item.vinOwner" :value="item.vinOwner"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>客户类型</div>
              <el-form-item><span class="addinputInfo">{{ formobj.customerType }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>联系电话</div>
              <el-form-item><span class="addinputInfo">{{ formobj.mobile }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">证件类型</div>
              <el-form-item>
                <el-select class="addinputInfo" v-model="formobj.certificateTypeKey" filterable clearable placeholder="" @change="certificateTypeChange">
                  <el-option v-for="item in certificateType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">证件号码</div>
              <el-form-item><el-input class="addinputInfo" style="width: 40%;" v-model="formobj.IDNumber" clearable placeholder=""/></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">证件有效期</div>
              <el-form-item><el-date-picker class="addinputInfo" v-model="formobj.endDate" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期" />
              </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.certificateAddress" clearable placeholder=""/></el-form-item>
            </el-col>
          </el-row>
          <div class="title">二次销售车辆列表</div>
          <el-table :key="tableKey" :data="formobj.loanSecondarySalesVehVoList" :index="index" border style="width: 100%">
            <el-table-column fixed width="80" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="vinNo" label="车架号" align="center" min-width="100" />
            <el-table-column prop="vehMark" label="车牌号" align="center" min-width="100"/>
            <el-table-column prop="vehType" label="车辆类型" align="center" min-width="120"/>
            <el-table-column prop="modelName" label="车型" align="center" min-width="130"/>
          </el-table>
          <div class="title">附件</div>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">二次销售合同</div>
              <el-form-item>
                <uploadImg ref="uploadImg" class="addinputInfo" v-model="formobj.contractFiles" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">其他证件</div>
              <el-form-item>
                <uploadImg ref="uploadImg" class="addinputInfo" v-model="formobj.otherFiles" :limit="50" bucket="map" :upload-data="{ type: '0001' }"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="70%">
      <el-form :model="formobj" class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="4" class="tleftb">
            <span>客户名称</span>
          </el-col>
          <el-col :span="8">
            <el-form-item><el-input class="addinputw" v-model="formobj.customerName" clearable placeholder=""/></el-form-item>
          </el-col>
          <el-col :span="4" class="tleftb">
            <span>客户类型</span>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-select v-model="formobj.customerTypeKey" filterable clearable placeholder="" @change="customerTypeChange">
                <el-option v-for="item in customerType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4" class="tleftb">
            <span>联系电话</span>
          </el-col>
          <el-col :span="8">
            <el-form-item><el-input class="addinputw" v-model="formobj.mobile" clearable placeholder=""/></el-form-item>
          </el-col>
          <el-col :span="4" class="tleftb">
            <span>证件类型</span>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-select v-model="formobj.certificateTypeKey" filterable clearable placeholder="" @change="certificateTypeChange">
                <el-option v-for="item in certificateType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4" class="tleftb">
            <span>证件号码</span>
          </el-col>
          <el-col :span="8">
            <el-form-item><el-input class="addinputw" v-model="formobj.IDNumber" clearable placeholder=""/></el-form-item>
          </el-col>
          <el-col :span="4" class="tleftb">
            <span>证件有效期</span>
          </el-col>
          <el-col :span="8">
            <el-form-item><el-date-picker class="addinputw" v-model="formobj.endDate" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4" class="tleftb">
            <span>证件地址</span>
          </el-col>
          <el-col :span="20">
            <el-form-item><el-input class="addinputw" v-model="formobj.certificateAddress" clearable placeholder=""/></el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align:center;margin-top: 20px;">
        <el-button type="primary" size="mini" @click="reject">确 定</el-button>
        <el-button type="info " size="mini" @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/secondarysales/secondarysales'
import { pickCustomer, typeValues } from '@/api/Common/dictcommons'

export default {
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      dialogVisible: false,
      tableKey: 0,
      index: 0,
      customer_list: [],
      customerType_list: [],
      certificateType_list: [],
      formobj: {
        mainSid: '',
        createDept: '',
        saleDate: '',
        salePrice: '',
        customerSid: '',
        customerName: '',
        customerType: '',
        customerTypeKey: '',
        mobile: '',
        certificateTypeKey: '',
        certificateType: '',
        IDNumber: '',
        endDate: '',
        certificateAddress: '',
        loanSecondarySalesVehVoList: [],
        contractFiles: [],
        otherFiles: []
      },
      rules: {}
    }
  },
  methods: {
    init() {
      typeValues({ type: 'certificateType' }).then((resp) => {
        if (resp.success) {
          this.certificateType_list = resp.data
        }
      })
      typeValues({ type: 'customerType' }).then((resp) => {
        if (resp.success) {
          this.customerType_list = resp.data
        }
      })
      pickCustomer({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.customer_list = res.data
        }
      })
    },
    showEdit(sid) {
      this.viewTitle = '交回车辆二次销售合同信息补充'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.init()
      req.getDetails({ sid: sid }).then((res) => {
        if (res.success) {
          this.formobj = res.data
        }
      })
    },
    addCustomer() {
      this.dialogVisible = true
      this.formobj.customerName = ''
      this.formobj.customerSid = ''
      this.formobj.customerType = ''
      this.formobj.customerTypeKey = ''
      this.formobj.mobile = ''
      this.formobj.endDate = ''
      this.formobj.IDNumber = ''
      this.formobj.certificateType = ''
      this.formobj.certificateTypeKey = ''
      this.formobj.certificateAddress = ''
    },
    reject() {
      this.dialogVisible = false
    },
    changeCustomer(value) {
      const choosetItem = this.customer_list.filter((item) => item.vinOwner === value)
      if (choosetItem.length > 0 && choosetItem !== null) {
        this.formobj.customerSid = choosetItem[0].newCustomerSid
        this.formobj.customerType = choosetItem[0].customerType
        this.formobj.customerTypeKey = choosetItem[0].customerTypeKey
        this.formobj.mobile = choosetItem[0].phoneNum
        this.formobj.endDate = choosetItem[0].endDate
        this.formobj.IDNumber = choosetItem[0].idNumber
        this.formobj.certificateType = choosetItem[0].certificateType
        this.formobj.certificateTypeKey = choosetItem[0].certificateTypeKey
        this.formobj.certificateAddress = ''
      } else {
        this.formobj.customerSid = ''
        this.formobj.customerType = ''
        this.formobj.customerTypeKey = ''
        this.formobj.mobile = ''
        this.formobj.endDate = ''
        this.formobj.IDNumber = ''
        this.formobj.certificateType = ''
        this.formobj.certificateTypeKey = ''
        this.formobj.certificateAddress = ''
      }
    },
    customerTypeChange(value) {
      const choose = this.customerType_list.filter((item) => item.dictKey === value)
      if (choose.length > 0 && choose !== null) {
        this.formobj.customerType = choose[0].dictValue
      } else {
        this.formobj.customerType = ''
      }
    },
    certificateTypeChange(value) {
      const choose = this.certificateType_list.filter((item) => item.dictKey === value)
      if (choose.length > 0 && choose !== null) {
        this.formobj.certificateType = choose[0].dictValue
      } else {
        this.formobj.certificateType = ''
      }
    },
    saveOrUpdate() {
      if (this.formobj.customerName === '' || this.formobj.mobile === '' || this.formobj.customerType === '') {
        this.$message({ showClose: true, type: 'error', message: '客户信息中新车主名称、客户类型、联系电话等不能为空' })
        return
      }
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          if (this.formobj.contractFiles.length > 0) {
            const aa = []
            this.formobj.contractFiles.forEach((e) => {
              aa.push(e.url)
            })
            this.formobj.contractFiles = aa
          }
          if (this.formobj.otherFiles.length > 0) {
            const bb = []
            this.formobj.otherFiles.forEach((e) => {
              bb.push(e.url)
            })
            this.formobj.otherFiles = bb
          }
          req.save(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 = {
        mainSid: '',
        createDept: '',
        saleDate: '',
        salePrice: '',
        customerSid: '',
        customerName: '',
        customerType: '',
        customerTypeKey: '',
        mobile: '',
        certificateTypeKey: '',
        certificateType: '',
        IDNumber: '',
        endDate: '',
        certificateAddress: '',
        loanSecondarySalesVehVoList: [],
      }
      this.submitdisabled = false
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 100px !important;
}
.addinputInfo {
  margin-left: 90px !important;
}
.titleOne {
  padding: 7px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>
