<!-- 麻醉/精神药品配制/领取申请单 -->
<template>
	<div>
		<div class="detail-container">
			<div class="detail-title"><img src="@/assets/images/detail-title.png">{{ formData.bdmc }}<img src="@/assets/images/detail-title.png" /></div>
			<div class="detail-content">
				<div class="content">
					<LineLabel label="template.gsp.MJYLQSQD.jbxx" />
					<div class="template-form-item">
						<BaseInfoFormPackage @select="onSelectBlur" @clickable="onClickSybh" ref="baseInfoRef"
							:formConfig="baseInfoFormConfig" :formData="formData" />
					</div>
					<BaseInfoFormPackage label="template.common.remark" ref="remarkRef" :formConfig="remarkConig"
						:formData="formData" />
				</div>
			</div>
		</div>
		<SelectReagentDialog type="MJYLQSQD" selectedCode="sn" title="template.gsp.MJYLQSQD.qxzsyhbm"
			:searchForm="searchForm" :columns="columns" @submit="onDialogSubmit" :listApi="public_studyList"
			ref="selectReagentDialogRef">
		</SelectReagentDialog>
	</div>
</template>

<script>
import BaseInfoFormPackage from "@/components/Template/BaseInfoFormPackage";
import LineLabel from "@/components/Template/LineLabel";
import templateMixin from "../../mixins/templateMixin";
import CustomTable from '@/components/Template/CustomTable.vue';
import TableOpertaion from "@/components/Template/operation/TableOpertaion.vue";
import SelectReagentDialog from "../../dialog/SelectReagentDialog.vue";
import { public_studyList } from '@/api/business/public/public';
import { listData } from "@/api/system/dict/data"

export default {
	name: "MJYLQSQD",
	dicts: ["business_gyjl", "business_gynd", "business_dwzs", "business_dwsl", "business_dwtz", "business_sywzmc"],
	components: { BaseInfoFormPackage, LineLabel, CustomTable, TableOpertaion, SelectReagentDialog },
	mixins: [templateMixin],
	props: {
		value: {
			type: {},
			default: () => { },
		},
		fillType: {
			type: String,
			default: 'preFill',
		},
	},
	watch: {
		value: {
			immediate: true,
			handler(v) {

			}
		},
		fillType: {
			immediate: true,
			handler(v) {
				console.log(v, "fillType")
			}
		},
	},
	computed: {
		// 备注表单配置
		remarkConig() {
			return [
				{
					type: "cellItem",
					config: {
						remark: {
							label: "",
							type: "textarea",
							fillType: "actFill",
							span: 1,
							placeholder: "template.common.remarkPlaceholder",
							maxlength: 1000,
							rows: 5
						}
					}
				}
			]
		},
		// 试验基本信息表单配置
		baseInfoFormConfig() {
			return [
				{
					type: "step",
					config: {
						syNo: {
							label: "template.gsp.MJYLQSQD.sybhhbmmc",
							type: "clickable",
							fillType: "actFill",
						},
						SD: {
							label: "template.gsp.MJYLQSQD.sd",
							type: "input",
							disabled: true,
							fillType: "actFill",
						},
						sywzmc: {
							label: "template.gsp.MJYLQSQD.sywzmc",
							type: "select",
							fillType: "actFill",
							options: this.getDictOptions('business_sywzmc'),
						},
						gyjl: {
							label: "template.gsp.MJYLQSQD.gyjl",
							type: "select",
							otherCode: "gyjlOther",
							fillType: "actFill",
							options: this.gyjlOptions,
						},
						xynd: {
							label: "template.gsp.MJYLQSQD.xynd",
							type: "select",
							otherCode: "xyndOther",
							fillType: "actFill",
							options: this.gyndOptions,
						},
						xyl: {
							label: "template.gsp.MJYLQSQD.xyl",
							type: "inputNumber",
							fillType: "actFill",
						},
						xysj: {
							label: "template.gsp.MJYLQSQD.xysj",
							type: "dateTime",
							fillType: "actFill",
						},
						yjghsj: {
							label: "template.gsp.MJYLQSQD.yjghsj",
							type: "dateTime",
							fillType: "actFill",
						},
						dwzs: {
							label: "template.gsp.MJYLQSQD.dwzs",
							type: "select",
							fillType: "actFill",
							otherCode: "dwzsOther",
							options: this.getDictOptions('business_dwzs'),
						},
						dwpjtz: {
							label: "template.gsp.MJYLQSQD.dwpjtz",
							type: "input",
							fillType: "actFill",
							subType: "select",
							subFillType: "actFill",
							subKey: "dwtzUnit",
							subOptions: this.getDictOptions('business_dwtz'),
						},
						dwsl: {
							label: "template.gsp.MJYLQSQD.dwsl",
							type: "input",
							fillType: "actFill",
							subType: "select",
							subFillType: "actFill",
							subKey: "dwslUnit",
							otherCode: "dwslOther",
							subOptions: this.getDictOptions('business_dwsl'),
						},

					}
				},
			]
		}
	},
	data() {
		return {
			public_studyList: public_studyList,
			searchForm: {
				name: {
					label: this.$t('page.business.study.study.name'),
				},
				sn: {
					label: this.$t('page.business.study.study.sn'),
				},
				leaderName: {
					label: this.$t('page.business.study.study.leader'),
				},
			},
			columns: [
				{
					prop: 'name',
					label: 'page.business.study.study.name',
				},
				{
					prop: 'sn',
					label: 'page.business.study.study.sn',
				},
				{
					prop: 'status',
					label: 'page.business.study.study.status',
				},
				{
					prop: 'leaderName',
					label: 'page.business.study.study.leader',
				},
				{
					prop: 'createTime',
					label: 'page.business.study.study.createDate',
				}
			],
			gyjlOptionsOld: [],
			gyndOptionsOld: [],
			gyjlOptions: [],
			gyndOptions: []
		};
	},
	created() {
		this.getGyjl()
		this.getGynd()
	},
	mounted() {
	},
	methods: {
		//初始化sywzmc，gyjl，gynd
		init(val) {
			if (val.key == 'sywzmc') {
				this.filterGyjl(val, false)
			}
			if (val.key == 'gyjl') {
				this.filterGynd(val, false)
			}
		},
		//sywzmc，gyjl选择回调
		onSelectBlur(val) {
			if (val.key == 'sywzmc' && ((!this.formData.sywzmc||this.formData.sywzmc=='') || val.type == 'save')) {
				this.filterGyjl(val, true)
			}
			if (val.key == 'gyjl' && ((!this.formData.gyjl||this.formData.gyjl=='')  || val.type == 'save')) {
				this.filterGynd(val, true)
			}
		},
		//gyjl选项
		filterGyjl(val, reset) {
			let that = this
			let tmp = []
			_.forEach(_.filter(this.gyjlOptionsOld, function (o) {
				if (o.remark && o.remark != '') {
					let _index = _.findIndex(o.remark.split(','), function (r) {
						return r == val.value;
					})
					return _index > -1 ? true : false;
				} else {
					return false;
				}
			}), function (item) {
				tmp.push({ label: item.dictLabel, value: item.dictValue })
			})
			this.gyjlOptions = tmp
			if (reset) {
				this.gyndOptions = []
				// setTimeout(() => {
				// 	that.$refs.baseInfoRef.batchUpdateFormData({
				// 		gyjl: '',
				// 		xynd: '',
				// 	});
				// }, 10)
			}
		},
		//gynd选项
		filterGynd(val, reset) {
			let tmp = []
			_.forEach(_.filter(this.gyndOptionsOld, function (o) {
				if (o.remark && o.remark != '') {
					let _index = _.findIndex(o.remark.split(','), function (r) {
						return r == val.value;
					})
					return _index > -1 ? true : false;
				} else {
					return false;
				}
			}), function (item) {
				tmp.push({ label: item.dictLabel, value: item.dictValue })
			})
			this.gyndOptions = tmp
			if (reset) {
				// setTimeout(() => {
				// 	that.$refs.baseInfoRef.batchUpdateFormData({
				// 		xynd: '',
				// 	});
				// }, 10)
			}
		},
		//获取给药剂量
		getGyjl() {
			listData({
				pageNum: 1,
				pageSize: 999999,
				dictType: 'business_gyjl',
				status: 0
			}).then(response => {
				this.gyjlOptionsOld = response.rows
				if (this.formData.sywzmc && this.formData.sywzmc !== '') {
					this.init({ key: 'sywzmc', value: this.formData.sywzmc })
				}
			})
		},
		//获取给药浓度
		getGynd() {
			listData({
				pageNum: 1,
				pageSize: 999999,
				dictType: 'business_gynd',
				status: 0
			}).then(response => {
				this.gyndOptionsOld = response.rows
				if (this.formData.gyjl && this.formData.gyjl !== '') {
					this.init({ key: 'gyjl', value: this.formData.gyjl })
				}
			})
		},
		//试验编号或部门名称点击事件回调
		onDialogSubmit(selectedId, currentRow) {
			console.log(JSON.stringify(currentRow))
			//试验
			if (currentRow.type == 1) {
				this.$refs.baseInfoRef.batchUpdateFormData({
					syNo: currentRow.sn,
					SD: currentRow.leaderName,
				});
				this.formData.syId = currentRow.id
				this.formData.bmId = currentRow.deptId
				this.formData.sdId = currentRow.leader
			}
			//部门
			if (currentRow.type == 2) {
				this.$refs.baseInfoRef.batchUpdateFormData({
					syNo: currentRow.deptName,
					SD: currentRow.nickName,
				});
				this.formData.syId = ''
				this.formData.bmId = currentRow.deptId
				this.formData.sdId = currentRow.userId
			}
			this.$refs.selectReagentDialogRef.onCancel()
		},
		//试验编号或部门名称点击事件
		onClickSybh() {
			this.$refs.selectReagentDialogRef.show(this.formData.id)
			// this.$refs.baseInfoRef.setClickable("syNo", true);
		},
		//获取已填写的表单数据
		getFilledFormData() {
			let content = this.getFilledFormDataByRefs(["baseInfoRef", "storageConditionRef", "stepFormPackageRef", "stepRef", "remarkRef"])
			content.syId = this.formData.syId
			content.bmId = this.formData.bmId
			content.sdId = this.formData.sdId
			return content;
		},
		//获取填写完成的表单数据
		async getFormData() {
			let content = await this.validFormFields(["baseInfoRef", "remarkRef"]);
			content.syId = this.formData.syId
			content.bmId = this.formData.bmId
			content.sdId = this.formData.sdId
			return content;
		}
	}
};
</script>
<style rel="stylesheet/scss" lang="scss">
.mt-20 {
	margin-top: 20px;
}
</style>