<!--
 * @Author: ysh
 * @Date: 2024-08-01 08:58:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-10-22 15:39:11
-->
<template>
  <div class="app-container" style="height:95vh;background-color:#f0f4f7">
    <div class="warpper">
      <div class="left mr10">
        <div class="titles mb20">耗材信息</div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="耗材名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入耗材名称" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="规格型号" prop="series">
            <el-input v-model="queryParams.series" placeholder="请输入规格型号" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="耗材品牌" prop="brand">
            <el-input v-model="queryParams.brand" placeholder="请输入耗材品牌" clearable @keyup.enter.native="handleQuery" />
          </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>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['oa:material:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
              v-hasPermi="['oa:material:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['oa:material:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['oa:material:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange" height="550px"
          highlight-current-row @row-click="handleRow">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="耗材编号" align="center" prop="materialId" />
          <el-table-column label="耗材名称" align="center" prop="name" />
          <!-- <el-table-column label="耗材类别" align="center" prop="type" /> -->
          <el-table-column label="规格型号" align="center" prop="series" />
          <el-table-column label="耗材品牌" align="center" prop="brand" />
          <el-table-column label="存放地点" align="center" prop="place" />
          <el-table-column label="剩余数量" align="center" prop="remain" />
          <el-table-column label="单位" align="center" prop="unit" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['oa:material:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['oa:material:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :autoScroll="false"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>

      <div class="right">
        <div class="right-top">
          <div class="titles">采购信息</div>
          <div class="sub-title mb20">当前点击耗材：{{ clickMaterial ? clickMaterial : '未选择' }}</div>
          <buy-info :clickRow="clickRow" :clickMaterial="clickMaterial"></buy-info>
        </div>
        <div class="right-bottom mt10">
          <div class="titles">发放信息</div>
          <div class="sub-title mb20">当前点击耗材：{{ clickMaterial ? clickMaterial : '未选择' }}</div>
          <give-info :clickRow="clickRow" :clickMaterial="clickMaterial"></give-info>
        </div>
      </div>
    </div>

    <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="name">
          <el-input v-model="form.name" placeholder="请输入耗材名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="series">
          <el-input v-model="form.series" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="耗材品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入耗材品牌" />
        </el-form-item>
        <el-form-item label="存放地点" prop="place">
          <el-input v-model="form.place" placeholder="请输入存放地点" />
        </el-form-item>
        <el-form-item label="剩余数量" prop="remain">
          <el-input v-model="form.remain" v-if="form.materialId"></el-input>
          <span v-else>{{ form.remain }}</span>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" 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 { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial } from "@/api/oa/material/material";
import { listMaterialBuy, getMaterialBuy, delMaterialBuy, addMaterialBuy, updateMaterialBuy } from "@/api/oa/material/materialBuy";
import { listMaterialGive, getMaterialGive, delMaterialGive, addMaterialGive, updateMaterialGive } from "@/api/oa/material/materialGive";
import buyInfo from './components/buyInfo.vue';
import GiveInfo from './components/giveInfo.vue';

export default {
  components: { buyInfo, GiveInfo },
  name: "Material",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      materialList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: null,
        type: null,
        series: null,
        brand: null,
        place: null,
        remain: null,
        unit: null,
      },
      form: {},
      rules: {
      },
      buyList: [],
      buyloading: false,
      buyTotal: 0,
      giveList: [],
      giveloading: false,
      giveTotal: 0,
      clickMaterial: undefined,
      clickRow: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc耗材信息列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        name: null,
        type: null,
        series: null,
        brand: null,
        place: null,
        remain: null,
        unit: null,
        remark: 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.materialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.remain = 0;
      this.title = "添加耗材信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.materialId || this.ids
      getMaterial(materialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改耗材信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialId != null) {
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$modal.confirm('是否确认删除cmc耗材信息编号为"' + materialIds + '"的数据项？').then(function () {
        return delMaterial(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/material/export', {
        ...this.queryParams
      }, `material_${new Date().getTime()}.xlsx`)
    },
    handleRow(row) {
      this.buyloading = true
      let brand = row.brand
      let name = row.name
      let series = row.series
      if (brand && series) {
        this.clickMaterial = name + '-' + brand + '（' + series + '）'
      } else if (brand == null && series != null) {
        this.clickMaterial = name + '（' + series + '）'
      } else if (brand != null && series == null) {
        this.clickMaterial = name + '-' + brand
      } else {
        this.clickMaterial = name
      }
      this.clickRow = row
      listMaterialGive({ materialId: row.materialId }).then(res => {
        this.giveList = res.rows;
        this.giveTotal = res.total
        this.giveloading = false
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.warpper {
  width: 100%;
  display: flex;

  .titles {
    font-family: 'puhuiti';
    font-size: 18px;
    line-height: 20px;
    padding-left: 10px;
    border-left: 3px solid var(--current-color);
    // color: var(--current-color);
  }

  .sub-title {
    font-size: 12px;
    color: #a09a9a;
    padding-left: 13px;
  }

  .left {
    flex: 1;
    background: #fff;
    padding: 10px;
    height: 810px;
  }

  .right {
    flex: 1;

    .right-top {
      width: 100%;
      height: 400px;
      background: #fff;
      padding: 10px;
    }

    .right-bottom {
      width: 100%;
      height: 400px;
      background: #fff;
      padding: 10px;
    }
  }
}
</style>
