<template>
    <div class="app-container" id="">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="时间范围" prop="time">
            <el-date-picker
                v-model="queryParams.time"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                type="daterange"
                value-format="yyyy-MM-dd"
                style="width: 240px"
            ></el-date-picker>
        </el-form-item>
        <el-form-item label="关键字" prop="keyWord">
          <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入关键字"
            clearable :maxlength="20"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="往来单位" prop="unitId">
                    <el-cascader v-model="unitId"
                        :show-all-levels="false"
                        :options="unitList"
                        :props="selectTreeProps"
                        filterable
                        clearable
                        collapse-tags
                        @change="unitChange"
                        placeholder="请选择往来单位"
                    ></el-cascader> 
        </el-form-item>
        <el-form-item label="工程名称" prop="proId">
            <el-cascader v-model="projectId"
                    :show-all-levels="false"
                    :options="proList"
                    :props="selectTreeProps"
                    filterable clearable
                    collapse-tags
                    @change="proChange"
                    placeholder="请选择工程名称"
                ></el-cascader>
        </el-form-item>
        <el-form-item label="物资名称" prop="typeName">
            <el-input
                v-model="queryParams.typeName"
                placeholder="请输入物资名称"
                clearable :maxlength="20"
                style="width: 240px"
            />
        </el-form-item>
        <el-form-item label="设备状态" prop="statusName">
          <el-select
              v-model="queryParams.statusName"
              filterable clearable
              style="width: 240px"
              placeholder="请选择"
          >
              <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              >
              </el-option>
          </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-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                >导出</el-button
            > 
        </el-form-item>
      </el-form>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 
      <el-table v-loading="loading" :data="tableList" border>  
          <el-table-column label="序号" align="center" width="80" type="index">
                <template scope="scope"> 
                    <span>{{ (queryParams.pageNum - 1) * 10 + scope.$index+1 }}</span>
                </template> 
          </el-table-column> 
          <el-table-column label="协议号" align="center" prop="agreementCode" :show-overflow-tooltip="true"/>
          <el-table-column label="工程名称" align="center" prop="proName" :show-overflow-tooltip="true"/>
          <el-table-column label="往来单位" align="center" prop="unitName" :show-overflow-tooltip="true"/>
          <el-table-column label="物资名称" align="center" prop="typeName" :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true"/>
          <el-table-column label="计量单位" align="center" prop="unit" :show-overflow-tooltip="true"/> 
          <el-table-column label="设备编码" align="center" prop="maCode" :show-overflow-tooltip="true"/> 
          <el-table-column label="领用人员" align="center" prop="leaseMan" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="领用时间" align="center" prop="leaseDate" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="退料人员" align="center" prop="backMan" :show-overflow-tooltip="true"></el-table-column> 
          <el-table-column label="退料时间" align="center" prop="backDate" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="当前状态" align="center" prop="statusName" :show-overflow-tooltip="true"/> 
      </el-table> 
      <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          :page-sizes="[5, 10, 15, 20, 30]"
          @pagination="getList"
      />  
    </div>
</template>

<script>
import {
  getMachineHistoryRecordListApi
} from '@/api/stquery/stquery';
import { 
    getProjectList,
    getUnitList, 
} from '@/api/back/index.js'
export default {
    name: '',
    dicts: [],
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 弹出层标题
            title: '查看',
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            selectTreeProps: {
                children: 'children',
                label: 'name',
                // multiple: false,
                value: 'id',
                // multiple: true,
            },
            unitId: null,
            projectId: null,
            unitList: [],
            proList: [],
            statusList:[{id:'在用',name:'在用'},{id:'在退',name:'在退'}],
            // 总条数
            total: 0,
            // 主表格数据
            tableList: [], 
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                time: null,
                keyWord: null,
                typeName: null, //物资名称 
                statusName: null //状态
            }, 
        }
    },
    created() {
        this.initSelectData()
        this.getList()
    },
    methods: {
      initSelectData() {
          this.GetUnitData()
          this.GetProData()
      },
      // 获取 来往单位 列表数据
      async GetUnitData() {
              const params = {
                  projectId: this.queryParams.proId /*  */,
              }
              const res = await getUnitList(params)
              this.unitList = res.data
      },
      unitChange(val){
          if(val&&val.length>0){
              this.queryParams.unitId=this.unitId[this.unitId.length - 1]
          }else if(val&&val.length==0){
              this.queryParams.unitId=""
          }
          this.GetProData()
      }, 
      // 获取 工程名称 列表数据
      async GetProData() {
              const params = { unitId: this.queryParams.unitId}
              const res = await getProjectList(params)
              this.proList = res.data
      },
      proChange(val){
          if(val&&val.length>0){
              this.queryParams.proId=this.projectId[this.projectId.length - 1]
          }else if(val&&val.length==0){
              this.queryParams.proId=""
          }
          this.GetUnitData()
      },

      // 获取列表
      async getList() {
          this.loading = true
          const params = {
                unitId: this.queryParams.unitId,
                proId: this.queryParams.proId,
                keyWord: this.queryParams.keyWord,
                typeName: this.queryParams.typeName,
                statusName: this.queryParams.statusName,
                startTime: this.queryParams.time && this.queryParams.time[0],
                endTime: this.queryParams.time && this.queryParams.time[1],
                pageSize: this.queryParams.pageSize,
                pageNum: this.queryParams.pageNum
            }
          const res = await getMachineHistoryRecordListApi(params)
          this.loading = false
          if(res.data.rows.length>0){
            this.tableList = res.data.rows;
          }else{
            this.tableList=[]
          }
          this.total = res.data.total;
      },
      /** 搜索按钮操作 */
      handleQuery() {
          this.queryParams.pageNum = 1
          this.getList()
      }, 
      /** 重置按钮操作 */
      resetQuery() { 
        this.queryParams.time=[]
        this.unitId=[]
        this.projectId=[]
        this.resetForm('queryForm')
        this.handleQuery()
      }, 
      /** 导出按钮操作 */
      handleExport() {
          this.download(
              'material/complex_query/exportMachineHistoryRecord',
              {
                  ...this.queryParams,
              },
              `综合查询_设备使用追溯_${new Date().getTime()}.xlsx`,
          )
      }, 

    },
}
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}
.clickText {
  color: #02a7f0;
  cursor: pointer;
}
</style>
