<template>
  <div>
    <el-empty description="请选择左边表格的耗材，显示对应的发放数据" v-if="!clickMaterial"></el-empty>
    <el-row :gutter="10" class="mb8" v-if="clickMaterial">
      <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-row>
    <el-table v-loading="giveloading" :data="giveList" v-if="clickMaterial" height="300px">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="发放时间" align="center" prop="giveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.giveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领用人" align="center" prop="acceptUser.nickName" />
      <el-table-column label="领用部门" align="center" prop="dept.deptName" />
      <el-table-column label="领用数量" align="center" prop="giveNum" />
      <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:give:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:give:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改cmc耗材发放对话框 -->
    <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="materialId">
          <el-tag>{{ clickMaterial }}</el-tag>
        </el-form-item>
        <el-form-item label="发放时间" prop="giveTime">
          <el-date-picker clearable v-model="form.giveTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发放时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领用人" prop="accepter">
          <el-select v-model="form.accepter" filterable clearable>
            <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用部门" prop="useDept">
          <el-select v-model="form.useDept" filterable clearable>
            <el-option v-for="item in $store.state.user.deptList" :key="item.deptId" :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用数量" prop="giveNum">
          <el-input-number :controls="false" v-model="form.giveNum" 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 { listMaterialGive, getMaterialGive, delMaterialGive, addMaterialGive, updateMaterialGive } from "@/api/oa/material/materialGive";
export default {
  props: {
    clickRow: {
      type: Object,
      require: true,
      default: {}
    },
    clickMaterial: {
      type: String,
      require: true,
    }
  },
  data() {
    return {
      ids: [],
      single: true,
      // 非多个禁用
      multiple: true,
      open: false,
      title: '',
      giveloading: false,
      giveList: [],
      giveTotal: 0,
      form: {},
      rules: {},
    }
  },
  watch: {
    clickRow: {
      handler(newVal) {
        if (newVal) {
          this.getList();
        }
      },
      immediate: true,  // 在绑定时立即执行一次  
      deep: true       // 深度监听对象的变化  
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      listMaterialGive({ pageSize: 999, materialId: this.clickRow.materialId }).then(res => {
        this.giveList = res.rows;
        this.giveTotal = res.total
        this.giveloading = false
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增耗材发放记录";
    },
    // 表单重置
    reset() {
      this.form = {
        giveId: null,
        materialId: null,
        giveTime: null,
        accepter: null,
        useDept: null,
        giveNum: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.confirmMaterial(row.material)
      const giveId = row.giveId || this.ids
      getMaterialGive(giveId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改耗材发放记录";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.giveId != null) {
            updateMaterialGive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.materialId = this.clickRow.materialId
            addMaterialGive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const giveIds = row.giveId || this.ids;
      this.$modal.confirm('是否确认删除cmc耗材发放编号为"' + giveIds + '"的数据项？').then(function () {
        return delMaterialGive(giveIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    confirmMaterial(row) {
      if (row) {
        this.form.materialId = row.materialId;
        if (this.$refs.form)
          this.$refs.form.validateField('materialId')
      }
    },
  }
}
</script>

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