<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="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入借出单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请日期" prop="applicationDate">
        <el-date-picker clearable
          v-model="queryParams.applicationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借出部门" prop="lendDepartment">
        <el-input
          v-model="queryParams.lendDepartment"
          placeholder="请输入借出部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经手人" prop="bySponsor">
        <el-input
          v-model="queryParams.bySponsor"
          placeholder="请输入经手人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="往来单位" prop="passingUnit">
        <el-input
          v-model="queryParams.passingUnit"
          placeholder="请输入往来单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="preparedBy">
        <el-input
          v-model="queryParams.preparedBy"
          placeholder="请输入制单人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单部门" prop="preparedDepartment">
        <el-input
          v-model="queryParams.preparedDepartment"
          placeholder="请输入制单部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="corporation">
        <el-input
          v-model="queryParams.corporation"
          placeholder="请输入所属公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录状态" prop="recordStatus">
        <el-select v-model="queryParams.recordStatus" placeholder="请选择记录状态" clearable>
          <el-option
            v-for="dict in dict.type.asset_record_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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>
    </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="['change:assetLend: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="['change:assetLend: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="['change:assetLend: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="['change:assetLend:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetLendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="借出单号" align="center" prop="orderNumber" />
      <el-table-column label="资产总原值" align="center" prop="assetTotalOriginalValue" />
      <el-table-column label="资产总净值" align="center" prop="assetTotalNetValue" />
      <el-table-column label="申请日期" align="center" prop="applicationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借出部门" align="center" prop="lendDepartment" />
      <el-table-column label="经手人" align="center" prop="bySponsor" />
      <el-table-column label="往来单位" align="center" prop="passingUnit" />
      <el-table-column label="借用说明" align="center" prop="reason" />
      <el-table-column label="制单人" align="center" prop="preparedBy" />
      <el-table-column label="制单部门" align="center" prop="preparedDepartment" />
      <el-table-column label="所属公司" align="center" prop="corporation" />
      <el-table-column label="记录状态" align="center" prop="recordStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_record_status" :value="scope.row.recordStatus"/>
        </template>
      </el-table-column>
      <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="['change:assetLend:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['change:assetLend: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="借出单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入借出单号" />
        </el-form-item>
        <el-form-item label="资产总原值" prop="assetTotalOriginalValue">
          <el-input v-model="form.assetTotalOriginalValue" placeholder="请输入资产总原值" />
        </el-form-item>
        <el-form-item label="资产总净值" prop="assetTotalNetValue">
          <el-input v-model="form.assetTotalNetValue" placeholder="请输入资产总净值" />
        </el-form-item>
        <el-form-item label="申请日期" prop="applicationDate">
          <el-date-picker clearable
            v-model="form.applicationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借出部门" prop="lendDepartment">
          <el-input v-model="form.lendDepartment" placeholder="请输入借出部门" />
        </el-form-item>
        <el-form-item label="经手人" prop="bySponsor">
          <el-input v-model="form.bySponsor" placeholder="请输入经手人" />
        </el-form-item>
        <el-form-item label="往来单位" prop="passingUnit">
          <el-input v-model="form.passingUnit" placeholder="请输入往来单位" />
        </el-form-item>
        <el-form-item label="借用说明" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入借用说明" />
        </el-form-item>
        <el-form-item label="制单人" prop="preparedBy">
          <el-input v-model="form.preparedBy" placeholder="请输入制单人" />
        </el-form-item>
        <el-form-item label="制单部门" prop="preparedDepartment">
          <el-input v-model="form.preparedDepartment" placeholder="请输入制单部门" />
        </el-form-item>
        <el-form-item label="所属公司" prop="corporation">
          <el-input v-model="form.corporation" placeholder="请输入所属公司" />
        </el-form-item>
        <el-form-item label="记录状态" prop="recordStatus">
          <el-select v-model="form.recordStatus" placeholder="请选择记录状态">
            <el-option
              v-for="dict in dict.type.asset_record_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </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 { listAssetLend, getAssetLend, delAssetLend, addAssetLend, updateAssetLend } from "@/api/change/assetLend";

export default {
  name: "AssetLend",
  dicts: ['asset_record_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产借出表格数据
      assetLendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        applicationDate: null,
        lendDepartment: null,
        bySponsor: null,
        passingUnit: null,
        preparedBy: null,
        preparedDepartment: null,
        corporation: null,
        recordStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "借出单号不能为空", trigger: "blur" }
        ],
        bySponsor: [
          { required: true, message: "经手人不能为空", trigger: "blur" }
        ],
        passingUnit: [
          { required: true, message: "往来单位不能为空", trigger: "blur" }
        ],
        preparedBy: [
          { required: true, message: "制单人不能为空", trigger: "blur" }
        ],
        recordStatus: [
          { required: true, message: "记录状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产借出列表 */
    getList() {
      this.loading = true;
      listAssetLend(this.queryParams).then(response => {
        this.assetLendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        assetTotalOriginalValue: null,
        assetTotalNetValue: null,
        applicationDate: null,
        lendDepartment: null,
        bySponsor: null,
        passingUnit: null,
        reason: null,
        preparedBy: null,
        preparedDepartment: null,
        corporation: null,
        recordStatus: 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
      getAssetLend(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) {
            updateAssetLend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssetLend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产借出编号为"' + ids + '"的数据项？').then(function() {
        return delAssetLend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('change/assetLend/export', {
        ...this.queryParams
      }, `assetLend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
