<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="keyWord">
        <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入关键字"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物品种类" prop="itemId">
        <el-select
          v-model="queryParams.itemId"
          placeholder="请选择设备类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="typeItem in itemList"
            :key="typeItem.typeId"
            :label="typeItem.typeName"
            :value="typeItem.typeId"
          />
        </el-select>
      </el-form-item>
    
      <el-form-item label="设备类型" prop="deviceId">
        <el-select
          v-model="queryParams.deviceId"
          placeholder="请选择设备类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="typeItem in typeList"
            :key="typeItem.typeId"
            :label="typeItem.typeName"
            :value="typeItem.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规格型号" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="请选择规格型号"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="model in modelList"
            :key="model.typeId"
            :label="model.typeName"
            :value="model.typeId"
          />
        </el-select>
      </el-form-item> 

      <el-form-item label="资产属性" prop="propId">
        <el-select v-model="queryParams.propId" placeholder="请选择资产属性">
                <el-option
                  v-for="keeper in propList"
                  :key="keeper.propId"
                  :label="keeper.propName"
                  :value="keeper.propId"
                ></el-option>
              </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="devicesList" @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="itemType" />
      <el-table-column label="设备类型" align="center" prop="deviceType" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="specificationType" :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="maStatusName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.maStatusName"/>
        </template>
      </el-table-column>
      <el-table-column label="采购单号" align="center" prop="buyTask" />
      <el-table-column label="固定资产编号" align="center" prop="assetsCode" />
      <el-table-column label="设备原编号" align="center" prop="preCode" />
      
      <el-table-column label="二维码" align="center" prop="" />
      <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="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
              size="mini"
              type="text"
              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="itemType">
              <el-input v-model="form.itemType" placeholder="请输入物品种类" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="deviceType">
              <el-input v-model="form.deviceType" placeholder="请输入设备类型" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="specificationType">
              <el-input v-model="form.specificationType" placeholder="请输入规格型号" disabled/>
            </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="请输入设备编号" disabled/>
            </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="二维码编号" >
              <!-- <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="buyTask">
              <el-input v-model="form.buyTask" placeholder="请输入购置批次" />
            </el-form-item>
          </el-col>
          <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="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="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="propId">
              <el-select v-model="form.propId" placeholder="请选择资产属性">
                <el-option
                  v-for="keeper in propList"
                  :key="keeper.propId"
                  :label="keeper.propName"
                  :value="keeper.propId"
                ></el-option>
              </el-select>
            </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="请输入字典名称"  disabled/>
            </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="16">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" 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-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";
import { getProLists } from "@/api/base/base";
import { imgUpLoad } from "@/api/system/upload";
import { getTypeList } from "@/api/store/warehousing";

export default {
  name: "Dict",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
       //资产类型数据
       propList: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      devicesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],

      itemList:[],
      typeList:[],
      modelList:[],
      // 查询参数
      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();
    this.getPropData();
    this.getTypeList();
  },
  methods: {
    //设备类型
    getTypeList() {
      getTypeList({level:'2'}).then(response => {
          this.itemList = response.data;
        }
      );
      getTypeList({level:'3'}).then(response => {
          this.typeList = response.data;
        }
      );
      getTypeList({level:'4'}).then(response => {
          this.modelList = response.data;
        }
      );
    },
      /** 查询资产属性下拉 */
    getPropData() {
      getProLists().then(response => {
        this.propList = response.rows; 
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.beginTime=this.dateRange[0]
      this.queryParams.endTime=this.dateRange[1]
      listMachine(this.queryParams).then(response => {
            this.devicesList = 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.$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>
