<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产名称" prop="assetsName">
        <el-input v-model="queryParams.assetsName" 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">新增
        </el-button>
        <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="资产名称" align="center" prop="assetsName"/>
      <el-table-column label="资产属性" align="center" prop="assetsAttribute">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_attributes" :value="scope.row.assetsAttribute"/>
        </template>
      </el-table-column>
      <el-table-column label="资产分类" align="center" prop="assetsTypeName"/>
      <el-table-column label="规格型号" align="center" prop="assetsSpecs"/>
      <el-table-column label="单位" align="center" prop="assetsUnit"/>
      <el-table-column label="库存数量" align="center" prop="assetsNumbers"/>
      <el-table-column label="存放地点" align="center" prop="depositPlace" min-width="150px"/>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:assets:edit']">编辑
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleEnterStorage(scope.row)"
                     v-hasPermi="['system:assets:remove']">入库记录
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleOutStorage(scope.row)"
                     v-hasPermi="['system:assets:remove']">出库记录
          </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="680px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资产名称" prop="assetsName">
              <el-input v-model="form.assetsName" placeholder="请输入资产名称"/>
            </el-form-item>
            <el-form-item label="规格型号" prop="assetsSpecs">
              <el-input v-model="form.assetsSpecs" placeholder="请输入规格型号"/>
            </el-form-item>
            <el-form-item label="单位" prop="assetsUnit">
              <el-input v-model="form.assetsUnit" placeholder="请输入单位"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存数量" prop="assetsNumbers">
              <el-input v-model="form.assetsNumbers" placeholder="请输入库存数量" disabled/>
            </el-form-item>
            <el-form-item label="资产属性" prop="assetsAttribute">
              <el-select v-model="form.assetsAttribute" placeholder="请选择资产属性" style="width:100%">
                <el-option v-for="dicts in dict.type.asset_attributes" :key="dicts.value" :value="dicts.value"
                           :label="dicts.label"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="资产分类" prop="assetsTypeName">
              <el-input v-model="form.assetsTypeName" placeholder="请选择资产分类"
                        @click.native="selectAssetsType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="存放地点" prop="depositPlace">
          <el-input v-model="form.depositPlace" placeholder="请输入存放地点"/>
        </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>
    <!-- 选择资产分类 -->
    <el-drawer ref="refDrawer" :title="asseTtitle" :visible.sync="drawer">
      <Treeselect v-model="treeData" @select="assetsIdAndName" :options="assetsOptions" :disable-branch-nodes="true"
                  :show-count="true"
                  placeholder="请选择资产分类"/>
    </el-drawer>
    <!-- 入库新增对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="1000px">
      <el-form ref="form1" :model="form1" :rules="rules" label-width="80px" :inline="true">
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="form1.assetsName"></el-input>
        </el-form-item>
        <el-form-item label="规格型号" prop="assetsSpecs">
          <el-input v-model="form1.assetsSpecs"></el-input>
        </el-form-item>
        <el-form-item label="资产属性" prop="assetsUnit">
          <el-input v-model="form1.assetsUnit"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="assetsNumbers">
          <el-input v-model="form1.assetsNumbers"></el-input>
        </el-form-item>

        <el-form-item label="入库数量" prop="enterNumbers">
          <el-input v-model="form1.assetsAttribute" placeholder="请输入入库数量"/>
        </el-form-item>

        <el-form-item label="入库时间" prop="enterDate">
          <el-input v-model="form1.enterDate" placeholder="请输入入库时间"/>
        </el-form-item>

        <el-form-item label="入库类型" prop="enterType">
          <el-input v-model="form1.enterType" placeholder="请输入库类型"/>
        </el-form-item>

        <el-form-item label="入库人" prop="enterBy">
          <el-input v-model="form1.enterBy" placeholder="默认当前登陆人"/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form1.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 出库新增对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="1000px">
      <el-form ref="form" :model="form2" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="资产名称" prop="assetsName" readonly>
              <el-input v-model="form2.assetsName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="规格型号" prop="assetsSpecs">
              <el-input v-model="form2.assetsSpecs"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="资产属性" prop="assetsUnit">
              <el-input v-model="form2.assetsUnit"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="库存数量" prop="assetsNumbers">
              <el-input v-model="form2.assetsNumbers"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="出库数量" prop="outNumbers">
              <el-input v-model="form.assetsAttribute" placeholder="请输入入库数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="出库时间" prop="outDate">
              <el-input v-model="form.outDate" placeholder="请输入入库时间"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="出库类型" prop="outType">
              <el-input v-model="form.depositPlace" placeholder="请输入库类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="出库人" prop="outBy">
              <el-input v-model="form.depositPlace" placeholder="默认当前登陆人"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <!--        <el-form-item label="${comment}" prop="delFlag">-->
        <!--          <el-input v-model="form.delFlag" placeholder="请输入${comment}" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listAssets, getAssets, addAssets, updateAssets} from "@/api/smartSchool/logisticsManage/itemRegistration";
import {AddAssetsEnter, EditAssetsEnter} from "../../../../api/smartSchool/logisticsManage/itemRegistration";
import {getAssetsType} from "@/api/smartSchool/officialWork/purchaseApply.js"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  dicts: ["asset_attributes"],
  name: "Assets",
  components: {Treeselect},
  data() {
    return {
      treeData: "",
      // 项目预算详细
      projectDetail: [],
      // 后端传入的资产分类
      assetsOptions: [],
      asseTtitle: "",
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品登记表格数据
      assetsList: [],
      // 弹出层标题
      title: "",
      title1: "入库",
      title2: "出库",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetsName: null,
        assetsSpecs: null,
        assetsNumbers: null,
        assetsUnit: null,
        assetsAttribute: null,
        depositPlace: null,
        assetsType: null,
      },
      // 表单参数
      form: {
        assetsNumbers: 0
      },
      form1: {},
      form2: {},
      // 表单校验
      rules: {
        assetsName: [
          {required: true, message: "资产名称不能为空", trigger: "blur"}
        ],
        assetsSpecs: [
          {required: true, message: "规格型号不能为空", trigger: "blur"}
        ],
        assetsUnit: [
          {required: true, message: "单位不能为空", trigger: "blur"}
        ],
        assetsNumbers: [
          {required: true, message: "库存数量不能为空", trigger: "blur"}
        ],
        assetsAttribute: [
          {required: true, message: "资产属性不能为空", trigger: "blur"}
        ],
        assetsTypeName: [
          {required: true, message: "资产分类不能为空", trigger: "blur"}
        ],
        // assetsType: [
        //   {required: true, message: "资产分类不能为空", trigger: "blur"}
        // ],
        depositPlace: [
          {required: true, message: "存放地点不能为空", trigger: "blur"}
        ],


      }
    };
  },
  created() {
    this.getList();
    this.getType();
  },
  methods: {
    /** 查询物品登记列表 */
    getList() {
      this.loading = true;
      listAssets(this.queryParams).then(response => {
        this.assetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetsName: null,
        assetsSpecs: null,
        assetsNumbers: 0,
        assetsUnit: null,
        assetsAttribute: null,
        depositPlace: null,
        assetsType: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    //  获取资产分类
    getType() {
      getAssetsType().then(response => {
        this.assetsOptions = response.data;
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    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
      getAssets(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 选择资产分类
    selectAssetsType() {
      this.asseTtitle = "资产分类";
      this.treeData = this.form.assetsType
      this.drawer = true;
    },
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form1.id != null) {
            EditAssetsEnter(this.form1).then(response => {
              this.$modal.msgSuccess("入库成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            AddAssetsEnter(this.form1).then(response => {
              this.$modal.msgSuccess("入库成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.id != null) {
            EditAssetsOut(this.form2).then(response => {
              this.$modal.msgSuccess("出库成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            AddAssetsOut(this.form2).then(response => {
              this.$modal.msgSuccess("出库成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete1(row) {
      this.open1 = true
      this.form1 = row
      this.form1.enterBy = ''
      this.form1.enterType = ''

    },
    handleEnterStorage(row) {
      this.$router.push({
        path: "/logisticsManage/itemRegistration/EnterStorage",
        query: {
          id: row.id,
          assetsName: row.assetsName,
          assetsUnit: row.assetsUnit,
          assetsNumbers: row.assetsNumbers,
          assetsSpecs: row.assetsSpecs,
          assetsAttribute: row.assetsAttribute

        }
      })
    },
    handleOutStorage(row) {
      this.$router.push({
        path: "/logisticsManage/itemRegistration/OutStorage",
        query: {
          id: row.id,
          assetsName: row.assetsName,
          assetsUnit: row.assetsUnit,
          assetsNumbers: row.assetsNumbers,
          assetsSpecs: row.assetsSpecs,
          assetsAttribute: row.assetsAttribute
        }
      })
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('system/assets/export', {
    //     ...this.queryParams
    //   }, `assets_${new Date().getTime()}.xlsx`)
    // }
    assetsIdAndName(node) {
      this.$set(this.form, "assetsType", node.id)
      this.$set(this.form, "assetsTypeName", node.label)
      this.drawer = false
    },
  }
};
</script>

