<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>
        <el-button type="primary" @click="getData('search')">查询 </el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <div>
      <el-form
        :model="form"
        ref="form"
        label-width="237px"
        class="grid-form-class"
      >
        <el-table
          v-loading="listLoading"
          element-loading-text="加载中"
          :data="form.list"
          fit
          stripe
          border
        >
          <template slot="empty">
            <img src="@/assets/nopage.png" alt />
            <p>暂无数据</p>
          </template>
          <el-table-column
            fixed
            type="index"
            label="序号"
            width="60"
          ></el-table-column>
          <el-table-column label="药品编码" prop="drugListId" width="150" />
          <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="invCnt" width="150" />
          <el-table-column width="150">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>最低库存</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.lowInv'"
                :rules="rules.natureNum"
              >
                <el-input
                  v-model="scope.row.lowInv"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column width="150">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>预警库存</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.warnLnv'"
                :rules="rules.natureNum"
              >
                <el-input
                  v-model="scope.row.warnLnv"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column width="150">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>最高库存</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.highInv'"
                :rules="rules.natureNum"
              >
                <el-input
                  v-model="scope.row.highInv"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column width="110">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>是否预警</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.warnFlg'"
                :rules="rules.selectRequired"
              >
                <el-select
                  v-model="scope.row.warnFlg"
                  placeholder="请选择"
                  clearable
                >
                  <el-option label="是" value="1"> </el-option>
                  <el-option label="否" value="0"> </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column width="110">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>是否自动补货</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.autoSplmFlg'"
                :rules="rules.selectRequired"
              >
                <el-select
                  v-model="scope.row.autoSplmFlg"
                  placeholder="请选择"
                  clearable
                >
                  <el-option label="是" value="1"> </el-option>
                  <el-option label="否" value="0"> </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>

    <div class="foot-button" style="margin-bottom: 0px">
      <el-button type="success" @click="save()">保存</el-button>
    </div>

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

<script>
import {
  getAutoSplmCfg,
  saveAutoSplmCfgBatch
} from "@/api/storeroom/auto-replenish-configuration";
import ylPagination from "@/components/yl-pagination";
import rules from "@/utils/rules";

export default {
  components: {
    ylPagination
  },

  watch: {},

  data() {
    return {
      rules,
      form: {
        list: [] //列表
      },
      btLoading1: false,

      total: 0, //分页
      listLoading: false,
      listQuery: {
        //条件查询
        drugInfo: "",
        current: 1,
        size: 10
      }
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 重置页面
    reset() {
      this.listQuery.drugInfo = "";
      this.getData();
    },

    //获取列表数据
    getData(type) {
      if (type == "search") {
        this.listQuery.current = 1;
      }
      this.listLoading = true;

      getAutoSplmCfg(this.listQuery)
        .then(response => {
          this.form.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
          this.$refs.form.clearValidate();
        })
        .catch(err => {
          this.listLoading = false;
        });
    },

    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$confirm("确认保存", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              let data = [];
              this.form.list.forEach(item => {
                data.push({
                  drugListId: item.drugListId,
                  lowInv: item.lowInv,
                  warnLnv: item.warnLnv,
                  highInv: item.highInv,
                  warnFlg: item.warnFlg,
                  autoSplmFlg: item.autoSplmFlg
                });
              });
              saveAutoSplmCfgBatch(data)
                .then(response => {
                  this.$message({
                    message: "保存成功",
                    type: "success"
                  });
                })
                .catch(err => {});
            })
            .catch(() => {});
        }
      });
    }
  }
};
</script>

<style scoped lang="scss"></style>
