<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <div class="query-box">
        <div class="query-box__left">
          <el-form-item label="" prop="drugsName">
            <el-input
              v-model="queryParams.drugsName"
              placeholder="请输入药品名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="drugsType">
            <el-select
              v-model="queryParams.drugsType"
              placeholder="请选择药品分类"
              clearable
              size="small"
            >
              <el-option
                v-for="item in drugsTypeArr"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="drugsSpecsType">
            <el-select
              v-model="queryParams.drugsSpecsType"
              placeholder="请选择中药类型"
              clearable
              size="small"
            >
              <el-option
                v-for="item in chinesMedicineTypeArr"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="query-box__right">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-table
      v-loading="loading"
      :data="inventoryList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" align="center" label="序号" width="55"/>
      <!-- <el-table-column label="药房id" align="center" prop="pharmacyId" /> -->
      <el-table-column
        label="药品名称"
        align="center"
        prop="drugsName"
        width="110"
      />
      <el-table-column
        label="药品规格"
        align="center"
        prop="specsName"
        width="110"
      />
      <el-table-column
        label="药品产地"
        align="center"
        prop="priceName"
        width="110"
      />
      <el-table-column
        label="单位"
        align="center"
        prop="packingUnit"
        width="110"
      >
        <!-- <template slot-scope="scope">
          <dict-tag :options="unitArr" :value="scope.row.packingUnit" />
        </template> -->

      </el-table-column>
      <el-table-column
        label="中药类型"
        align="center"
        prop="drugsSpecsType"
        width="110"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="chinesMedicineTypeArr"
            :value="scope.row.drugsSpecsType"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="药品别名"
        align="center"
        prop="drugsAlias"
        width="110"
      />
      <el-table-column
        label="进货单价"
        align="center"
        prop="purchasePrice"
        width="110"
      />
      <el-table-column label="零售单价" align="center" prop="retailPrice" />
      <el-table-column label="是否停用" align="center">
        <template slot-scope="scope">
          {{ scope.row.priceState == 1 ? "停用" : "启用" }}
        </template>
      </el-table-column>
      <el-table-column label="是否作废" align="center">
        <template slot-scope="scope">
          {{ scope.row.priceDel == 0 ? "作废" : "正常" }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库存上限" prop="stockMax">
          <el-input v-model="form.stockMax" placeholder="请输入库存上限" />
        </el-form-item>
        <el-form-item label="库存下限" prop="stockMin">
          <el-input v-model="form.stockMin" placeholder="请输入库存下限" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  // listInventory,
  selectInventorDrugsList,
  getInventory,
  delInventory,
  addInventory,
  updateInventory,
  exportInventory,
} from "@/api/inventory/inventory";

export default {
  name: "Inventory",
  data() {
    return {
      drugsTypeArr: [],
      chinesMedicineTypeArr: [],
      unitArr: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        drugsType: null,
        drugsSpecsType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pharmacyId: [
          { required: true, message: "药房id不能为空", trigger: "blur" },
        ],
        validTime: [
          { required: true, message: "有效时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDicts("drugs_type").then((response) => {
      this.drugsTypeArr = response.data;
    });

    this.getDicts("chinese_medicine_type").then((response) => {
      this.chinesMedicineTypeArr = response.data;
    });
    this.getDicts("unit").then((response) => {
      this.unitArr = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询药品库存列表 */
    getList() {
      this.loading = true;
      // listInventory(this.queryParams).then((response) => {
      selectInventorDrugsList(this.queryParams).then((response) => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        drugsName: null,
        specsId: null,
        priceId: null,
        stock: null,
        purchasePrice: null,
        retailPrice: null,
        approvalCode: null,
        stockMax: null,
        stockMin: null,
        site: null,
        productionCode: null,
        validTime: null,
        del: null,
        state: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改药品库存";
      this.form = row;
      // getInventory({ id, pageNum: 1, pageSize: 10 }).then((response) => {
      //   this.form = response.rows[0];
      //   //console.log(response.rows);

      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '确定需要删除选中数据吗？',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInventory(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有药品库存数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInventory(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
