<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="dictName">
        <el-input
            v-model="queryParams.dictName"
            placeholder="请输入关键字"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品种类" prop="dictType">
        <el-input
            v-model="queryParams.dictType"
            placeholder="请选择物品种类"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产类型" prop="dictType">
        <el-input
            v-model="queryParams.dictType"
            placeholder="是否是固定资产"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="dictType">
        <el-input
            v-model="queryParams.dictType"
            placeholder="请选择设备类型"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="规格型号"
            clearable
            style="width: 240px"
        >
          <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="物品种类" align="center" prop="typeId" />
      <el-table-column label="设备类型" align="center" prop="dictName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="dictName" :show-overflow-tooltip="true">
<!--        <template slot-scope="scope">-->
<!--          <router-link :to="'/system/dict-data/index/' + scope.row.maId" class="link-type">-->
<!--            <span>{{ scope.row.dictType }}</span>-->
<!--          </router-link>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="maStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.maStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="固定资产编号" align="center" prop="assetsCode" />
      <el-table-column label="设备原编号" align="center" prop="preCode" />
      <el-table-column label="购置批次" align="center" prop="buyTask" />
      <el-table-column label="绑定标签" align="center" prop="typeId" />
      <el-table-column label="所在仓库" align="center" prop="ownHouse" />
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </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="primary"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除</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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="物品种类" prop="dictName">
              <el-input v-model="form.dictName" placeholder="请输入物品种类" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="typeId">
              <el-input v-model="form.typeId" placeholder="请输入设备类型" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="dictName">
              <el-input v-model="form.dictName" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备编号" prop="maCode">
              <el-input v-model="form.maCode" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备状态" prop="maStatus">
              <el-input v-model="form.maStatus" placeholder="请输入设备状态" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定资产编号" prop="assetsCode">
              <el-input v-model="form.assetsCode" placeholder="请输入固定资产编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="设备原编号" prop="preCode">
              <el-input v-model="form.preCode" placeholder="请输入设备原编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购置批次" prop="buyTask">
              <el-input v-model="form.buyTask" placeholder="请输入购置批次" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="绑定标签" prop="dictName">
              <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="所在仓库" prop="ownHouse">
              <el-input v-model="form.ownHouse" placeholder="请输入所在仓库" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="本次检验人员" prop="checkMan">
              <el-input v-model="form.checkMan" placeholder="请输入本次检验人员" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="本次检验时间" prop="thisCheckTime">
              <el-input v-model="form.thisCheckTime" placeholder="请输入本次检验时间" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="下次检验时间" prop="nextCheckTime">
              <el-input v-model="form.nextCheckTime" placeholder="请输入下次检验时间" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产属性" prop="dictName">
              <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出厂时间" prop="outFacTime">
              <el-input v-model="form.outFacTime" placeholder="请输入出厂时间" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出厂编号" prop="outFacCode">
              <el-input v-model="form.outFacCode" placeholder="请输入出厂编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出入库编号" prop="outFacCode">
              <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="管理员" prop="dictName">
              <el-input v-model="form.dictName" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="维修员" prop="dictName">
              <el-input v-model="form.dictName" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注" prop="dictName">
              <el-input v-model="form.dictName" type="textarea" placeholder="" />
            </el-form-item>
          </el-col>

        </el-row>

<!--        <el-form-item label="状态" prop="status">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio-->
<!--                v-for="dict in dict.type.sys_normal_disable"-->
<!--                :key="dict.value"-->
<!--                :label="dict.value"-->
<!--            >{{dict.label}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </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 {addMachine, delMachine, getMachine, listMachine, updateMachine} from "@/api/store/tools";
export default {
  name: "Dict",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMachine(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.typeList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        maId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const maId = row.maId || this.ids
      getMachine(maId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.maId != undefined) {
            updateMachine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMachine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maIds = row.maId || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + maIds + '"的数据项？').then(function() {
        return delMachine(maIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
