<template>
  <div style="min-height:100%" ref="viewBox">
    <crumbs :title="title" :param="$route.params" linkTo="ZskRegisteredDrug"></crumbs>
    <div class="contents">
      <div class="content">
        <el-form
          :inline="true"
          ref="formInline"
          :rules="rules"
          :model="formInline"
          class="demo-form-inline"
          label-width="100px"
        >
          <el-form-item label="药品代码:" prop="drugCode">
            <el-input v-model.trim="formInline.drugCode" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="最小包装单位:">
            <el-input v-model.trim="formInline.minPackUnit" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="注册名称:" prop="name">
            <el-input v-model.trim="formInline.name" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="药品企业:">
            <el-input v-model.trim="formInline.company" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="英文名称:">
            <el-input v-model.trim="formInline.enName" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="批准文号:">
            <el-input v-model.trim="formInline.approval" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="商品名称:">
            <el-input v-model.trim="formInline.tradeName" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="药品本位码:">
            <el-input v-model.trim="formInline.standardCode" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="药品剂型:">
            <el-input v-model.trim="formInline.form" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="医保类型:">
            <el-input v-model.trim="formInline.insuranceType" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="注册规格:">
            <el-input v-model.trim="formInline.specification" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="医保备注:">
            <el-input v-model.trim="formInline.insuranceRemrk" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="最小包装数量:">
            <el-input v-model.trim="formInline.minPackQuantity" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="药品类别:">
            <el-input v-model.trim="formInline.drugType" maxlength="200" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="关联通用名:">
            <el-select
              clearable
              remote
              filterable
              @focus="getfocus"
              :remote-method="searchConcept"
              v-model.trim="formInline.drugConceptId"
            >
              <el-option
                v-for="item in conceptList"
                :key="item.id"
                :label="item.libName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div class="Butn">
          <el-button class="disclButn" size="small" type="primary" @click="confirm">确定</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
import api from '@api/icss.js';
import config from '@api/config.js';
import utils from '@api/utils.js';

export default {
  name: 'AddZskDiagBase',
  data() {
    return {
      formInline: {
        id: '',
        drugCode: '', //药品代码
        name: '', //注册名称
        enName: '', //英文名称
        tradeName: '', //商品名称
        form: '', //药品剂型
        specification: '', //注册规格
        minPackQuantity: '', //最小包装数量
        minPackUnit: '', //最小包装单位
        company: '', //药品企业
        approval: '', //批准文号
        standardCode: '', //药品本位码
        insuranceType: '', //医保类型
        insuranceRemrk: '', //医保备注
        drugType: '', //药品类别
        drugConceptId: '' //药品通用名Id
      },
      rules: {
        drugCode: [
          { required: true, message: '请输入药品代码', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入注册名称', trigger: 'change' }]
      },
      conceptList: [],
      value: '',
      isEdit: false,
      title: '注册药品维护-添加注册药品'
    };
  },
  created() {
    let type = this.$route.params.type;
    if (type == 2) {
      this.title = '注册药品维护-修改注册药品';
      api.getByParam({ id: this.$route.params.id }).then(res => {
        if (res.data.code == 0) {
          const data = res.data.data;
          this.formInline.id = data.id;
          this.formInline.drugCode = data.drugCode;
          this.formInline.name = data.name;
          this.formInline.enName = data.enName;
          this.formInline.tradeName = data.tradeName;
          this.formInline.form = data.form;
          this.formInline.specification = data.specification;
          this.formInline.minPackQuantity = data.minPackQuantity;
          this.formInline.minPackUnit = data.minPackUnit;
          this.formInline.company = data.company;
          this.formInline.approval = data.approval;
          this.formInline.standardCode = data.standardCode;
          this.formInline.insuranceType = data.insuranceType;
          this.formInline.insuranceRemrk = data.insuranceRemrk;
          this.formInline.drugType = data.drugType;
          this.formInline.drugConceptId = data.drugConceptId;
          this.conceptList = [
            { id: data.drugConceptId, libName: data.drugName }
          ];
        } else {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          });
        }
      });
    }
  },
  methods: {
    searchConcept(val) {
      if (val == '') {
        return;
      }
      api
        .getsearchDrug({ name: val })
        .then(res => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.$nextTick(() => {
              // dom元素更新后执行，因此这里能正确打印更改之后的值
              this.conceptList = data; // 改变了的值
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    validateForms(callBack) {
      this.$refs['formInline'].validate(valid => {
        if (valid) {
          callBack();
        } else {
          return false;
        }
      });
    },
    saveOrUpdate(params) {
      api.saveOrUpdate(params).then(res => {
        if (res.data.code == 0) {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.$router.push({
            name: 'ZskRegisteredDrug',
            params: this.$route.params
          });
        } else {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          });
        }
      });
    },
    getfocus() {
      this.conceptList = [];
    },
    confirm() {
      let _this = this;
      this.validateForms(function() {
        let params = _this.formInline;
        _this.saveOrUpdate(params);
      });
    }
  }
};
</script>
<style lang="less" scoped>
@import '../../less/admin.less';
.container {
  top: 0;
}
.contents {
  height: 100%;
  .content {
    height: 600px;
    background: #fff;
    padding: 20px 20px 0;
    color: #545455;
    min-width: 980px;
    position: relative;
    /deep/ .el-form-item {
      width: 45%;
      margin-bottom: 20px;
      .el-form-item__label {
        margin-right: 20px;
      }

      .el-input {
        .el-input__inner {
          width: 300px;
        }
      }
    }
  }
}
</style>