<template>
    <el-dialog
            :title="formTitle"
            width="1000px"
            :visible.sync="visible"
            :before-close="_close"
            class="addDrugsDialog"
    >
        <el-tabs v-model="activeName">
            <el-tab-pane label="基础数据" name="first">
                <div>
                    <el-form :model="form" :rules="rules" ref="form" label-width="120px" style="display: flex;flex-wrap: wrap;justify-content: space-between;">
                        <el-form-item label="药品名称 : " prop="drug_name">
                            <el-input v-model="form.drug_name" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>

                        <el-form-item label="药品别名 : ">
                            <el-input v-model="form.drug_alias" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>

                        <el-form-item label="药品类别 : " prop="drug_category">
                            <el-select v-model="form.drug_category" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in drugCategoryList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <div style="width:100%;">
                          <div><span style="width:120px;text-align:right;display:inline-block;margin-bottom:10px;"><span style="color:red;">*</span>单位&规格：</span><span style="color:#999">例如：0.75 g * 60 粒 / 盒</span></div>
                        </div>
                        <div style="width:100%;display:flex;align-items:center;margin-left:120px;">
                            <el-form-item prop="dose" class="noMargin">
                              <el-input v-model="form.dose" type="number" style="width:100px;margin-right:5px;" placeholder="剂量" :disabled="dosageShow"></el-input>
                            </el-form-item>
                            <el-form-item prop="dose_unit" class="noMargin">
                              <el-select v-model="form.dose_unit" style="width:160px;" placeholder="剂量单位" @change="changeDoseUnit">
                                <el-option
                                  v-for="(item,index) in getDataConfig('hemodialysis','units')"
                                  :key="index"
                                  :label="item.name"
                                  :value="item.name">
                                </el-option>
                              </el-select>
                            </el-form-item>
                            <span style="margin-bottom:20px;font-size:20px;padding:0 10px;">*</span>
                            <el-form-item prop="min_number" class="noMargin">
                              <el-input v-model="form.min_number" style="width:100px;margin-right:5px;" :disabled="dosageShow" type="number" placeholder="拆零"> </el-input>
                            </el-form-item>
                            <el-form-item prop="min_unit" class="noMargin">
                              <el-select v-model="form.min_unit" style="width:160px;"  placeholder="拆零单位"  @change="changeMinUnit">
                                <el-option
                                  v-for="(item,index) in getDataConfig('hemodialysis','units')"
                                  :key="index"
                                  :label="item.name"
                                  :value="item.name">
                                </el-option>
                              </el-select>
                            </el-form-item>
                          <span style="margin-bottom:20px;font-size:20px;padding:0 10px;">/</span>
                          <el-form-item prop="max_unit" class="noMargin">
                            <el-select v-model="form.max_unit" style="width:160px;" placeholder="包装单位" @change="changeMaxUnit">
                              <el-option
                                v-for="(item,index) in getDataConfig('hemodialysis','units')"
                                :key="index"
                                :label="item.name"
                                :value="item.name">
                              </el-option>
                            </el-select>
                          </el-form-item>
                        </div>


                        <el-form-item label="药品类型 : " prop="drug_type">
                            <el-select v-model="form.drug_type" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in drugTypeList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                      <el-form-item label="默认给药途径:" prop="delivery_way">
                        <el-select v-model="form.delivery_way" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="(item, index) in deliveryWayOptions"
                                    :key="index"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="默认执行频率:" prop="execution_frequency">
                        <el-select v-model="form.execution_frequency" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in executionFrequencyOptions"
                                    :key="index"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="默认单次用量 : " prop="drug_dose">
                         <el-input v-model="form.drug_dose" style="width:70px;margin-right:5px;" placeholder=""
                                      maxlength="30"></el-input>
                          <el-select v-model="form.drug_dose_unit" style="width:80px;" placeholder="单次用量单位">
                                <el-option
                                        v-for="(item,index) in unitList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                           </el-select>
                     </el-form-item>
                    <el-form-item label="默认开药数量:" prop="prescribing_number">
                        <el-input v-model="form.prescribing_number" style="width:160px;" placeholder=""
                                  maxlength="30"></el-input>
                    </el-form-item>
                     <el-form-item label="开药数量单位:" prop="prescribing_number">
                          <el-select v-model="form.prescribing_number_unit" style="width:160px;" placeholder="开药数量单位">
                              <el-option
                                v-for="(item,index) in getDataConfig('hemodialysis','units')"
                                :key="index"
                                :label="item.name"
                                :value="item.name">
                              </el-option>
                          </el-select>
                    </el-form-item>
                    <el-form-item label="默认开药天数:" prop="prescribing_number">
                        <el-input v-model="form.drug_day" style="width:160px;" placeholder=""
                                  maxlength="30"></el-input>
                    </el-form-item>
                       <el-form-item label="库存警戒:" prop="drug_stock_limit">
                            <el-input v-model="form.drug_stock_limit" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="产地:" prop="drug_origin_place">
                            <el-input v-model="form.drug_origin_place" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="药品剂型 : " prop="drug_dosage_form">
                            <el-select v-model="form.drug_dosage_form" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in drugDosageFormList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="医保等级 : ">
                            <el-select v-model="form.medical_insurance_level" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in medicalInsuranceLevelList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <!-- <el-form-item label="单位换算 : ">
                            <el-input v-model="form.unit_matrixing" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item> -->
                        <el-form-item label="拆零零售价(元) : " prop="min_price">
                            <el-input v-model="form.min_price" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="包装零售价(元) : " prop="retail_price">
                          <el-input v-model="form.retail_price" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="进货价(元) : " prop="last_price">
                            <el-input v-model="form.last_price" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="开药控制 : " prop="drug_control">
                            <el-select v-model="form.drug_control" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in drugControlList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="批准文号 : " prop="number">
                            <el-input v-model="form.number" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="药物分类 : " prop="drug_classify">
                            <el-select v-model="form.drug_classify" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item, index) in drugClassifyList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>



                        <el-form-item label="限制性用药 : ">
                            <el-select v-model="form.lmt_used_flag" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in lmt_used_flags"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>


                        <el-form-item label="审批标志 : ">
                            <el-select v-model="form.hosp_appr_flag" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in hosp_appr_flags"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>


                        <el-form-item label="医保编码 : " prop="medical_insurance_number">
                            <el-input v-model="form.medical_insurance_number" style="width:160px;" placeholder=""
                                      maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="生产厂商 : " prop="manufacturer">

                            <el-select v-model="form.manufacturer" style="width:160px;" filterable placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in manufacturerList"
                                        :key="index"
                                        :label="item.manufacturer_name"
                                        :value="item.id">
                                </el-option>
                            </el-select>

                        </el-form-item>
                        <el-form-item label="药理分类 : " prop="pharmacology_category">
                            <el-select v-model="form.pharmacology_category" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index)  in pharmacologyCategoryList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="统计分类 : " prop="statistics_category">
                            <el-select v-model="form.statistics_category" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index)  in statisticsCategoryList"
                                        :key="index"
                                        :label="item.name"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="本位码 : " prop="code">
                            <el-input v-model="form.code" style="width:160px;" placeholder="" maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="是否特病目录:" prop="is_special_diseases">
                            <el-select v-model="form.is_special_diseases" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in options"
                                        :key="index"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="是否备案 : " prop="is_record">
                            <el-select v-model="form.is_record" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in options"
                                        :key="index"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="处方药标志">
                            <el-select v-model="form.prescription_mark" style="width:160px;" placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in options"
                                        :key="index"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="社保目录编码">
                            <el-input v-model="form.social_security_directory_code" style="width:160px"></el-input>
                        </el-form-item>



                        <el-form-item label="备案日期:">
                            <el-date-picker
                                    style="width:160px"
                                    value-format="yyyy-MM-dd"
                                    v-model="form.record_date"
                                    type="date"
                                    placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item>

                        <el-form-item label="药品备注:">
                            <el-input v-model="form.drug_remark" style="width:160px"></el-input>
                        </el-form-item>

                        <el-form-item label="经销商: " style="width:100%">

                            <el-select v-model="form.dealer" style="width:100%;" filterable placeholder="请选择">
                                <el-option
                                        v-for="(item,index) in dealer"
                                        :key="index"
                                        :label="item.dealer_name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="" style="display:none;">
                            <el-input style="width:160px;" placeholder="" maxlength="30"></el-input>
                        </el-form-item>
                        <el-form-item label="状态 : " prop="drug_status">
                            <el-checkbox-group v-model="drug_status">
                                <el-checkbox label="停用">停用</el-checkbox>
                                <el-checkbox label="贵重">贵重</el-checkbox>
                                <el-checkbox label="收费">收费</el-checkbox>
                                <el-checkbox label="库存管理">库存管理</el-checkbox>
                                <el-checkbox label="默认用药途径收费">默认用药途径收费</el-checkbox>
                                <el-checkbox label="注射类">注射类</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                        <el-form-item label="限制说明 : " prop="limit_remark" style="width:100%;">
                            <el-input type="form.restrictions" :rows="2" placeholder="请输入内容"
                                      v-model="form.limit_remark"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
            </el-tab-pane>
            <el-tab-pane label="扩展数据" name="second">
                <el-form :model="form" label-width="120px"
                         style="display: flex;flex-wrap: wrap;justify-content: space-between;">

                    <el-form-item label="标签:" prop="label">
                        <el-select v-model="form.label" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in sign"
                                    :key="index"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="收费项目排序:" prop="sort">
                        <el-input v-model="form.sort" style="width:160px;" placeholder="" maxlength="30"></el-input>
                    </el-form-item>
                    <el-form-item label="是否常用医嘱:" prop="is_use_doctor_advice">
                        <el-select v-model="form.is_use_doctor_advice" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in options"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否默认:" prop="is_default">
                        <el-select v-model="form.is_default" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="(item,index) in options"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否收费预估项:" prop="is_charge_predict">
                        <el-select v-model="form.is_charge_predict" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="item,index in options"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否统计工作量:" prop="is_statistics_work">
                        <el-select v-model="form.is_statistics_work" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="item,index in options"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否收费常用项:" prop="is_charge_use">
                        <el-select v-model="form.is_charge_use" style="width:160px;" placeholder="请选择">
                            <el-option
                                    v-for="item,index in options"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="" style="visibility: hidden;">
                        <el-input style="width:160px;" placeholder="" maxlength="30"></el-input>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
        </el-tabs>


        <span slot="footer" class="dialog-footer">
    <el-button @click="cancle('form')">取 消</el-button>
    <el-button type="primary" @click="comfirm('form')">保 存</el-button>
  </span>
    </el-dialog>
</template>

<script>
  import { getDataConfig, getDictionaryDataConfig } from '@/utils/data'
  import { getAdviceConfig } from '@/api/advice'
  import { GetAllConfig, getManufacturerList,getInitializtion } from '@/api/stock'

  export default {
    data() {
      return {
        visible: false,
        formTitle: '',
        activeName: 'first',
        // manufacturers:[],
        sign: [
          { id: 1, name: '透析模式' },
          { id: 2, name: '特殊护理' },
          { id: 3, name: '抗凝剂' }
        ],
        options: [{
          value: '1',
          label: '是'
        }, {
          value: '2',
          label: '否'
        }],
        lmt_used_flags: [{
          id: 0,
          name: '否'
        }, {
          id: 1,
          name: '是'
        }], hosp_appr_flags: [
          {
            id: 1,
            name: '无需审批'
          }, {
            id: 2,
            name: '审批通过'
          }, {
            id: 3,
            name: '审批不通过'
          }
        ],

        deliveryWayOptions: [],
        executionFrequencyOptions: [],
        value: '',
        radio: '1',
        drug_status: [],
        form: {
          lmt_used_flag: 0, //是否限制用药
          hosp_appr_flag: '',//审批标志
          drug_name: '',//药品名称
          pinyin: '',//拼音
          wubi: '',//五笔输入法
          drug_alias: '',//药品别名
          drug_alias_pinyin: '',//别名拼音
          drug_alias_wubi: '',//别名五笔
          drug_category: '',//药品类别
          drug_spec: '',//药品规格
          drug_type: '',//药品类型
          drug_stock_limit: '',//库存警戒
          drug_origin_place: '',//产地
          drug_dosage_form: '',//药品剂型
          medical_insurance_level: '',//医保等级
          max_unit: '',//最大单位
          min_unit: '',//最小单位
          unit_matrixing: '',//单位换算
          retail_price: '',//零售价
          last_price: '',//上次进价
          drug_control: '',//开药控制
          number: '',//批准文号
          drug_classify: '',//药物分类
          drug_dose: '',//药品剂量
          drug_dose_unit: '',
          medical_insurance_number: '',//医保编号
          manufacturer: '', //生产产商
          pharmacology_category: '',//药理分类
          statistics_category: '',//统计分类
          code: '',//本位码
          is_special_diseases: '',//是否特病目录
          is_record: '',//是否备案
          agent: '',// 代理商
          drug_status: '',// 状态
          limit_remark: '',//限制说明
          delivery_way: '',//默认给药途径
          execution_frequency: '',//默认执行频率
          single_dose: '',//默认单次用量
          prescribing_number: '',//默认开药数量
          label: '',//标签
          sort: '',//收费项目排序
          is_use_doctor_advice: '',//是否常用医嘱
          is_default: '',//是否默认
          is_charge_predict: '',//是否收费预估项
          is_statistics_work: '',//是否统计工作量
          is_charge_use: '',//是否收费常用项
          drug_code: '',
          dealer: '',
          prescription_mark: '',//处方药标志
          social_security_directory_code: '',//社保目录编码
          record_date: '',//备案日期
          drug_remark: '',//药品备注
          drug_day:"",//默认开药天数
          min_number:"",//拆零数量
          min_price:"",//拆零零售价
          dose_unit:"",//剂量
          dose:"",//剂量单位
          prescribing_number_unit:"",
          total:"",
        },

        rules: {
          drug_name: [{ required: true, message: '请填写药品名称', trigger: 'blur' }],
          drug_alias: [{ required: true, message: '请选择药品类别', trigger: 'change' }],
          // drug_spec: [{ required: true, message: '请填写药品规格', trigger: 'blur' }],
          dose:[{required:true,message:'请输入剂量',trigger:'blur'}],
          dose_unit:[{required:true,message:"请填写剂量",trigger:'blur'}],
          max_unit: [{ required: true, message: '请填写包装单位', trigger: 'change' }],
          min_unit: [{ required: true, message: '请填写拆零单位', trigger: 'change' }],
          unval:[{required:true,message:"请填写拆零",trigger:'change'}],
          min_number:[{required:true,message:"请填写拆零",trigger:'change'}],
          packing_unit: [{ required: true, message: '请填写包装单位', trigger: 'change' }],
          drug_type: [{ required: true, message: '请填写药品类型', trigger: 'blur' }],
          drug_stock_limit: [{ required: true, message: '请填写库存警戒', trigger: 'blur' }],
          drug_origin_place: [{ required: true, message: '请填写产地', trigger: 'blur' }],
          drug_dosage_form: [{ required: true, message: '请填写药品剂型', trigger: 'blur' }],
          medical_insurance_level: [{ required: true, message: '请选择医保等级', trigger: 'change' }],

          unit_matrixing: [{ required: true, message: '请填写单位换算', trigger: 'blur' }],
          retail_price: [{ required: true, message: '请填写拆零零售价', trigger: 'blur' }],
          last_price: [{ required: true, message: '请填写进货价', trigger: 'blur' }],
          packing_price:[{required:true,message:'请填写包装零售价',trigger:'blur'}],
          drug_classify: [{ required: true, message: '请选择药物分类', trigger: 'change' }],
          drug_dose: [{ required: true, message: '请填写剂量', trigger: 'blur' }],
          manufacturer: [{ required: true, message: '请选择生产产商', trigger: 'change' }],
          dealer: [{ required: true, message: '请选择经销商', trigger: 'change' }],
          prescription_mark: [{ required: true, message: '请选择处方药标志' }],
          social_security_directory_code: [{ required: true, message: '请填写社保目录编码' }],
          record_date: [{ required: true, message: '请选择备案日期' }],
          drug_remark: [{ required: true, message: '请填写药品备注' }],
          lmt_used_flag:[{ required: true, message: '请选择是否限制用药' }],
          hosp_appr_flag:[{ required: true, message: '请选择审批标志' }],

        },
        page: 1,
        limit: 500,
        manufacturerList: [],
        dealer: [],
        drugTypeList:[],
        drugCategoryList:[],
        drugDosageFormList:[],
        medicalInsuranceLevelList:[],
        drugControlList:[],
        drugClassifyList:[],
        statisticsCategoryList:[],
        pharmacologyCategoryList:[],
        dosageShow:false,
        unitList:[]
      }
    },
    props: {
      manufacturers: {
        type: Array
      },
      titles: {
        type: String,
        default: ''
      },
      formValue: {
        type: Object
      },
      isCreated: {
        type: Number,
        default: 1
      },


    },
    methods: {
      cancle: function(formName) {
        this.$emit('dialog-cancle', this.getValue())
      },
      comfirm: function(formName) {
        this.$refs[formName].validate((valid) => {
          console.log(valid)
          if (valid) {
            // console.log(1111,this.getValue())
            this.$emit('dialog-comfirm', this.getValue())
          }
        })
      },
      getDictionaryDataConfig(module, filed_name) {
        return getDictionaryDataConfig(module, filed_name)

      },
      getDataConfig(module, filed_name) {
        return getDataConfig(module, filed_name)
      },
      _close: function(done) {
        // this.clear()
        // done()
        this.$confirm('是否保存')
          .then(_ => {
            this.comfirm('form')
            // done();
          })
          .catch(_ => {
            done()
          });
      },
      clear: function() {
        this.form.id = 0
        this.form.name = ''
        this.form.intro = ''
      },
      show(id, obj) {

        this.unitList = []
        var arr =  getDataConfig('hemodialysis','units')
        for(let i=0;i<arr.length;i++){
            if(obj.max_unit == arr[i].name){
              this.unitList.push(arr[i])
            }
        }
        for(let i=0;i<arr.length;i++){
            if(obj.min_unit == arr[i].name){
              this.unitList.push(arr[i])
            }
        }
        console.log('this.unitList',this.unitList)
        this.getInitializtion()
        this.getlist()
        this.GetAllConfig()
        if(obj.drug_classify == 0){
           obj.drug_classify = ""
        }else{
          obj.drug_classify = parseInt(obj.drug_classify)
        }

        if(obj.dealer == 0){
           obj.dealer = ""
        }

        this.visible = true
        if (id == 0) {
          this.formTitle = '新增'
          this.form['id'] = 0
          for (let key in obj) {
            this.form[key] = obj[key]
          }

          this.form['lmt_used_flag'] = 0

        } else {
          for (let key in obj) {
            this.form[key] = obj[key]
          }

          this.form.drug_classify = parseInt(this.form.drug_classify)
          this.form['id'] = id
          this.drug_status = []
          if (this.form.drug_status.length > 0) {
            for (let i = 0; i < this.form.drug_status.split(",").length; i++) {
              this.drug_status.push(this.form.drug_status.split(',')[i])
            }
            console.log(this.drug_status)
          }

          this.formTitle = '编辑'

        }
        this.getAdviceConfig()

      }, getAdviceConfig() {
        getAdviceConfig().then(response => {
          if (response.data.state == 1) {
            this.deliveryWayOptions =
              response.data.data.drugways === null
                ? []
                : response.data.data.drugways

            this.executionFrequencyOptions =
              response.data.data.efs === null ? [] : response.data.data.efs
          }
        })

      }
      ,
      hide() {
        // this.clear()
        this.form.social_security_directory_code = ''
        this.form.prescription_mark = ''
        this.form.record_date = ''
        this.form.drug_remark = ''
        this.form.dose = ''
        this.form.dose_unit = ""
        this.form.min_number= ""
        this.form.min_unit = ""
        this.form.max_unit = ""
        this.form.min_price = ""
        this.form.prescribing_number_unit = ""
        this.visible = false
      },
      getValue: function() {
        const form = {}
        form['id'] = this.form.id
        form['drug_name'] = this.form.drug_name
        form['pinyin'] = this.form.pinyin
        form['wubi'] = this.form.wubi
        form['drug_alias'] = this.form.drug_alias
        form['drug_alias_pinyin'] = this.form.drug_alias_pinyin
        form['drug_alias_wubi'] = this.form.drug_alias_wubi
        form['drug_category'] = this.form.drug_category
        form['drug_spec'] = this.form.drug_spec
        form['drug_type'] = this.form.drug_type
        form['drug_stock_limit'] = this.form.drug_stock_limit
        form['drug_origin_place'] = this.form.drug_origin_place
        form['drug_dosage_form'] = this.form.drug_dosage_form
        form['medical_insurance_level'] = this.form.medical_insurance_level
        form['max_unit'] = this.form.max_unit
        form['min_unit'] = this.form.min_unit
        form['unit_matrixing'] = this.form.unit_matrixing
        form['retail_price'] = this.form.retail_price
        form['last_price'] = this.form.last_price
        form['drug_control'] = this.form.drug_control
        form['number'] = this.form.number
        form['drug_dose'] = this.form.drug_dose
        form['drug_dose_unit'] = this.form.drug_dose_unit
        form['medical_insurance_number'] = this.form.medical_insurance_number
        form['manufacturer'] = this.form.manufacturer
        form['pharmacology_category'] = this.form.pharmacology_category
        form['statistics_category'] = this.form.statistics_category
        form['code'] = this.form.code
        form['is_special_diseases'] = this.form.is_special_diseases
        form['is_record'] = this.form.is_record
        form['agent'] = this.form.agent
        form['drug_status'] = this.drug_status.join(',')
        form['limit_remark'] = this.form.limit_remark
        form['delivery_way'] = this.form.delivery_way
        form['execution_frequency'] = this.form.execution_frequency
        form['single_dose'] = this.form.single_dose
        form['prescribing_number'] = this.form.prescribing_number
        form['label'] = this.form.label
        form['sort'] = this.form.sort
        form['is_use_doctor_advice'] = this.form.is_use_doctor_advice
        form['is_default'] = this.form.is_default
        form['is_charge_predict'] = this.form.is_charge_predict
        form['is_statistics_work'] = this.form.is_statistics_work
        form['is_charge_use'] = this.form.is_charge_use
        form['drug_code'] = this.form.drug_code
        form['drug_classify'] = this.form.drug_classify
        form['dealer'] = this.form.dealer
        form['prescription_mark'] = this.form.prescription_mark
        form['social_security_directory_code'] = this.form.social_security_directory_code
        form['record_date'] = this.form.record_date
        form['drug_remark'] = this.form.drug_remark
        form['hosp_appr_flag'] = this.form.hosp_appr_flag
        form['lmt_used_flag'] = this.form.lmt_used_flag
        form['total'] = this.form.total
        form['min_number'] = this.form.min_number
        form["drug_day"] = this.form.drug_day
        form['dose'] = this.form.dose,
        form['dose_unit'] = this.form.dose_unit
        form['min_price'] = this.form.min_price
        form['prescribing_number_unit'] = this.form.prescribing_number_unit
        return form
      },
      getlist() {
        var params = {
          page: this.page,
          limit: this.limit
        }

        getManufacturerList(params).then(response => {
          var manufacturerList = response.data.data.manufacturer

          this.manufacturerList = manufacturerList
        })
      },
      GetAllConfig() {
        GetAllConfig().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.dealer = response.data.data.dealer
            // console.log('经销商', this.dealer)
          }
        })
      },
      getInitializtion(){
        getInitializtion().then(response=>{
           if(response.data.state ==1){
             var drugCategoryList =  response.data.data.drugCategoryList

             this.drugCategoryList = drugCategoryList
             var drugTypeList = response.data.data.drugTypeList

              this.drugTypeList = drugTypeList
             var drugDosageFormList =  response.data.data.drugDosageFormList

             this.drugDosageFormList = drugDosageFormList
             var medicalInsuranceLevelList =  response.data.data.medicalInsuranceLevelList

             this.medicalInsuranceLevelList = medicalInsuranceLevelList
             var drugControlList = response.data.data.drugControlList

             this.drugControlList = drugControlList
             var drugClassifyList = response.data.data.drugClassifyList

             this.drugClassifyList = drugClassifyList
             var statisticsCategoryList =  response.data.data.statisticsCategoryList

             this.statisticsCategoryList = statisticsCategoryList
             var pharmacologyCategoryList = response.data.data.pharmacologyCategoryList

             this.pharmacologyCategoryList = pharmacologyCategoryList
           }
        })
      },
      changeDoseUnit(val){
        if(val == this.form.min_unit){
          // this.form.dose = 1
          this.form.min_number = 1
          this.dosageShow = true
        }
        if(val !=this.form.min_unit){
          this.dosageShow = false
        }
        var arr =  getDataConfig('hemodialysis','units')
        this.unitList = []
        for(let i=0;i<arr.length;i++){

          if(this.form.min_unit == arr[i].name){
            this.unitList.push(arr[i])
          }
          if(val == arr[i].name){
            this.unitList.push(arr[i])
          }
          let idArr = []
          this.unitList.map(item => {
            idArr.push(item.id)
          })
          if(idArr.indexOf(this.form.drug_dose_unit) == -1){
            if(this.form.drug_dose_unit == arr[i].id){
              this.unitList.push(arr[i])
            }
          }

        }
        let ojb = {};
        this.unitList = this.unitList.reduce(function(prevArr, currentItem) {
            //利用对象的键名无法重复的特点，mch_id是唯一区别的属性值
            ojb[currentItem.name] ? '' : ojb[currentItem.name] = true && prevArr.push(currentItem);
            return prevArr
        }, [])

      },
      changeMinUnit(val){
        if(val == this.form.dose_unit){
          // this.form.dose = 1
          this.form.min_number = 1
          this.dosageShow = true
        }
        if(val != this.form.max_unit){
          this.dosageShow = false
        }
        if(val == this.form.max_unit){
          this.form.min_number = 1
          this.dosageShow = true
        }

        // if(this.form.dosage == "" || this.form.max_unit == ""){
        //    this.form.min_number = 1
        // }
        var arr = getDataConfig('hemodialysis','units')
        this.unitList = []
        for(let i=0;i<arr.length;i++){
            if(val == arr[i].name){
              this.unitList.push(arr[i])
            }
            if(this.form.max_unit == arr[i].name){
              this.unitList.push(arr[i])
            }
            if(this.form.drug_dose_unit == arr[i].id){
              this.unitList.push(arr[i])
            }
            let idArr = []
            this.unitList.map(item => {
              idArr.push(item.id)
            })
            if(idArr.indexOf(this.form.drug_dose_unit) == -1){
              if(this.form.drug_dose_unit == arr[i].id){
                this.unitList.push(arr[i])
              }
            }
        }
        let ojb = {};
        this.unitList = this.unitList.reduce(function(prevArr, currentItem) {
          //利用对象的键名无法重复的特点，mch_id是唯一区别的属性值
          ojb[currentItem.name] ? '' : ojb[currentItem.name] = true && prevArr.push(currentItem);
          return prevArr
        }, [])

      },
      changeMaxUnit(val){
        if(val == this.form.min_unit){
          this.form.min_number = 1
          this.dosageShow = true
        }
      }
    }, watch: {
      visible(val) {
        // this.form = this.formValue

      }
    },
    created() {
      this.getInitializtion()
       //获取所有经销商
      this.GetAllConfig()
      this.getlist()



    }
  }
</script>

<style lang="scss">
    .addDrugsDialog {

      .el-dialog__body {
          padding-top: 0px;
      }
      .noMargin{
        .el-form-item__content{
          margin-left: 0 !important;
        }
      }

    }
</style>
