<script setup>
import { reactive, ref, watch } from 'vue'
import { ElMessageBox } from 'element-plus'
import { customInterence } from '@/api/api.js'
import{province_sorce,city_sorce} from '@/utils/distpicker';

const props = defineProps({
  form:{
    type: Object
  }
})
const emit = defineEmits(['cancel'])

const formRule = ref({
    name:[{ required: true, message: '请填写客户名称', trigger: 'blur' }],
    CreditCode:[{ required: true, message: '请填写社会信用码', trigger: 'blur' }],
    City:[{ required: true, message: '请选择地址', trigger: 'change' }],
    Source:[{ required: true, message: '请选择客户来源', trigger: 'change' }],
    IndustryId:[{ required: true, message: '请选择行业', trigger: 'change' }],
})

const formData = reactive({})

/* 获取客户来源数据 */
const fromArr = ref([])//来源选项数据
function getCustomerSourceList(){
  customInterence.customerSourceList({}).then(res=>{
    if(res.Ret===200){
      fromArr.value=res.Data.List
    }
  })
}
getCustomerSourceList()

/* 获取行业选项数据 */
const tradeArr = ref([])//行业选项数据
function getIndustry() {
  let type = localStorage.getItem('RoleType') || '';
  customInterence.getindustry({
    Classify:type
  }).then(res => {
    if(res.Ret === 200) {
      tradeArr.value = res.Data.List || [];
    }
  })
}
getIndustry()


function cancelHandle(refresh=false){
  emit('cancel',refresh)
}

//选择地区
function selectRegion(e) {
  formData.value.Province =e.province.value=='省'?'':e.province.value;
  formData.value.City = e.city.value=='市'?'':e.city.value;
}

const formRef = ref(null)
async function handleConfirm(){
    formRef.value.validate(async (valid) => {
        if(valid){
            let IndustryName=''
            tradeArr.value.forEach(item=>{
                if(item.IndustryId==formData.IndustryId){
                    IndustryName=item.IndustryName
                }
            })
            let res=await customInterence.completeInfo({
                CompanyId:formData.CompanyId,
                CompanyName:formData.name,
                CreditCode:formData.CreditCode,
                Province:formData.Province,
                City:formData.City,
                IndustryId:Number(formData.IndustryId)||0,
                IndustryName:IndustryName,
                Source:formData.Source
            })
            if(res.Ret===200){
                cancelHandle(true)
                ElMessageBox('信息补全成功，请重新点击相应操作','提示', {
                    confirmButtonText: '知道了',
                    showCancelButton:false,
                    type: 'success'
                })
            }
        }
    })
}

watch(
  () => props.form,
  (nval) => {
    formData.value=nval
      if(nval.flag){
          //跨部门
          formRule.value.Source[0].required=false
          formRule.value.IndustryId[0].required=false
      }
      //海外去掉地址
      if(nval.RegionType=='海外'){
          formRule.value.City[0].required=false
      }
  }
)

</script>
<template>
<div>
  <el-dialog
	:model-value="form.show"
	:close-on-click-modal="false"
	:modal-append-to-body='false'
	@before-close="cancelHandle(false)"
	class="self_dialog"
	center
  draggable
	width="600px">
    <template #header>
      <div>
          <span style="font-size:16px;">请先补全客户基本信息</span>
      </div>
    </template>

    <el-form ref="formRef" :model="formData" :rules="formRule" label-width="100px">
        <el-form-item label="客户全称" prop="name" >
            <el-input  v-model="formData.name" style="width:400px" :disabled="formData.nameDisable"></el-input>
        </el-form-item>
        <el-form-item label="社会信用码" prop="CreditCode">
            <el-input v-model="formData.CreditCode" placeholder="请填写社会信用码" style="width:400px" :disabled="formData.CreditCodeDisable"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" prop="City" v-if="formData.RegionType!='海外'" >
            <v-distpicker :province-source="province_sorce"
            :city-source="city_sorce"  :disabled="formData.CityDisable" :province="formData.Province" :city="formData.City" hide-area  @selected="selectRegion"></v-distpicker>
        </el-form-item>
        <div v-if="!formData.flag">
            <el-form-item label="客户来源" prop="Source">
                <el-select v-model="formData.Source" :disabled="formData.SourceDisable" placeholder="请选择客户来源" style="width:400px">
                    <el-option
                    v-for="item in fromArr"
                    :key="item.SourceId"
                    :label="item.SourceName"
                    :value="item.SourceName">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="行业" prop="IndustryId">
                <el-select v-model="formData.IndustryId" :disabled="formData.IndustryIdDisable" placeholder="请选择行业" style="width:400px">
                    <el-option
                    v-for="item in tradeArr"
                    :key="item"
                    :label="item.IndustryName"
                    :value="item.IndustryId">
                    </el-option>
                </el-select>
            </el-form-item>
        </div>
        <div style="text-align:center;margin:30px 0">
            <el-button type="primary" @click="handleConfirm">确定</el-button>
            <el-button type="primary" plain @click="cancelHandle(false)">取消</el-button>
        </div>
    </el-form>
  </el-dialog>
</div>
</template>
<style scoped lang="scss">
:deep(.distpicker-address-wrapper) select {
	width: 198px;
}
</style>