<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="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入条形码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <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="kind">
        <el-select v-model="queryParams.kind" placeholder="请选择资产种类" clearable>
          <el-option
            v-for="dict in dict.type.asset_kind"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资产分类" prop="categoryNumber">
        <el-select v-model="queryParams.categoryNumber" filterable placeholder="请输入资产分类">
          <el-option
            v-for="item in assetCategoryList"
            :key="item.number"
            :label="item.name"
            :value="item.number">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="取得时间" prop="buildDate">
        <el-date-picker clearable
          v-model="queryParams.buildDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择取得时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="存放地点" prop="placeName">
        <el-select v-model="queryParams.placeName" filterable placeholder="请输入存放地点">
          <el-option
            v-for="item in placeList"
            :key="item.id"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产用途" prop="purpose">
        <el-select v-model="queryParams.purpose" placeholder="请选择资产用途" clearable>
          <el-option
            v-for="dict in dict.type.asset_prupose"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用部门" prop="deptId">
        <el-cascader
          v-model="queryParams.deptId"
          :options="deptList"
          :show-all-levels="false"
          clearable :props="{value: 'id'}"
          placeholder="请选择使用部门"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="登记人" prop="registrant">
        <el-input
          v-model="queryParams.registrant"
          placeholder="请输入登记人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂商" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          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 label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.asset_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="['asset:asset: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="['asset:asset: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="['asset:asset: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="['asset:asset:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetList" @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="barCode" />
      <el-table-column label="资产编号" align="center" prop="number" />
      <el-table-column label="资产名称" align="center" prop="name" />
      <el-table-column label="资产种类" align="center" prop="kind">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_kind" :value="scope.row.kind"/>
        </template>
      </el-table-column>
      <el-table-column label="资产分类" align="center" prop="categoryNumber">
        <template slot-scope="scope">
          <span>{{getAssetCategory(scope.row.categoryNumber)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="资产原值" align="center" prop="amount" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="取得时间" align="center" prop="buildDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buildDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="存放地点" align="center" prop="placeName" />
      <el-table-column label="资产用途" align="center" prop="purpose">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_prupose" :value="scope.row.purpose"/>
        </template>
      </el-table-column>
      <el-table-column label="使用部门ID" align="center" prop="deptId" />
      <el-table-column label="登记人" align="center" prop="registrant" />
      <el-table-column label="规格型号" align="center" prop="specificationsModel" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="生产厂商" align="center" prop="manufacturer" />
      <el-table-column label="品牌型号" align="center" prop="brand" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_status" :value="scope.row.status"/>
        </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="['asset:asset:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:asset: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="barCode">
          <el-input v-model="form.barCode" placeholder="请输入条形码" />
        </el-form-item>
        <el-form-item label="资产编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="资产名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="资产种类" prop="kind">
          <el-select v-model="form.kind" placeholder="请选择资产种类">
            <el-option
              v-for="dict in dict.type.asset_kind"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产分类编号" prop="categoryNumber">
          <el-input v-model="form.categoryNumber" placeholder="请输入资产分类编号" />
        </el-form-item>
        <el-form-item label="资产原值" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入资产原值" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="取得时间" prop="buildDate">
          <el-date-picker clearable
            v-model="form.buildDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择取得时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="存放地点" prop="placeName">
          <el-input v-model="form.placeName" placeholder="请输入存放地点" />
        </el-form-item>
        <el-form-item label="资产用途" prop="purpose">
          <el-select v-model="form.purpose" placeholder="请选择资产用途">
            <el-option
              v-for="dict in dict.type.asset_prupose"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入使用部门ID" />
        </el-form-item>
        <el-form-item label="登记人" prop="registrant">
          <el-input v-model="form.registrant" placeholder="请输入登记人" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specificationsModel">
          <el-input v-model="form.specificationsModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="生产厂商" prop="manufacturer">
          <el-input v-model="form.manufacturer" 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="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.asset_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 { listAsset, getAsset, delAsset, addAsset, updateAsset } from "@/api/asset/asset";
import { allCategory } from '@/api/asset/category.js'
import { treeselect } from '@/api/system/dept.js'
import { allPlace } from '@/api/asset/place.js'

export default {
  name: "Asset",
  dicts: ['asset_kind', 'asset_prupose', 'asset_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产信息表格数据
      assetList: [],
      // 资产分类数据
      assetCategoryList: [],
      // 部门数据
      deptList: [],
      // 地点数据
      placeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        barCode: null,
        number: null,
        name: null,
        kind: null,
        categoryNumber: null,
        buildDate: null,
        placeName: null,
        purpose: null,
        deptId: null,
        registrant: null,
        remark: null,
        manufacturer: null,
        brand: null,
        supplier: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        barCode: [
          { required: true, message: "条形码不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "资产编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "资产名称不能为空", trigger: "blur" }
        ],
        kind: [
          { required: true, message: "资产种类不能为空", trigger: "change" }
        ],
        categoryNumber: [
          { required: true, message: "资产分类编号不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "资产原值不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        buildDate: [
          { required: true, message: "取得时间不能为空", trigger: "blur" }
        ],
        registrant: [
          { required: true, message: "登记人不能为空", trigger: "blur" }
        ],
        specificationsModel: [
          { required: true, message: "规格型号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.initData();
  },
  methods: {
    /** 初始化数据 */
    initData() {
      this.loading = true;
      const promice = Promise.all([listAsset(this.queryParams), allCategory(), treeselect(), allPlace()])
      promice.then(([response, response2, response3, response4]) => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.assetCategoryList = response2.data
        this.deptList = response3.data
        this.placeList = response4.data
      })
    },
    /** 获取资产分类 */
    getAssetCategory(number) {
      let value = ''
      this.assetCategoryList.forEach(category => {
        if (category.number === number) {
          value = `[${number}]${category.name}`
        }
      });
      return value
    },
    /** 查询资产信息列表 */
    getList() {
      this.loading = true;
      listAsset(this.queryParams).then(response => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        barCode: null,
        number: null,
        name: null,
        kind: null,
        categoryNumber: null,
        amount: null,
        quantity: null,
        buildDate: null,
        placeName: null,
        purpose: null,
        deptId: null,
        registrant: null,
        specificationsModel: null,
        remark: null,
        manufacturer: null,
        brand: null,
        supplier: null,
        status: 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
      getAsset(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) {
            updateAsset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAsset(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 delAsset(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/asset/export', {
        ...this.queryParams
      }, `asset_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
