<template>
  <div>
    <c-page :title="title">
      <el-form ref="modelForm" label-width="160px" size="small" label-position="right"
        :model="model" :rules="Pattern" :validate-on-rule-change="false">
        <c-tabs v-model="tabVal" ref="elment" type="card">
          <el-tab-pane label="基本信息" name="pty">
            <c-content>
              <m-pty-N :model="model" title="dbapty" type="add" ref="pty" :branchlist="branchlist"></m-pty-N>
            </c-content>
          </el-tab-pane>
        </c-tabs>
      </el-form>
      <div style="text-align: center">
        <c-button type="primary" style="margin-right: 10px" @click="commitAdd" v-if="type === 'add'">提 交</c-button>
        <c-button type="primary" @click="goBack">返 回</c-button>
      </div>
    </c-page>
    <el-dialog :title="'主地址信息'" :visible.sync="adrFlg" :modal-append-to-body="false" :close-on-click-modal="false" width="70%">
        <m-adr ref="adr" :adr="mainAdr" :model="model" :operate="operate" @changeObjkey="changeObjkey" :bchlist="bchlist" :branchlist="branchlist"></m-adr>
        <span slot="footer" class="dialog-footer">
          <button
            class="el-button el-button--default el-button--small"
            style="margin-left: 0"
            size="small"
            :disabled="false"
            @click.prevent="cancel"
          >
            <span>取 消</span>
          </button>
          <c-button type="primary" @click="addMainAdr"
            >保 存</c-button
          >
        </span>
    </el-dialog>
  </div>
</template>

<script>
import Utils from "~/utils";
import PtyInfo from "~/page/Statics/Pty-N/Dbipty-N/PtyInfo";
import Ptyn,{ Pattern1, Pattern2 } from "../Dbipty-N/Ptyn.js";
import { add, getBranchPn,  getBchcodlist, getBchlist, getBch } from "~/service/manage/ptyn.js";
import AdrInfo from "../Dbipty-N/AdrInfo";
import Adrn from "../Dbipty-N/Adrn.js";
import { add as addMainAdr } from "~/service/manage/adr.js";
import Api from "~/service/Api";

export default {
  name: "StaticsDbaptyN",
	components: {
    "m-pty-N": PtyInfo,
    "m-adr": AdrInfo,
  },
  provide() {
    return {
      root: this,
      opertype: "add",
    };
  },
  props: {
    type: {
			type: String,
			default: "add"
		},
    title: {
			type: String,
			default: "dbapty"
		}
  },
  data() {
    return {
      tabVal: "pty",
      model: new Ptyn().data,
      bchlist: [],
      branchlist :[],
      adrFlg: false,
      mainAdr: new Adrn().data,
      operate: "add",
      zhFlg:"N"
    };
  },
  computed: {
    Pattern: function(){
      if (this.model.ptytyp == 'H') {
        return Pattern2
      } else {
        return Pattern1
      }
    }
  },
  created() {
    let currentOrg = this.$store.state.UserContext.currentOrg
    if (currentOrg) {
      if (currentOrg.level == '0' || currentOrg.level == 0) {
        this.zhFlg="Y"
      }
    }
    //this.getZonghang()
    this.getBranch()
    this.getBch()
  },
  mounted() {
  },
  methods: {
    getZonghang() {
      getBranchPn().then((res) => {
        if(res.respCode == "AAAAAA") {
          this.bchlist = res.data
        }
      })
    },

    //获取支行(分支行)列表
    getBranch() {
      if (this.zhFlg=="Y"){
        getBranchPn().then((res) => {
        if(res.respCode == "AAAAAA") {
          this.branchlist = res.data
        }
        })
      }else{
        getBchcodlist().then((res) => {
        if (res.respCode == "AAAAAA") {
          this.$set(this, "branchlist", res.data)
        }
        })
      }


      
    },
    //获取记账机构列表
    getBch() {
      if (this.zhFlg=="Y"){
        getBranchPn().then((res) => {
        if(res.respCode == "AAAAAA") {
          this.bchlist = res.data
        }
        })
      }else{
        getBchlist().then((res) => {
        if (res.respCode == "AAAAAA") {
          this.$set(this, "bchlist", res.data)
        }
      })
      }
    },


    commitAdd() {
      let zhFlg="N"
      let currentOrg = this.$store.state.UserContext.currentOrg
      if (currentOrg) {
        if (currentOrg.level == '0' || currentOrg.level == 0) {
          zhFlg="Y"
        }
      }
      if (zhFlg=="Y" && this.model.ptytyp=="C" && this.model.idtype=="2" && this.model.akkcom=="10313" ){
        this.$notify.warning({
            title: '提示',
            message: '总行无权限维护财务公司信息!'
          });
          return 0
      }

      if (zhFlg!="Y" && !(this.model.ptytyp=="C" && this.model.idtype=="2" && this.model.akkcom=="10313") ){
        this.$notify.warning({
            title: '提示',
            message: '分行无权限维护非财务公司信息!'
          });
          return 0
      }

      console.log("--",this.$refs.pty.bchAuthMod)
      let  bchAuthMod=this.$refs.pty.bchAuthMod
      if ((this.model.ptytyp=="C" && this.model.idtype=="2" && this.model.akkcom=="10313") ){
        if ( bchAuthMod.zFlg !="Y" ){
          let zBchkeyMsg="" 
          if (bchAuthMod.zBchkeyList !=null && bchAuthMod.zBchkeyList.length>0){
            for (let j of bchAuthMod.zBchkeyList){
              let tmp=j.branch +" - "+j.bchname
              zBchkeyMsg=zBchkeyMsg+"<br/>"+tmp
            }
          }

          this.$notify.warning({
              	title: '提示:',
                message: "无主办权限! 主办机构:"+zBchkeyMsg,
                dangerouslyUseHTMLString: true 
              });
          return 0;

        }
      }


      this.$refs.modelForm.validate((validated) => {
        if (validated) {
          
          this.mainAdr.ptyextkey = this.model.extkey;
          this.mainAdr.nam = this.model.nam1;
          this.mainAdr.ptynam = this.model.nam;
          this.mainAdr.usg = 'MAA';
          this.mainAdr.branchcode = this.model.branchcode;

          this.mainAdr.adr1=this.model.adr1;
          this.mainAdr.adr2=this.model.adr2;
          this.mainAdr.adr3=this.model.adr3;
          this.mainAdr.adr4=this.model.adr4;
          this.mainAdr.loccty=this.model.loccty;
          this.mainAdr.pobzip=this.model.pobzip;
          this.mainAdr.ptytyp=this.model.ptytyp;

          console.log( this.mainAdr.adr1)

          let branch = this.model.branchcode;
          //获取记账机构
          Api.post(`/manager/pty/getBch`, {"branchcode": branch}).then((res) => {
            if(res.respCode == "AAAAAA") {
              if (res.data == null || res.data.branch == null ){
                this.mainAdr.bchkeycode = null
                if(branch == "1000"){
                  this.mainAdr.bchkeycode = "1000"
                }
              }else{
                this.mainAdr.bchkeycode = res.data.branch;
              }
              
            }else{
              this.$message.info(res.respMsg)
            }
          })


          this.adrFlg = true;
        } else {
          this.$notify.error({
            title: '失败',
            message: '请检查必输项!'
          });
        }
      });
    },
    addMainAdr() {
      this.$refs.adr.$refs.modelForm.validate((validated) => {
        if (validated) {
          add(this.model)
            .then((res) => {
              if (res.respCode == "AAAAAA") {
                this.mainAdr.ptyinr = res.data.inr;
                addMainAdr(this.mainAdr).then((res) => {
                  if (res.respCode == "AAAAAA") {
                    this.adrFlg = false;
                    this.$store.commit('delTagsArry', this.$route.path)
                    this.$router.push( {name: 'StaticsDbeptyN', query: { inr: this.mainAdr.ptyinr}} )
                    this.$notify.success("保存成功!");
                  } else {
                    this.$notify.error(res.respMsg)
                  }
                })
              } else {
                this.$notify.error(res.respMsg)
              }
            })
            .catch((err) => {
              this.$notify.error("新增客户失败");
            });
          
        } else {
          this.$notify.error({
            title: '失败',
            message: '请检查必输项!'
          });
        }
      })
    },
    changeObjkey (val) {
      this.$set(this.mainAdr, 'ptaextkey', val)
    },
    /**
     * update 是否更新dblpty的查询列表
     */
    // goBack(update) {
    //   this.$store.commit('delTagsArry', this.$route.path);
    //   this.$router.history.push('/business/dblptyn')
    //   // this.$store.dispatch("TagsView/delView", this.$route);
    //   // this.$router.push({ name: "StaticsDblptyN", params: { update } });
    // },
    goBack(){
      this.$confirm(cancelMsg, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
				})
				.then(()=>{
					this.$store.commit('delTagsArry', this.$route.path);
          this.$router.history.push('/business/dblptyn')
				})
				.catch(() => {
            this.$message({
                type: "info",
                message: "已取消",
            });
        });
    },
    cancel() {
      //this.$store.commit('delTagsArry', this.$route.path)
      //this.$router.go(-1)
      this.adrFlg=false
    },
  },

};
</script>

<style>
</style>