<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb
        v-if="this.$route.query.type == 1"
        :crumbs="crumbs"
      ></bread-crumb>
      <bread-crumb
        v-if="this.$route.query.type == 2"
        :crumbs="crumbs2"
      ></bread-crumb>

      <div style="float: right">
        <el-button size="small" @click="back()" class="filter-item"
          >取 消</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="submit()"
          class="filter-item"
          >保 存</el-button
        >
      </div>
    </div>

    <div class="app-container">
      <drugs-stock-dialog
        ref="dialog"
        :propForm="propForm"
        :visibility="isVisibility"
        v-on:dialog-comfirm="comfirm"
        v-on:dialog-cancle="cancle"
      >
      </drugs-stock-dialog>

      <div class="cell clearfix">
        <label class="title"
          ><span class="name"><span style="color: red">*</span>仓库</span>
          :</label
        >
        <el-select
          size="small"
          v-model="storehouse_id"
          filterable
          placeholder="请选择仓库"
          style="width: 200px"
          :disabled="true"
        >
          <el-option
            v-for="(option, index) in houstList"
            :key="index"
            :label="option.storehouse_name"
            :value="option.id"
          >
          </el-option>
        </el-select>
        <label class="title"><span class="name">出库时间</span> : </label>
        <el-date-picker
          :disabled="drug_show"
          size="small"
          v-model="warehouse_out_time"
          prefix-icon="el-icon-date"
          :editable="false"
          style="width: 200px"
          type="date"
          placeholder="选择日期时间"
          align="right"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>

      <el-form :rules="tableRules" :model="recordInfo" ref="tableForm">
        <el-table
          :data="recordInfo.recordData"
          :class="signAndWeighBoxPatients"
          style="width: 100%"
          border
          max-height="450"
          :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }"
        >
          <el-table-column align="center" width="230">
            <template slot="header" slot-scope="scope">
              <span>药品名称<span style="color: red">*</span></span>
            </template>

            <template slot-scope="scope">
              <el-form-item style="padding-top: 15px">
                <el-autocomplete
                  class="checkSearch"
                  popper-class="my-autocomplete"
                  v-model="scope.row.drug_name"
                  :fetch-suggestions="querySearchAsync"
                  :trigger-on-focus="true"
                  placeholder="请输入药品名称"
                  @select="handleSelect"
                  @input="changeDrugName(scope.$index)"
                  style="width: 210px !important"
                  :disabled="drug_show"
                >
                  <i class="el-icon-search el-input__icon" slot="suffix"></i>
                  <template slot-scope="{ item }">
                    <div class="name">
                      {{
                        item.drug_name +
                        item.dose +
                        item.dose_unit +
                        "*" +
                        item.min_number +
                        item.min_unit +
                        "/" +
                        item.max_unit +
                        " " +
                        item.manufacturer
                      }}
                    </div>
                  </template>
                </el-autocomplete>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column align="center" width="140">
            <template slot="header" slot-scope="scope">
              <span>药品类型</span>
            </template>
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.drug_type"
                filterable
                placeholder="请选择厂商"
                :disabled="drug_show"
              >
                <el-option
                  v-for="(option, index) in drugTypeList"
                  :key="index"
                  :label="option.name"
                  :value="option.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="center" width="140">
            <template slot="header" slot-scope="scope">
              <span>规格&单位</span>
            </template>
            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px">
                <el-input
                  :disabled="true"
                  placeholder="请输入规格&单位"
                  v-model="scope.row.name"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" width="150">
            <template slot="header" slot-scope="scope" >
                  <span>批号</span>
              </template>
                <template slot-scope="scope">
                  <el-form-item style="padding-top: 20px">
                    <el-select v-model="scope.row.batch_number" filterable placeholder="请选择">
                      <el-option
                        v-for="(item,index) in numberList"
                        :key="index"
                        :label="item.batch_number"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
              </template>
          </el-table-column> -->

          <el-table-column width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>剩余库存</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :rules="tableRules.stock_count"
                style="padding-top: 20px"
              >
                <div>
                  <el-input
                    :disabled="true"
                    style="width: 120px"
                    placeholder="剩余库存"
                    v-model="scope.row.stock_count"
                  ></el-input>
                </div>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column width="200" align="center">
            <template slot="header" slot-scope="scope">
              <span>出库数量<span style="color: red">*</span></span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'recordData.' + scope.$index + '.count'"
                :rules="tableRules.count"
                style="padding-top: 20px"
              >
                <div style="display: flex">
                  <el-input
                    oninput="value=value.replace(/\D|^0/g,'')"
                    style="width: 100px"
                    :disabled="drug_show"
                    placeholder="请输入出库数量"
                    type="number"
                    v-model="scope.row.count"
                    @input="
                      getSupplyCount(
                        scope.row.count,
                        scope.row.max_unit,
                        scope.row.min_number,
                        scope.$index,
                        scope.row.total_count,
                        scope.row.max_unit_fisrt,
                        scope.row.min_unit_fisrt
                      )
                    "
                  ></el-input>
                  <el-select
                    v-model="scope.row.count_unit"
                    filterable
                    placeholder="请选择单位"
                    style="width: 80px"
                    :disabled="unitShow"
                  >
                    <el-option
                      v-for="(option, index) in unitList"
                      :key="index"
                      :label="option.name"
                      :value="option.name"
                    >
                    </el-option>
                  </el-select>
                </div>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="出库对象" width="140" align="center">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.admin_user_id"
                filterable
                placeholder="请选择厂商"
                :disabled="drug_show"
              >
                <el-option
                  v-for="(option, index) in doctorList"
                  :key="index"
                  :label="option.user_name"
                  :value="option.admin_user_id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <!-- <el-table-column width="120" align="center">
            <template slot="header" slot-scope="scope">
              <span>进货价</span>
            </template>
            <template slot-scope="scope">
            
              <el-form-item
                :rules="tableRules.last_price"
                style="padding-top: 20px"
              >
                <el-input
                 :disabled="true"
                  placeholder="进货价"
                  type="number"
                  v-model="scope.row.last_price"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column> -->
          <el-table-column width="120" align="center">
            <template slot="header" slot-scope="scope">
              <span>出货价<span style="color: red">*</span></span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'recordData.' + scope.$index + '.price'"
                :rules="tableRules.price"
                style="padding-top: 20px"
              >
                <el-input
                  :disabled="drug_show"
                  placeholder="请输入出货价"
                  type="number"
                  v-model="scope.row.price"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="总价" width="80" align="center">
            <template slot-scope="scope">
              {{ calculate(scope.row.price * scope.row.count) }}
            </template>
          </el-table-column>
          <el-table-column label="生产厂家" width="140" align="center">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.manufacturer"
                filterable
                placeholder="请选择厂商"
                :disabled="drug_show"
              >
                <el-option
                  v-for="(option, index) in manufacturerList"
                  :key="index"
                  :label="option.manufacturer_name"
                  :value="option.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" width="180" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="drug_show"
                prefix-icon="el-icon-date"
                style="width: 145px"
                v-model="scope.row.product_date"
                type="date"
                placeholder="选择日期时间"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="有效日期" width="180" align="center">
            <template slot-scope="scope">
              <el-date-picker
                :disabled="drug_show"
                prefix-icon="el-icon-date"
                style="width: 145px"
                v-model="scope.row.expiry_date"
                type="date"
                placeholder="选择日期时间"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="经销商" width="140" align="center">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.dealer"
                filterable
                placeholder="请选择厂商"
                :disabled="drug_show"
              >
                <el-option
                  v-for="(option, index) in dealerList"
                  :key="index"
                  :label="option.dealer_name"
                  :value="option.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="center" width="150" label="批准文号">
            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px" :disabled="true">
                <el-input
                  placeholder="请输入批准文号"
                  v-model="scope.row.number"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="备注" width="120" align="center">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                :disabled="drug_show"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="150"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="新增"
                placement="top"
              >
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-circle-plus-outline"
                  @click="handleEdit(scope.$index, scope.row)"
                >
                </el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="top"
              >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.$index, scope.row)"
                >
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
  </div>
</template>

<script>
import { uParseTime } from "@/utils/tools";

import {
  deleteDrugWarehouseOutInfo,
  editDrugWarehouseoutInfo,
  getDrugWarehouseOutInfo,
  GetAllConfigOne,
  GetAllDrugInfoByID,
  getDrugBatchNumber,
} from "@/api/drug/drug_stock";
import BreadCrumb from "../../components/bread-crumb";
import DrugsStockDialog from "./drugsStockDialog/index";
import { postSearchDrugList } from "@/api/data";
import { getDictionaryDataConfig, getDataConfig } from "@/utils/data";
export default {
  components: { DrugsStockDialog, BreadCrumb },
  name: "salesReturnEdit",

  data() {
    return {
      crumbs: [
        { path: false, name: "库存管理" },
        { path: false, name: "药品出库单" },
        { path: false, name: "编辑出库单" },
      ],
      crumbs2: [
        { path: false, name: "库存管理" },
        { path: false, name: "其他出库单" },
        { path: false, name: "编辑出库单" },
      ],
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",

      adminUserOptions: null,
      currentIndex: 0,
      warehouse_out_time: "",
      recordInfo: {
        recordData: [],
        stock_in_code: "",
        current_index: "",
      },
      tableRules: {
        count: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        price: [{ required: true, message: "单价不能为空", trigger: "blur" }],
      },
      ruleForm: {
        manufacturer: [
          { required: true, message: "请选择厂商", trigger: "change" },
        ],
      },
      // prop
      isVisibility: false,
      propForm: {
        goods: [],
        goodType: [],
        goodInfo: [],
        goodUnit: [],
        title: "入库",
        manufacturer: 0,
        dealer: 0,
      },

      form: {
        manufacturer: 0,
        dealer: 0,
      },
      warehouseInfoList: [],
      warehouseOut: {},
      manufacturer: [],
      dealer: [],
      goodType: [],
      goodInfo: [],
      drugTypeList: [],
      manufacturerList: [],
      drug_show: false,
      unitList: [],
      unitShow: true,
      numberList: [],
      houstList: [],
      storehouse_id: "",
    };
  },
  methods: {
    comfirm: function (val) {
      this.$refs.dialog.hide();
      this.propForm.goodType = [];
      this.propForm.goods = [];

      if (val.selectedGoodInfo.length > 0) {
        for (let i = val.selectedGoodInfo.length - 1; i >= 0; i--) {
          if (i == 0) {
            this.recordInfo.recordData[this.currentIndex].drug_id =
              val.selectedGoodInfo[i].id;

            this.recordInfo.recordData[this.currentIndex].price =
              val.selectedGoodInfo[i].last_price.toString();

            this.recordInfo.recordData[this.currentIndex].retail_price =
              val.selectedGoodInfo[i].retail_price.toString();
          } else {
            const tempForm = {};
            tempForm["id"] = 0;
            tempForm["drug_id"] = val.selectedGoodInfo[i].id;
            tempForm["retail_price"] =
              val.selectedGoodInfo[i].retail_price.toString();

            tempForm["count"] = "";
            tempForm["price"] = val.selectedGoodInfo[i].last_price.toString();
            tempForm["remark"] = "";
            this.recordInfo.recordData.splice(
              this.currentIndex + 1,
              0,
              tempForm
            );
          }
        }
      }
      this.currentIndex = -1;
    },
    cancle: function () {
      this.$refs.dialog.hide();
      this.propForm.goods = [];

      this.propForm.goodType = [];
    },
    GetConfigInfo: function () {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      GetAllConfigOne().then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          this.manufacturer = response.data.data.manufacturer;
          this.dealer = response.data.data.dealer;
          // for(let i = 0; i<  response.data.data.drugs.length;i++){
          //   this.goodType.push(response.data.data.drugs[i])
          //   if(response.data.data.drugs[i].drug_specs != null) {
          //     for (let b = 0; b < response.data.data.drugs[i].drug_specs.length; b++) {
          //       this.goodInfo.push(response.data.data.drugs[i].drug_specs[b])
          //     }
          //   }
          // }
          this.manufacturer.splice(0, 0, { id: 0, manufacturer_name: "全部" });
          this.dealer.splice(0, 0, { id: 0, dealer_name: "全部" });
        }
        loading.close();
      });
    },
    typeName: function (drug_id) {
      let name = "";
      for (let i = 0; i < this.goodInfo.length; i++) {
        if (this.goodInfo[i].id == drug_id) {
          name = this.goodInfo[i].drug_name;
        }
      }
      return name;
    },
    specificationName: function (drug_id) {
      let name = "";
      for (let i = 0; i < this.goodInfo.length; i++) {
        if (this.goodInfo[i].id == drug_id) {
          name = this.goodInfo[i].drug_spec;
        }
      }
      return name;
    },
    handleEdit: function (index, row) {
      if (parseInt(this.$route.query.is_sys) == 12) {
        this.$message.error("调拨出库数据不能新增!");
        return;
      }
      if (row.supply_cancel_out_id > 0) {
        this.$message.error("采购出库数据不能新增!");
        return;
      }
      this.unitShow = false;
      if (this.drug_show == true) {
        this.$message.error("自动出库数据不能编辑");
        return;
      }
      const tempObj = {};
      tempObj["id"] = 0;
      tempObj["drug_id"] = 0;
      tempObj["retail_price"] = "";
      tempObj["count"] = "";
      tempObj["max_unit"] = "";
      tempObj["min_unit"] = "";
      tempObj["price"] = "";
      tempObj["remark"] = "";
      tempObj["batch_number"] = "";
      tempObj["number"] = "";
      tempObj["count_unit"] = "";
      tempObj["dealer"] = "";
      tempObj["manufacturer"] = "";
      tempObj["warehouse_info_id"] = 0;
      tempObj["admin_user_id"] = this.$store.getters.xt_user.user.user_name;
      tempObj["last_price"] = "";
      tempObj["stock_count"] = "";
      tempObj["total_count"] = 0;
      tempObj["max_unit_fisrt"] = 0;
      tempObj["min_unit_fisrt"] = 0;
      this.recordInfo.recordData.push(tempObj);
    },
    handleDelete: function (index, row) {
      if (parseInt(this.$route.query.is_sys) == 12) {
        this.$message.error("调拨出库数据不能删除!");
        return;
      }
      if (row.supply_cancel_out_id > 0) {
        this.$message.error("采购出库数据不能删除!");
        return;
      }

      if (this.drug_show == true) {
        this.$message.error("采购出库数据不能编辑");
        return;
      }
      if (row.id == 0) {
        this.recordInfo.recordData.splice(index, 1);
      } else {
        const params = {
          id: row.id,
        };
        this.$confirm("确认删除该出库商品信息记录?", "删除出库商品信息记录", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteDrugWarehouseOutInfo(params).then((response) => {
              if (response.data.state == 0) {
                this.$message.error(response.data.msg);
                return false;
              } else {
                this.$message.success("删除成功");

                this.recordInfo.recordData.splice(index, 1);
              }
            });
          })
          .catch(() => {});
      }
    },
    // getTime(val, temp) {
    //   if (val != 0) {
    //     return uParseTime(val, temp)
    //   } else {
    //     return ''
    //   }
    // },
    getTime(val) {
      if (val < 0) {
        return "";
      }
      if (val == "") {
        return "";
      } else {
        return uParseTime(val, "{y}-{m}-{d}");
      }
    },
    showDialog(index, row) {
      this.currentIndex = index;
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });

      const params = {
        manufacturer_id: this.form.manufacturer,
        dealer_id: this.form.dealer,
      };
      GetAllDrugInfoByID(params).then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          if (response.data.data.drugs.length <= 0) {
            this.$message.error("该厂商或经销商没有药品信息");
            return;
          }
          this.$refs.dialog.show();
          for (let i = 0; i < response.data.data.drugs.length; i++) {
            this.propForm.goods.push(response.data.data.drugs[i]);
          }
          const obj = {};
          this.propForm.goods = this.propForm.goods.reduce((cur, next) => {
            obj[next.id] ? "" : (obj[next.id] = true && cur.push(next));
            return cur;
          }, []); // 设置cur默认类型为数组，并且初始值为空的数组
        }

        for (let i = 0; i < this.propForm.goods.length; i++) {
          for (let a = 0; a < this.propForm.goods[i].drug_specs.length; a++) {
            var respObj = this.propForm.goods[i].drug_specs[a];
            respObj["isSelected"] = false;
          }
        }
        loading.close();
      });
    },

    back() {
      this.$router.go(-1);
    },
    submit() {
      this.$refs["tableForm"].validate((valid) => {
        if (valid) {
          const array = this.recordInfo.recordData;
          for (let i = 0; i < array.length; i++) {
            if (array[i].drug_id == 0) {
              this.$message.error("药品名称或者药品规格名称不能为空");
              return;
            }
          }
          for (let i = 0; i < this.recordInfo.recordData.length; i++) {
            this.recordInfo.recordData[i].price =
              this.recordInfo.recordData[i].price.toString();
            if (this.recordInfo.recordData[i].dealer == "") {
              this.recordInfo.recordData[i].dealer = 0;
            }
            for (let j = 0; j < this.manufacturerList.length; j++) {
              if (
                this.recordInfo.recordData[i].manufacturer ==
                this.manufacturerList[j].manufacturer_name
              ) {
                this.recordInfo.recordData[i].manufacturer =
                  this.manufacturerList[j].id;
              }
            }
            for (let z = 0; z < this.dealerList.length; z++) {
              if (
                this.recordInfo.recordData[i].dealer ==
                this.dealerList[z].dealer_name
              ) {
                this.recordInfo.recordData[i].dealer = this.dealerList[z].id;
              }
            }

            for (let y = 0; y < this.numberList.length; y++) {
              if (
                this.recordInfo.recordData[i].batch_number ==
                this.numberList[y].id
              ) {
                this.recordInfo.recordData[i].batch_number =
                  this.numberList[y].batch_number;
                this.recordInfo.recordData[i].warehouse_info_id =
                  this.numberList[y].id;
              }
              if (
                this.recordInfo.recordData[i].batch_number ==
                this.numberList[y].batch_number
              ) {
                this.recordInfo.recordData[i].warehouse_info_id =
                  this.numberList[y].id;
              }
            }

            for (let s = 0; s < this.doctorList.length; s++) {
              if (
                this.recordInfo.recordData[i].admin_user_id ==
                this.doctorList[s].user_name
              ) {
                this.recordInfo.recordData[i].admin_user_id =
                  this.doctorList[s].admin_user_id;
              }
            }
            this.recordInfo.recordData[i].last_price =
              this.recordInfo.recordData[i].last_price.toString();
            this.recordInfo.recordData[i].stock_count =
              this.recordInfo.recordData[i].stock_count.toString();
          }
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          const params = {
            stockOut: this.recordInfo.recordData,
          };

          editDrugWarehouseoutInfo(
            params,
            this.warehouse_out_time,
            this.$route.query.id,
            this.$route.query.type,
            this.form.manufacturer,
            this.form.dealer,
            this.storehouse_id
          ).then((response) => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg);
              loading.close();
              return false;
            } else {
              var msg = response.data.data.msg;
              var drug_name = response.data.data.drug_name;
              var dose = response.data.data.dose;
              var dose_unit = response.data.data.dose_unit;
              var min_number = response.data.data.min_number;
              var min_unit = response.data.data.min_unit;
              var max_unit = response.data.data.max_unit;
              var str =
                drug_name +
                " " +
                dose +
                dose_unit +
                "*" +
                min_number +
                min_unit +
                "/" +
                max_unit;
              loading.close();
              if (msg == 1) {
                this.$message.error(str + "库存不足,请入库");
                loading.close();
              } else {
                this.$message.success("出库成功");
                this.recordInfo.recordData = [];
                this.$router.back(-1);
                loading.close();
              }
            }
          });
        } else {
          return false;
        }
      });
    },
    GetOrderDetail: function (order_id) {
      const params = {
        id: order_id,
      };
      getDrugWarehouseOutInfo(params).then((response) => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          var manulist = response.data.data.manulist;
          this.manufacturerList = manulist;
          this.dealerList = response.data.data.dealerList;

          for (let i = 0; i < response.data.data.list.length; i++) {
            (response.data.data.list[i].drug_name =
              response.data.data.list[i].drug.drug_name),
              (response.data.data.list[i].drug_type =
                response.data.data.list[i].drug.drug_type);
            response.data.data.list[i].name =
              response.data.data.list[i].drug.dose +
              response.data.data.list[i].drug.dose_unit +
              "*" +
              response.data.data.list[i].drug.min_number +
              response.data.data.list[i].drug.min_unit +
              "/" +
              response.data.data.list[i].drug.max_unit;
            // if(response.data.data.list[i].count%response.data.data.list[i].drug.min_number == 0){
            //    response.data.data.list[i].count = response.data.data.list[i].count/response.data.data.list[i].drug.min_number
            //    response.data.data.list[i].count_unit = response.data.data.list[i].drug.max_unit
            // }
            if (
              response.data.data.list[i].number == "0" ||
              response.data.data.list[i].number == 0
            ) {
              response.data.data.list[i].number = "";
            } else {
              response.data.data.list[i].number =
                response.data.data.list[i].number;
            }

            if (
              response.data.data.list[i].manufacturer == "0" ||
              response.data.data.list[i].manufacturer == 0
            ) {
              response.data.data.list[i].manufacturer = "";
            } else {
              response.data.data.list[i].manufacturer =
                response.data.data.list[i].drug.manufacturer;
            }

            response.data.data.list[i].price =
              response.data.data.list[i].price.toString();
            response.data.data.list[i].retail_price =
              response.data.data.list[i].price.toString();
            response.data.data.list[i].count =
              response.data.data.list[i].count.toString();

            if (
              response.data.data.list[i].dealer == "0" ||
              response.data.data.list[i].dealer == 0
            ) {
              response.data.data.list[i].dealer = "";
            } else {
              response.data.data.list[i].dealer =
                response.data.data.list[i].dealer;
            }

            response.data.data.list[i].product_date = this.getTime(
              response.data.data.list[i].product_date,
              "{y}-{h}-{d}"
            );
            response.data.data.list[i].expiry_date = this.getTime(
              response.data.data.list[i].expiry_date,
              "{y}-{h}-{d}"
            );

            response.data.data.list[i].stock_count =
              response.data.data.list[i].stock_count;
            response.data.data.list[i].last_price =
              response.data.data.list[i].last_price;
            this.recordInfo.recordData.push(response.data.data.list[i]);
          }
          console.log(
            "我爱的我的2级大风32323232323",
            this.recordInfo.recordData
          );
          for (let j = 0; j < this.recordInfo.recordData.length; j++) {
            for (let i = 0; i < manulist.length; i++) {
              if (
                this.recordInfo.recordData[j].manufacturer == manulist[i].id
              ) {
                this.recordInfo.recordData[j].manufacturer =
                  manulist[i].manufacturer_name;
              }
            }
            for (let z = 0; z < this.dealerList.length; z++) {
              if (
                this.recordInfo.recordData[j].dealer == this.dealerList[z].id
              ) {
                this.recordInfo.recordData[j].dealer =
                  this.dealerList[z].dealer_name;
              }
            }
          }
          console.log("wode 23232323232322323", this.recordInfo.recordData);
          this.warehouseOut = response.data.data.info;
          this.storehouse_id = response.data.data.info.storehouse_id;
          this.warehouse_out_time = this.getTime(
            this.warehouseOut.warehouse_out_time,
            "{y}-{m}-{d}"
          );
          this.houstList = response.data.data.houstList;
          this.doctorList = response.data.data.doctorList;
        }

        if (this.recordInfo.recordData.length == 0) {
          const tempObj = {};
          tempObj["id"] = 0;
          tempObj["drug_id"] = 0;
          tempObj["retail_price"] = "";

          tempObj["count"] = "";
          tempObj["price"] = "";
          tempObj["remark"] = "";
          this.recordInfo.recordData.push(tempObj);
        }
      });
    },
    calculate: function (val) {
      if (val == 0) {
        return "";
      }
      return Math.round(parseFloat(val) * 100) / 100;
    },
    changeManufacturer(val) {
      this.propForm.manufacturer = val;
    },
    changeDealer(val) {
      this.propForm.dealer = val;
    },
    querySearchAsync(keyword, cb) {
      let key = "";
      if (keyword != undefined) {
        key = keyword;
      }
      let searchArray = [];

      postSearchDrugList(key, this.storehouse_id).then((response) => {
        if (response.data.state == 1) {
          searchArray = response.data.data.list;

          var list = response.data.data.list;

          this.drugList = list;
          var manufacturerList = response.data.data.manufacturerList;
          this.manufacturerList = manufacturerList;
          for (let i = 0; i < this.drugList.length; i++) {
            for (let j = 0; j < this.manufacturerList.length; j++) {
              if (
                this.drugList[i].manufacturer == this.manufacturerList[j].id
              ) {
                this.drugList[i].manufacturer =
                  this.manufacturerList[j].manufacturer_name;
              }
            }
          }

          cb(this.drugList);
        } else {
          cb([]);
        }
      });
    },
    changeDrugName(val) {
      this.currentIndex = val;
    },
    handleSelect(val) {
      var arr = [];
      for (let i = 0; i < this.recordInfo.recordData.length; i++) {
        arr.push(this.recordInfo.recordData[i].drug_id);
      }
      var str = arr.join(",");

      if (str.indexOf(val.id) != -1) {
        this.$message.error("该药品已存在列表中");
        return;
      }
      this.getDrugBatchNumber(val.id);
      var str_count = "";
      var total_count = 0;
      for (let i = 0; i < val.drug_warehouse_info.length; i++) {
        if (val.max_unit == val.drug_warehouse_info[i].max_unit) {
          val.drug_warehouse_info[i].stock_max_number =
            val.drug_warehouse_info[i].stock_max_number * val.min_number;
        }
      }

      str_count = this.getWarehoseInfo(
        val.drug_warehouse_info,
        val.max_unit,
        val.min_unit,
        val.min_number
      );
      total_count = this.getWarehoseInfoTwo(val.drug_warehouse_info);
      for (let i = 0; i < this.recordInfo.recordData.length; i++) {
        if (i == this.currentIndex) {
          this.recordInfo.recordData[i].drug_id = val.id;
          this.recordInfo.recordData[i].drug_name = val.drug_name;
          this.recordInfo.recordData[i].drug_type = val.drug_type;
          this.recordInfo.recordData[i].name =
            val.dose +
            val.dose_unit +
            "*" +
            val.min_number +
            val.min_unit +
            "/" +
            val.max_unit;
          this.recordInfo.recordData[i].price = val.retail_price;
          this.recordInfo.recordData[i].manufacturer = val.manufacturer;
          this.recordInfo.recordData[i].number = val.number;
          this.recordInfo.recordData[i].max_unit = val.max_unit;
          this.recordInfo.recordData[i].min_unit = val.min_unit;
          this.recordInfo.recordData[i].last_price = val.last_price;
          this.recordInfo.recordData[i].stock_count = str_count;
          this.recordInfo.recordData[i].total_count = total_count;
          this.recordInfo.recordData[i].max_unit_fisrt = val.max_unit;
          this.recordInfo.recordData[i].min_unit_fisrt = val.min_unit;
          this.recordInfo.recordData[i].count_unit = val.max_unit;
          if (val.dealer == 0) {
            this.recordInfo.recordData[i].dealer = "";
          } else {
            this.recordInfo.recordData[i].dealer = val.dealer;
          }
        }
      }
    },
    getDictionaryDataConfig(module, filed_name) {
      return getDictionaryDataConfig(module, filed_name);
    },
    getDataConfig(module, filed_name) {
      return getDataConfig(module, filed_name);
    },
    getDrugBatchNumber(id) {
      var params = {
        id: id,
      };
      getDrugBatchNumber(params).then((response) => {
        if (response.data.state == 1) {
          var list = response.data.data.list;

          this.numberList = list;
        }
      });
    },
    getWarehoseInfo(arr, max_unit, min_unit, min_number) {
      var total = 0;
      var max_str = "";
      var min_str = "";
      if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          total += parseInt(arr[i].stock_max_number + arr[i].stock_min_number);
        }
      }
      if (total < min_number) {
        min_str = total + min_unit;
      }
      if (total == 0) {
        min_str = "";
        max_str = "";
      }
      if (total >= min_number) {
        if (parseInt(total / min_number) != 0) {
          max_str = parseInt(total / min_number) + max_unit;
        }
        if (total % min_number != 0) {
          min_str = (total % min_number) + min_unit;
        }
      }
      return max_str + min_str;
    },
    getWarehoseInfoTwo(arr) {
      var total = 0;
      if (arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          total += parseInt(arr[i].stock_max_number + arr[i].stock_min_number);
        }
      }
      return total;
    },
    getSupplyCount(
      count,
      max_unit,
      min_number,
      index,
      total_count,
      max_unit_fisrt,
      min_unit_fisrt
    ) {
      var count_total = 0;
      if (max_unit == max_unit_fisrt) {
        count_total = count * min_number;
      }

      if (max_unit == min_unit_fisrt) {
        count_total = count;
      }

      if (count_total > total_count) {
        this.$message.error("出库数量不能大于剩余库存");
        for (let i = 0; i < this.recordInfo.recordData.length; i++) {
          if (index == i) {
            this.recordInfo.recordData[i].count = "";
          }
        }
      } else {
        for (let i = 0; i < this.recordInfo.recordData.length; i++) {
          if (index == i) {
            this.recordInfo.recordData[i].count = count;
          }
        }
      }
      console.log("count_toal23233232", count_total);
    },
  },
  created() {
    this.drugTypeList = getDictionaryDataConfig("system", "drug_type");
    // this.GetConfigInfo()
    this.propForm.goodUnit = this.$store.getters.good_unit;
    const order_id = this.$route.query.id;
    this.GetOrderDetail(order_id);
    var sys = this.$route.query.is_sys;
    if (sys == 0) {
      this.drug_show = false;
    }
    if (sys == 1) {
      this.drug_show = true;
    }
    if (sys == 12) {
      this.drug_show = true;
    }
    if (parseInt(this.$route.query.supply_cancel_out_id) > 0) {
      this.drug_show = true;
    }
    this.unitList = getDataConfig("hemodialysis", "units");
  },
};
</script>

<style rel="stylesheet/css" lang="scss" scoped>
.information {
  border: 1px #dcdfe6 solid;
  padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }
}

.edit_separater {
  border-top: 1px solid rgb(233, 233, 233);
  margin-top: 15px;
  margin-bottom: 15px;
}

</style>

<style>
.sign-and-weigh-box .sign-and-weigh-box-patients .cell {
  font-size: 12px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
  background: #6fb5fa;
}

</style>
