<template>
    <div>
      <el-dialog
        class="popups"
        :visible.sync="dialogVisible"
        width="534px"
        :close-on-click-modal="false"
        @close="resetForm('ruleForm')"
      >
        <div class="poptitle">
          <img src="@/assets/images/economies/icon.png">
          <span>添加客户</span>
        </div>
        <el-form class="popform j" :model="queryParam" :rules="rules" ref="ruleForm" label-width="130px">
          <el-form-item label="企业名称：" class="row"  prop="companyName">
            <el-input type="text" placeholder="请输入" v-model="queryParam.companyName" @input="getCompany(1)"></el-input>
            <div class="resultlist infinite-list" v-if="showlist" id="box" v-infinite-scroll="load" style="overflow:auto">
              <div class="infinite-list-item" v-for="(item,index) in companData" @click="selCompany(item)"><span  v-html="item.name" :key="companypage"></span></div>
            </div>
          </el-form-item>
          <el-form-item label="客户等级：" class="row">
            <el-select  placeholder="请选择" v-model="queryParam.customerLevel">
              <el-option v-for="(item,index) in customerLevel" :key="index" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="法定代表人：" class="row">
            <el-input type="text" placeholder="请输入" v-model="queryParam.legalPerson"></el-input>
          </el-form-item>
          <el-form-item label="注册资本（万）：" class="row">
            <el-input type="text" placeholder="请输入" v-model="queryParam.registerCapital" @input='number'></el-input>
          </el-form-item>
          <el-form-item label="企业注册地：" class="row">
            <el-cascader :props="props" @change="getAddr($event)" ref="address" v-model="queryParam.address" :options="addressList" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="统一社会信用代码：" class="row">
            <el-input type="text" placeholder="请输入" v-model="queryParam.creditCode"></el-input>
          </el-form-item>
          <div class="popbot">
            <div class="wordprimary" @click="toct">前往城投平台寻找客户线索></div>
            <div class="btn btn_cancel h32" @click="resetForm('ruleForm')">返回</div>
            <div class="btn btn_primary h32" @click="submitForm('ruleForm')">添加</div>
          </div>
        </el-form>
      </el-dialog>
    </div>
</template>

<script>
  import { getDictType, getEnterprise } from '@/api/main'
  import { addCustomer, getCustomerList, importData } from '@/api/custom/custom'
  import prvinceTree from '@/assets/json/provinceTree'
  export default {
    name: '',
    components: {
    },
    props: {
      data:{}
    },
    data() {
      return {
        props:{ checkStrictly: true, expandTrigger: 'hover' },
        dialogVisible: this.data.open,
        //添加客户
        queryParam:{
          companyId:'',//jsk企业id
          companyName:'',//客户名称（企业名称
          customerLevel:'',//客户等级
          legalPerson:'',//法定代表人
          registerCapital:'',//注册资本
          registerAddress:'',//企业注册地址
          creditCode:'',//社会统一信用代码
          address:'',//选择的地址
          provinceId:'',
          cityId:'',
          districtId:'',
        },
        rules:{
          companyName:[{ required: true, message: '请输入非空格字符！', trigger: 'blur' },]
        },
        showlist:false,
        customerLevel:[]
      }
    },
    created() {
      this.prvinceTree()
      this.getDictType()
      console.log(this.dialogVisible)
      console.log('1111111111')
    },
    computed: {},
    methods:{
      async prvinceTree() {
        this.addressList = prvinceTree;
        this.getadd(this.addressList)
      },
      getDictType(){
        //获取客户等级
        getDictType('customer_level_type').then(result=>{
          this.customerLevel = result.code == 200 ? result.data:[]
        })
      },
      open() {
        // this.dialogVisible = true;
      },
      resetForm(formName) {
        this.queryParam = {
          companyId:'',//jsk企业id
          companyName:'',//客户名称（企业名称
          customerLevel:'',//客户等级
          legalPerson:'',//法定代表人
          registerCapital:'',//注册资本
          registerAddress:'',//企业注册地址
          creditCode:'',//社会统一信用代码
          address:'',//选择的地址
          provinceId:'',
          cityId:'',
          districtId:'',
        },
          this.dialogVisible = false
        this.showlist = false
      },
      getAddr(obj){
        if(obj.length == 0){
          this.queryParam.provinceId = ''
          this.queryParam.cityId = ''
          this.queryParam.districtId = ''
          return false
        }
        let labelString = this.$refs.address.getCheckedNodes()[0].pathLabels.join("-");
        this.queryParam.registerAddress = labelString
        this.queryParam.provinceId = obj[0]
        this.queryParam.cityId = obj.length>=1 ? obj[1]:''
        this.queryParam.districtId = obj.length>=2 ? obj[2]:''
      },
      //获取建设库客户
      getCompany(value){
        this.queryParam.companyId = null
        if(value == 1){
          this.companData = []
          this.companypage = 1
        }
        if (this.queryParam.companyName.length>=2){
          let param = {
            keyword:this.queryParam.companyName,
            page:{
              limit:20,
              page:this.companypage
            }
          }
          getEnterprise(JSON.stringify(param)).then(result=>{
            if(result.code != 200){
              return false
            }
            if(result.data.list != null && result.data.list.length>0){
              this.isscroll = true
              if (this.companData.length===0) {
                this.companData = result.data.list
              } else {
                let arr2 = result.data.list
                arr2.unshift(2, 0);
                Array.prototype.splice.apply(this.companData, arr2);
              }
              if(this.companData.length===0) {
                this.showlist = false
              }else{
                this.showlist = true
              }
              this.companypage += 1
            }else{
              this.isscroll = false
            }
          })
        }
      },
      load(){
        if(this.isscroll){
          this.getCompany(2)
        }
      },
      selCompany(item){
        this.queryParam.companyId = item.jskEid
        this.queryParam.companyName = item.name.replace(/<[^>]+>/g, '')
        this.queryParam.legalPerson = item.legalPerson
        let registeredCapitalStr = ""
        if(item.registeredCapitalStr == "0" || item.registeredCapitalStr == 0){
          registeredCapitalStr = ''
        }else{
          registeredCapitalStr = item.registeredCapitalStr
        }
        this.queryParam.registerCapital = registeredCapitalStr
        this.queryParam.creditCode = item.creditCode
        this.queryParam.provinceId = item.provinceId
        this.queryParam.cityId = item.cityId
        this.queryParam.districtId = item.cityId

        let list = []
        if(item.provinceId != null && item.provinceId != "")
          list.push(item.provinceId)
        if(item.cityId != null && item.cityId != "")
          list.push(item.cityId)
        if(item.districtId != null && item.districtId != "")
          list.push(item.districtId)
        this.$nextTick(()=>{
          this.queryParam.address = list
          let _this = this
          setTimeout(function() {
            if(_this.$refs.address){
              _this.queryParam.registerAddress = _this.$refs.address.getCheckedNodes()[0].pathLabels.join("-")
            }
          },1000)
        })
        this.showlist = false
      },
      //添加客户
      submitForm(formName) {
        // if(this.queryParam.companyId=="" || this.queryParam.companyId == null){
        //   this.$message.error({message:'请选择企业！',showClose:true})
        //
        //   return;
        // }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addCustomer(this.queryParam).then(result=>{
              if(result.code == 200){
                this.$message.success('添加成功！')
                this.dialogVisible = false
                this.resetForm('ruleForm')
              }else{
                this.$message.error(result.msg)
              }
            })
          } else {
          }
        });
      },
      toct(){
        this.$router.push({path:'/macro/urban'})
      },
      //输入数字
      number(value){
        if(value == '')
          this.queryParam.registerCapital = value
        else
          this.queryParam.registerCapital = value.replace(/^\D*(\d*(?:\.\d{0,6})?).*$/g, '$1')//输入6位小数
      },
      //处理注册地数据
      getadd(row) {
        this.addrcallback(row,this.getadd)
      },
      addrcallback(row,callback){
        if(row){
          row.forEach(item => {
            item.value = item.id
            callback &&  callback(item.children)
          })
        }
      },
    },

  }
</script>

<style scoped>

</style>
