<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="buyloading" :data="buyList" v-if="clickMaterial" height="300px">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="采购日期" align="center" prop="buyTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购地点" align="center" prop="place" />
      <el-table-column label="采购数量" align="center" prop="totalNum" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="总价" align="center" prop="amount" />
      <el-table-column label="采购人" align="center" prop="buyUser.nickName" />
      <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:buy:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:buy: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="buyTime">
          <el-date-picker clearable v-model="form.buyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择采购日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采购地点" prop="place">
          <el-input v-model="form.place" placeholder="请输入采购地点" />
        </el-form-item>
        <el-form-item label="采购数量" prop="totalNum">
          <el-input-number :controls="false" v-model="form.totalNum" placeholder="请输入采购数量" @blur="computeAmount()" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input-number :controls="false" v-model="form.price" placeholder="请输入单价" @blur="computeAmount()" />
        </el-form-item>
        <el-form-item label="总价" prop="amount">
          <el-tag>{{ form.amount }}</el-tag>
        </el-form-item>
        <el-form-item label="采购人" prop="buyer">
          <el-select v-model="form.buyer" 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="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listMaterialBuy, getMaterialBuy, delMaterialBuy, addMaterialBuy, updateMaterialBuy } from "@/api/oa/material/materialBuy";
import ChooseMateral from '../chooseMateral.vue';
export default {
  components: { ChooseMateral },
  props: {
    clickRow: {
      type: Object,
      require: true,
      default: {}
    },
    clickMaterial: {
      type: String,
      require: true,
    }
  },
  data() {
    return {
      ids:[],
      single: true,
      // 非多个禁用
      multiple: true,
      open: false,
      title: '',
      buyloading: false,
      buyList: [],
      form: {},
      rules: {},
    }
  },
  watch: {
    clickRow: {
      handler(newVal) {
        if (newVal) {
          this.getList();
        }
      },
      immediate: true,  // 在绑定时立即执行一次  
      deep: true       // 深度监听对象的变化  
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      listMaterialBuy({ pageSize: 999,materialId: this.clickRow.materialId }).then(res => {
        this.buyList = res.rows;
        this.buyTotal = res.total
        this.buyloading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buyId != null) {
            updateMaterialBuy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.materialId = this.clickRow.materialId
            addMaterialBuy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = " 新增耗材购买信息";
      this.reset();
      this.computeAmount();
    },
    // 表单重置
    reset() {
      this.form = {
        buyId: null,
        materialId: null,
        buyTime: null,
        place: null,
        totalNum: null,
        price: null,
        amount: null,
        buyer: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.confirmMaterial(row.material)
      const buyId = row.buyId || this.ids
      getMaterialBuy(buyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改耗材购买信息";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buyIds = row.buyId || this.ids;
      this.$modal.confirm('是否确认删除cmc耗材购买编号为"' + buyIds + '"的数据项？').then(function () {
        return delMaterialBuy(buyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    computeAmount() {
      let amount = (this.form.totalNum * this.form.price).toFixed(2);
      this.$set(this.form, 'amount', amount);
      this.$set(this.form, 'buyer', 17);
    },
    confirmMaterial(row) {
      if (row) {
        this.form.materialId = row.materialId;
        if (this.$refs.form)
          this.$refs.form.validateField('materialId')
      }
    },
  },
}
</script>

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