<template>
  <div class="common-box">
    <!--条件-->
    <el-form :model="listQuery" ref="queryForm" :inline="true">
      <el-form-item label="药品信息">
        <el-input
          v-model="listQuery.drugInfo"
          placeholder="请输入药品信息"
        ></el-input>
      </el-form-item>
      <el-form-item label="申领单位">
        <el-input
          v-model="listQuery.orgName"
          placeholder="请输入申领单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="供应商">
        <el-select
          v-model="listQuery.prodentpName"
          placeholder="请选择供应商"
          clearable
          filterable
          remote
          :remote-method="remoteMethod"
          :loading="splerloading"
        >
          <el-option
            v-for="item in splerOptions"
            :key="item.splerId"
            :label="item.splerName"
            :value="item.splerId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData('search')">查询 </el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="right-button">
      <el-button
        :disabled="multipleSelection.length == 0"
        class="lake-Blue"
        type="primary"
        icon="el-icon-thumb"
        @click="procurement()"
        >批量采购
      </el-button>
    </div>
    <!--列表-->
    <div>
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
        fit
        stripe
        border
        @selection-change="handleSelectionChange"
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="药品信息" min-width="300">
          <template slot-scope="scope">
            <div class="drug-name">
              <el-tag v-if="scope.row.drugName">
                {{ scope.row.drugName }}
              </el-tag>
              {{ "【" + scope.row.dosformName + "】" }}
              <el-tag v-if="scope.row.essdrugType">
                {{
                  scope.row.essdrugType === "0"
                    ? "非基药"
                    : scope.row.essdrugType === "1"
                    ? "基药"
                    : scope.row.essdrugType
                }}
              </el-tag>
              <br />
              {{ "生产厂家 【" + scope.row.prodentpName + "】" }}
              <br />
              {{
                "药品规格 【" +
                scope.row.specName +
                "*" +
                scope.row.convrat +
                scope.row.prepunt +
                "/" +
                scope.row.pacUnt +
                "】"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="药品编码" prop="drugListId" width="150" />
        <el-table-column label="批准文号" prop="aprvNo" width="150" />
        <el-table-column label="申领单位" prop="orgName" width="150" />
        <el-table-column label="申领数量" prop="appyCnt" width="150" />
        <el-table-column label="库存数量" prop="invCnt" width="150" />
        <el-table-column label="已出库数量" prop="stoOutCnt" width="150" />
        <!-- <el-table-column label="货位编号" prop="name1" width="150" /> -->
        <el-table-column label="供应商" prop="prodentpName" width="150" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button class="cyan" type="text" @click="procurement(scope.row)"
              >一键采购</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <yl-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getData"
    />
  </div>
</template>

<script>
import ylPagination from "@/components/yl-pagination";
import { getDrugSplerRlts } from "@/api/drugCataManage/index.js";
import { getDrugStroom, autoPurc } from "@/api/storeroom/inventory-reminder.js";
export default {
  components: {
    ylPagination
  },
  data() {
    return {
      splerloading: false,
      splerOptions: [],
      multipleSelection: [],
      list: [],
      listLoading: false,
      listQuery: {
        //条件查询
        drugInfo: "",
        prodentpName: "",
        orgName: "",
        current: 1,
        size: 10
      },
      total: 0 //分页
    };
  },

  created() {
    this.getData();
  },

  methods: {
    remoteMethod(query) {
      if (query !== "") {
        this.splerloading = true;
        getDrugSplerRlts({
          current: 1,
          size: 1000,
          splerName: this.listQuery.prodentpName
        })
          .then((response) => {
            this.splerOptions = response.data.records;
            this.splerloading = false;
          })
          .catch((err) => {
            this.splerloading = false;
          });
      } else {
        this.splerOptions = [];
      }
    },

    //获取列表数据
    getData(type) {
      if (type == "search") {
        this.listQuery.current = 1;
      }
      this.listLoading = true;
      getDrugStroom(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    reset() {
      this.listQuery.drugInfo = "";
      this.listQuery.prodentpName = "";
      this.listQuery.orgName = "";
      this.getData();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 一键采购
    procurement(row) {
      this.$confirm("确认采购", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = { idList: [] };
          if (row) {
            data.idList.push(row.id);
          } else {
            this.multipleSelection.forEach((item) => {
              data.idList.push(item.id);
            });
          }
          autoPurc(data)
            .then((response) => {
              this.$message({
                message: "操作成功",
                type: "success"
              });
              this.getData();
            })
            .catch((err) => {});
        })
        .catch(() => {});
    }
  }
};
</script>

<style>
</style>