<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>
          <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
      size="mini"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" align="center" width="55" label="序号" />
      <!-- <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="site" />
      <el-table-column label="库位号" align="center" prop="locatorNum" />

      <el-table-column label="修改人" align="center" prop="siteBy" />
      <el-table-column label="修改时间" align="center" prop="siteTime">
        <template slot-scope="scope">{{scope.row.siteTime|formatTime}}</template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inventory:inventory:edit']"
            >设置</el-button
          >
        </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="site">
          <el-input v-model="form.site" placeholder="请输入摆放位置" />
        </el-form-item>
        <el-form-item label="库位号" prop="locatorNum">
          <el-input v-model="form.locatorNum" 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,
  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" },
        ],
      },
    };
  },
  filters: {
    formatTime (val) {
      if (!val) return
      var t = new Date(val)
      var y = t.getFullYear(t)
      var m = t.getMonth() + 1
      var d = t.getDate()
      var h = t.getHours()
      var i = t.getMinutes()
      var s = t.getSeconds()
      m = m < 10 ? '0' + m : m
      d = d < 10 ? '0' + d : d
      h = h < 10 ? '0' + h : h
      i = i < 10 ? '0' + i : i
      s = s < 10 ? '0' + s : s
      return y + '/' + m + '/' + d + ' ' + h + ':' + i + ':' + s
    }
  },
  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) => {
        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,
        site: null,
        locatorNum: null,
        stockMin: 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({
              id: this.form.id,
              site: this.form.site,
              locatorNum: this.form.locatorNum
            }).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(
        '是否确认删除摆放位置编号为"' + ids + '"的数据项?',
        "警告",
        {
          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>
