<template>
  <div class="eibs-tab">
    <c-col :span="12" class="col-left">
      <c-col :span="24">
        <el-form-item label="国结客户类型" prop="ptytyp">
          <c-select v-model="model.ptytyp" style="width: 100%" placeholder="请选择国结客户类型" dbCode="PTYTYP" :filterKey="['B','H','A','C','O']" :disabled="isPageDisabled || opertype == 'edit'"> </c-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="客户编号" prop="extkey">
          <c-fullbox>
            <c-input v-model="model.extkey" placeholder="请输入客户编号" :disabled="isPageDisabled || opertype == 'edit'"> </c-input>
            <template slot="footer">
              <c-button
                style="margin: 0 0 0 10px; padding: 0 12px"
                size="small"
                type="primary"
                :disabled="!model.extkey || type =='info'||(model.ptytyp !== 'C' && model.ptytyp !== 'B' ) "
                @click="baseInfo(model.extkey)"
              >
                获取
              </c-button>
            </template>
          </c-fullbox>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="ECIF客户号" prop="ecifno">
          <c-fullbox>
            <c-input v-model="model.ecifno" placeholder="请输入ECIF客户号" :disabled="isPageDisabled || model.ptytyp != 'B' || opertype == 'edit'"> </c-input>
            <template slot="footer">
              <c-button
                style="margin: 0 0 0 10px; padding: 0 12px"
                size="small"
                type="primary"
                :disabled="!model.ecifno || model.ptytyp != 'B' || type == 'info'"
                @click="baseECIFInfo(model.ecifno)"
              >
                获取
              </c-button>
            </template>
          </c-fullbox>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="组织机构代码" prop="juscod">
          <c-input v-model="model.juscod" placeholder="请输入组织机构代码" :disabled="isPageDisabled||ptytypflg"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="金融机构类型" prop="akkcom">
          <c-select dbCode="FINORG" :isShowKeyAndLabel="true" filterable clearable v-model="model.akkcom" placeholder="请输入金融机构类型" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-select>
        </el-form-item>
      </c-col>
      
      <c-col :span="24">
        <el-form-item label="客户经理" prop="accusr">
          <c-input v-model="model.accusr" placeholder="请输入客户经理" :disabled="isPageDisabled||ptytypflg"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="支行代码" prop="branchcode">
          <el-select
            :remote="true"
            :default-first-option="true"
            v-model="model.branchcode" clearable filterable
            placeholder="请选择支行代码"
            :disabled="isPageDisabled||ptytypflg"
          >
            <el-option v-for="item in branchlist" :key="item.branch" :label="item.branch +' - '+ item.bchname"
                      :value="item.branch">
              <span style="float: left">{{ item.branch }} - {{ item.bchname }}</span>
            </el-option>
           </el-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="交易主体" prop="trnman">
          <c-select v-model="model.trnman" style="width: 100%" placeholder="请选择交易主体" dbCode="TRNMAN" :disabled="isPageDisabled||ptytypflg"> </c-select>
        </el-form-item>
      </c-col>
      <!-- <c-col :span="24">
        <el-form-item label="贸金客户类别" prop="crmtyp">
          <c-select v-model="model.crmtyp" style="width: 100%" placeholder="请选择贸金客户类别" dbCode="CRMTYP" :disabled="isPageDisabled||ptytypflg"> </c-select>
        </el-form-item>
      </c-col> -->
      
      <!-- <c-col :span="24">
        <el-form-item label="代理行关联客户号" prop="trnman">
          <c-select v-model="model.trnman" style="width: 100%" placeholder="请选择代理行关联客户号" dbCode="TRNMAN" :disabled="ptytypflg"> </c-select>
        </el-form-item>
      </c-col> -->
      <c-col :span="24">
        <el-form-item label="企业海关十位制编码" prop="bngcod" v-if="(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313')">
          <c-input :disabled="isPageDisabled" v-model="model.bngcod" placeholder="请输入企业海关十位制编码"> </c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="24">
        <el-form-item label="客户类型" prop="idtype">
          <c-select v-model="model.idtype" style="width: 100%" placeholder="请选择客户类型" dbCode="IDTYPE" :filterKey="['1','2']" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="客户英文名称" prop="nam">
          <c-input v-model="model.nam" placeholder="请输入客户名称" :disabled="isPageDisabled||opertype == 'edit'"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24" v-if="model.ptytyp != 'H'">
        <el-form-item label="客户中文名称" prop="nam1">
          <c-input v-model="model.nam1" placeholder="请输入客户中文名称" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24" v-if="model.ptytyp == 'H'">
        <el-form-item label="客户中文名称" prop="nam">
          <c-input v-model="model.nam" placeholder="请输入客户中文名称" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="统一社会信用代码" prop="idcode">
          <c-input v-model="model.idcode" placeholder="请输入统一社会信用代码" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="企业规模(国标)" prop="qygmgb">
          <c-select v-model="model.qygmgb" style="width: 100%" placeholder="请选择企业规模(国标)" dbCode="SIZTXT" :disabled="isPageDisabled||ptytypflg || opertype == 'edit'"> </c-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="客户等级" prop="sla">
          <c-select v-model="model.sla" style="width: 100%" placeholder="请选择客户等级" dbCode="SLATXT" :disabled="isPageDisabled||ptytypflg"> </c-select>
        </el-form-item>
      </c-col>
      <!-- <c-col :span="24">
        <el-form-item label="客户状态标志认定日期" prop="khbsdt">
          <c-date-picker v-model="model.khbsdt" placeholder="请选择客户状态标志认定日期" value-format="yyyy-MM-dd" :disabled="isPageDisabled||ptytypflg"> </c-date-picker>
        </el-form-item>
      </c-col> -->
      <!-- <c-col :span="24">
        <el-form-item label="国内地区编码" prop="discod">
          <c-input v-model="model.discod" placeholder="请输入国内地区编码"> </c-input>
        </el-form-item>
      </c-col> -->
      <c-col :span="24">
        <el-form-item label="代理行账户标志" prop="hbkaccflg" v-if="(!(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313'))">
          <c-radio-group v-model="model.hbkaccflg" :disabled="isPageDisabled||ptytypflg">
              <el-radio label="Y">是</el-radio>
              <el-radio label="">否</el-radio>
            </c-radio-group>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="代理行名称" prop="ecifnam1" v-if="(!(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313'))">
          <c-input v-model="model.ecifnam1" placeholder="请输入名称" :disabled="isPageDisabled||ptytypflg"> </c-input>
        </el-form-item>
      </c-col>

      <c-col :span="24">
        <el-form-item label="流动资金贷款利率档次" prop="cunqii" v-if="(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313')">
          <c-select :disabled="isPageDisabled" v-model="model.cunqii" style="width: 100%" placeholder="请选择流动资金贷款利率档次" dbCode="CUNQII" :isShowKeyAndLabel="true" filterable> </c-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="上浮比率" prop="bilvvv" v-if="(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313')">
          <c-input-number :disabled="isPageDisabled" v-model="model.bilvvv" placeholder="请输入上浮比率"> </c-input-number>
        </el-form-item>
      </c-col>

      <c-col :span="24">
        <el-form-item label="特殊区域标志" prop="speeco" v-if="(model.ptytyp=='C'&&model.idtype=='2'&&model.akkcom=='10313')">
          <c-radio-group :disabled="isPageDisabled" v-model="model.speeco">
            <el-radio label="Y">是</el-radio>
            <el-radio label="">否</el-radio>
          </c-radio-group>
        </el-form-item>
      </c-col>

    </c-col>
  </div>
</template>

<script>
import codes from "~/config/CodeTable";
import { getPtyInfoByEcif0905 } from "~/service/manage/ptyn.js";
import { getPtyListInfoByEcif0904 } from "~/service/manage/ptyn.js";
import Api from "~/service/Api"

export default {
  name: "",
  props: ["model", "type", "branchlist"],
  data() {
    return {
      ptyExistsFlg:false,
      oldPtyextkey:"",
      oldBranchList:[],
      chgBranchList:[],
      bchAuthMod:{
        cod_:"",
        msg_:"",
        zFlg:"N",
        zMsg:"",
        zBchList:[],
        zBchkeyList:[],
        zBranchList:[],

        xFlg:"N",
        xMsg:"",
        xBchList:[],
        xBchkeyList:[],
        xBranchList:[],
      },


    };
  },
  inject: ['opertype'],
  computed: {
    ptytypflg: function() {
      if (this.model.ptytyp == 'H') {
        return true
      } else {
        return false
      }
    },

    isPageDisabled() {
      if (this.type=="info") {
        return true
      }else if (this.type=="edit"){
        if (this.$store.state.UserContext.currentOrg.level == 0 || this.$store.state.UserContext.currentOrg.level == '0'){
          //总行
          if ((this.model.ptytyp=='C' && this.model.idtype=='2' && this.model.akkcom=='10313'  )){
            //财务公司
            return true
          }else{
            //非财务公司
            return false
          }
        }else{
          //分行
          if ((this.model.ptytyp=='C' && this.model.idtype=='2' && this.model.akkcom=='10313'  )){
            //财务公司
            if (this.$route.query.zFlg == "Y"){
              return false
            }else{
              return true
            }
          }else{
            //非财务公司
            return true
          }
        }
      }else if (this.type=="add"){
        return false
      }
      return false
    },


    isChaiwu (){
      let flg=false
      console.log("~~~~~~~~~~~~~~~~~~dbipty---index",this.model)
      if (this.model.ptytyp=='C' && this.model.idtype=='2' && this.model.akkcom=='10313'  ){
        flg = true
      }
      return flg
    },
    isZhongHang(){
      let zhongFlg=false
      let currentOrg = this.$store.state.UserContext.currentOrg
      if (currentOrg) {
        if (currentOrg.level == '0' || currentOrg.level == 0) {
          zhongFlg=true
        }
      }
      return zhongFlg
    },

  },
  mounted(){
    this.oldBranchList=this.branchlist
    this.chgBranchList=this.branchlist
    if (this.type=="add"){
      
        let zhFlg="N"
        let currentOrg = this.$store.state.UserContext.currentOrg
        if (currentOrg) {
          if (currentOrg.level == '0' || currentOrg.level == 0) {
            zhFlg="Y"
          }
        }
        if (zhFlg=="Y"){
         if (this.model.branchcode == "" ||this.model.branchcode == null){
         this.model.branchcode ="1000"
         this.model.bchkeycode ="1000"
         }
        }else{
          this.model.branchcode =null
          this.model.bchkeycode =null
        }
      
    }
    

  },
  methods: {
    
    getCodesByKey(key) {
      return codes[key] || [];
    },

    async baseInfo(extkey) {
      let zhFlg="N"
      let currentOrg = this.$store.state.UserContext.currentOrg
      if (currentOrg) {
        if (currentOrg.level == '0' || currentOrg.level == 0) {
          zhFlg="Y"
        }
      }
      
      if (this.model.ptytyp =='B'){
        console.log ("call0904--")
        let req = {
          swifno: this.model.extkey
        };

        await getPtyListInfoByEcif0904(req).then(res => {
          if(res.respCode == SUCCESS) {
            if (res.data.cod =="Y"){
              this.$notify.success({
            	title: '成功:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
            }else {
              this.$notify.warning({
            	title: '提示:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
              return
            }
            console.log("res---->",res)
            this.model.ecifno = res.data.res[0].orgno
            this.model.ecifjuscod = res.data.res[0].nationalorgcode
            this.model.ecifnam1 = res.data.res[0].orglegalname
            this.model.juscod = res.data.res[0].nationalorgcode

            if (this.model.ecifno!=='' ||this.model.ecifno !==null){
              this.baseECIFInfo(this.model.ecifno)
            }
          } else {
            this.$notify.error(res.respMsg) 
          }
        })
      }else if (this.model.ptytyp =='C'){
        console.log ("getPtyInfoByEcif0905--")
        
        await getPtyInfoByEcif0905({extkey}).then((res => {
          if(res.respCode == "AAAAAA") {
            if (res.data.cod =="Y"){
              this.$notify.success({
            	title: '成功:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
            }else {
              this.$notify.warning({
            	title: '提示:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
              return
            }
            
            this.model.idtype = res.data.res.idtype
            this.model.nam1 = res.data.res.nam1
            this.model.juscod = res.data.res.juscod
            this.model.idcode = res.data.res.idcode
            this.model.akkcom = res.data.res.akkcom
            this.model.qygmgb = res.data.res.qygmgb

            

          } else {
            this.$notify.error({
            	title: '错误:',
              message: res.respMsg,
              dangerouslyUseHTMLString: true 
              });
          }
        }))

        

        //分行加财务公司校验主协办权限,且设置可选机构
        //2.判断主办权限,没有主办权限则中断,并按主协办机构设置之后代码列表
          if (zhFlg!="Y" && this.model.ptytyp=="C" && this.model.idtype=="2" && this.model.akkcom=="10313" ){
            console.log("33333333333333")
            await this.checkZXbanForExtkeyByEcif0930(extkey,"Z")
            if (this.bchAuthMod.zFlg != "Y"){

              //this.branchlist=this.oldBranchList
              this.chgBranchList=this.bchAuthMod.zBranchList
              this.branchlist=this.chgBranchList
              console.log("~~~!Y")
              console.log("~~~!Y",this.branchlist)
              return 0;
            }else{
              if (this.bchAuthMod.zBranchList.length>0){
                this.chgBranchList=this.bchAuthMod.zBranchList
                this.branchlist=this.chgBranchList
                console.log("~~~Y")
                console.log("~~~Y",this.branchlist)
              }
            }
            }
      }
    },


    baseECIFInfo(ecifno) {
      console.log(ecifno.length)
      if (ecifno.length != 10) {
        this.$message.info("ECIF客户号需输入十位")
      } else {
        getPtyInfoByEcif0905({"extkey": ecifno}).then((res) => {
          if(res.respCode == "AAAAAA") {
            if (res.data.cod =="Y"){
              this.$notify.success({
            	title: '成功:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
            }else {
              this.$notify.warning({
            	title: '提示:',
              message: res.data.msg,
              dangerouslyUseHTMLString: true 
              });
            }
            
            this.model.idtype = res.data.res.idtype
            this.model.nam1 = res.data.res.nam1
            this.model.juscod = res.data.res.juscod
            this.model.idcode = res.data.res.idcode
            this.model.akkcom = res.data.res.akkcom
            this.model.qygmgb = res.data.res.qygmgb
          } else {
            
            this.$notify.error({
            	title: '错误:',
              message: res.respMsg,
              dangerouslyUseHTMLString: true 
              });
          }
        })
      }
    },

    //判断客户是否已存在
    async checkPtyExistsByExtkey(extkey){
      //判断客户号已存在
      let data={"extkey": extkey};
      await Api.post('/manager/pty/checkPtyExistsByExtkey',data).then(res => {
        if (res.respCode == SUCCESS) {
          this.ptyExistsFlg=res.data.exists;
        }
      });
    },

    //判断当前机构在ECIF是否有客户的主办权限
    async checkZXbanForExtkeyByEcif0930(extkey,authTyp){
      
      let data={
        "orgBch":this.$store.state.UserContext.currentOrg.departmentNumber,
        "orgNo": extkey
        };
      await Api.post('/manager/pty/checkZXbanForExtkeyByEcif0930',data).then(res => {
        if (res.respCode == SUCCESS) {
          this.bchAuthMod.cod_ = res.data.cod
          this.bchAuthMod.msg_ = res.data.msg

          this.bchAuthMod.zFlg = res.data.zFlg
          this.bchAuthMod.zMsg = res.data.zMsg
          this.bchAuthMod.zBchList = res.data.zBchList
          this.bchAuthMod.zBchkeyList = res.data.zBchkeyList
          this.bchAuthMod.zBranchList = res.data.zBranchList

          this.bchAuthMod.xFlg = res.data.xFlg
          this.bchAuthMod.xMsg = res.data.xMsg
          this.bchAuthMod.xBchList = res.data.xBchList
          this.bchAuthMod.xBchkeyList = res.data.xBchkeyList
          this.bchAuthMod.xBranchList = res.data.xBranchList

          if (this.bchAuthMod.zFlg  == 'Y'){
            this.$notify.success({
            	title: '成功:',
              message: this.bchAuthMod.msg_+"<br/>"+this.bchAuthMod.zMsg,
              dangerouslyUseHTMLString: true 
            });
          }
          if ( this.bchAuthMod.zFlg !='Y' && this.bchAuthMod.cod_ != 'E') {
            this.$notify.warning({
            	title: '提示:',
              message: this.bchAuthMod.msg_+"<br/>"+this.bchAuthMod.zMsg,
              dangerouslyUseHTMLString: true 
            });
          }else if (this.bchAuthMod.cod_ == 'E' ){
            this.$notify.error({
            	title: '错误:',
            	message: this.bchAuthMod.msg_+"<br/>"+this.bchAuthMod.zMsg,
              dangerouslyUseHTMLString: true 
            });
          }
        }
      });
    },
    
  },
};
</script>

<style>
</style>