import {
	filterCustomerManager,
	handleTree,
	isChinese
} from '@/utils/util'

const columnProps = [
  {
    prop : "name",
    label : "姓名",
  },
  {
    prop : "telephone",
    label : "电话",
  },
  {
    prop : "clueOperationName",
    label : "运营",
  },
  {
    prop : "appName",
    label : "来源",
  },
  {
    prop : "deptName",
    label : "归属",
  }
]

export default {
	data() {
		return {

			recognitionContent: "",

			defaultRegion: ['广东省', '广州市', '番禺区'],

			listData: [],
			deptList: [],
			dictCascadeData: [],
			genderTypeCodeDict: [],
			originalDictCascadeData : [],
			originaDeptList : [],

			title: "新建线索",

			rules: {
				name: {
					type: 'string',
					required: true,
					message: '请输入姓名',
					trigger: ['blur', 'change']
				},
				telephone: {
					type: 'string',
					required: true,
					message: '请输入电话',
					trigger: ['blur', 'change']
				},
				clueOperationName: {
					type: 'string',
					required: true,
					message: '请选择运营人',
					trigger: ['blur', 'change']
				},
				appName: {
					type: 'string',
					required: true,
					message: '请选择来源',
					trigger: ['blur', 'change']
				},
				deptName: {
					type: 'string',
					required: true,
					message: '请选择归属机构',
					trigger: ['blur', 'change']
				},
			},
			form: {
				manualAddressCode: '',
				manualProvince: '',
				manualCity: '',
				manualArea: '',
				name: '',
				telephone: '',
				genderTypeCode: '',
				age: '',
				weixin: '',
				address: '',
				remark: '',
				qq: '',
				email: '',
				adId: '',
				appName: undefined,
				appNameLabel: undefined,
				province: undefined,
				city: undefined,
				area: undefined,
				clueOperationId: undefined,
				clueOperationName: undefined,
				clueOwnerId: undefined,
				clueOwnerName: undefined,
				deptName: undefined,
				deptId: undefined,
			}
		}
	},
	methods: {
		async handleRecognition() {
			// 电话：15099998888
			// 姓名：卡地亚萧邦
			// 来源：广点通
			// 运营：yxx
			// 归属：集团总部
			const splitStr = "：";
			// 避免用错符号，直接转换一下
			const lines = this.recognitionContent.replace(/:/g, splitStr).split('\n');
			lines.forEach(line => {
				const [key, value] = line.split(splitStr);
				const column = columnProps.find(v => key.trim() === v.label);
				if (column) {
					const {
						prop
					} = column;
					this.form[prop] = value?.trim();
				}
			});

			// 来源 归属机构 运营人 需要找id
			const {
				appName,
				deptName,
				clueOperationName
			} = this.form;

			if (appName) {
				let dictList = this.originalDictCascadeData;
				let appNameDict = dictList.find(v => v.name === appName);
				if (appNameDict) {
					this.form.appName = appNameDict.id;
					this.form.appNameLabel = appNameDict.name;
				} else {
					uni.$u.toast(appName + "当前来源未配置，请重新选择");
				}
			}
			
			if (deptName) {
				let deptList = this.originaDeptList;
				let dept = deptList.find(v => v.deptName === deptName);
				if (dept) {
					this.form.deptId = dept.deptId;
				} else {
					uni.$u.toast(deptName + "找不到对应的机构");
				}
			}
			
			if(clueOperationName){
			  // 没有现成的数据 请求接口
			  const { data } =  await uni.$u.api.getIdByName({ clueOperationName : clueOperationName});
			  const { clueOperationId } = data;
			  this.form.clueOperationId = clueOperationId;
			}
		},
		// 获取选择的地区
		handleGetRegion(region) {
			const {
				0: provinceData,
				1: cityData,
				2: areaData
			} = region;
			this.form.manualProvince = provinceData.name;
			this.form.manualCity = cityData.name;
			this.form.manualArea = areaData.name;
			this.form.manualAddressCode = provinceData.name + cityData.name + areaData.name;
		},
		handleShowclueDept() {
			this.$refs.dept._show();
		},
		handleShowclueAppName() {
			this.$refs.appName._show();
		},
		handleShowclueOperation() {
			this.$refs.clueOperation._show();
		},
		handleShowclueOwner() {
			this.$refs.clueOwner._show();
		},
		appNameSeletchang(ids, names) {
			this.form.appName = ids[0];
			this.form.appNameLabel = names[0];
		},
		clueOperationSeletchang(ids, names) {
			this.form.clueOperationId = ids[0];
			this.form.clueOperationName = names[0];
		},
		clueOwnerSeletchang(ids, names) {
			this.form.clueOwnerId = ids[0];
			this.form.clueOwnerName = names[0];
		},
		deptSeletchang(ids, names) {
			this.form.deptId = ids[0];
			this.form.deptName = names[0];
		},
		// 返回
		handleClose() {
			this.$refs.form.resetFields();
			this.form.manualProvince = undefined;
			this.form.manualCity = undefined;
			this.form.manualArea = undefined;
			this.form.manualAddressCode = undefined;
		},
		// 保存
		handleNavSaveClick() {
			this.$refs.form.validate().then(async () => {
				await uni.$u.api.addClueMainInfo(this.form);
				uni.$u.toast("保存成功");
				this.handleClose();
			})
		},
		async handleOption() {
			// 获取人员
			uni.$u.api.getDeptCustomerByOrg().then(res => {
				this.listData = filterCustomerManager(res.data);
			});

			uni.$u.api.getDictCascadeData().then(({
				data
			}) => {
				this.originalDictCascadeData = data;
				this.dictCascadeData = handleTree(data, 'id');
			});

			uni.$u.api.selectAllDeptList({
				isDept: 2
			}).then(({
				data
			}) => {
				this.originaDeptList = data;
				this.deptList = handleTree(data, 'deptId');
			});

			this.$getDicts('sys_user_sex').then(res => {
				this.genderTypeCodeDict = res;
			});
		},

	},
	onLoad(option) {
		this.handleOption();
	}
}