<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 prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="开始日期"
          @change="() => queryParams.endTime = ''"
          style="width: 130px"
        />
      </el-form-item>
      <el-form-item>-</el-form-item>
      <el-form-item prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="结束日期"
          :picker-options="{ disabledDate: (t) => t.getTime() < new Date(queryParams.startTime).getTime() - 86400000}"
          style="width: 130px"
        />
      </el-form-item>
        <el-form-item prop="keyWord">
          <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入关键字"
            clearable :maxlength="20"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item prop="unitId">
            <treeselect
                v-model="queryParams.unitId"
                :options="unitList" :normalizer="normalizer"
                :show-count="true" style="width: 240px" :disable-branch-nodes="true"
                noChildrenText="没有数据了" noOptionsText="没有数据" noResultsText="没有搜索结果"
                placeholder="请选择退料单位" @select="unitChange"
            />
        </el-form-item>
        <el-form-item prop="proId">
            <treeselect
                v-model="queryParams.proId"
                :options="proList" :normalizer="normalizer"
                :show-count="true" style="width: 240px" :disable-branch-nodes="true"
                noChildrenText="没有数据了" noOptionsText="没有数据" noResultsText="没有搜索结果"
                placeholder="请选择退料工程" @select="proChange"
            />
        </el-form-item>
        <el-form-item prop="typeName">
            <el-input
                v-model="queryParams.typeName"
                placeholder="请输入机具名称"
                clearable :maxlength="20"
                style="width: 240px"
            />
        </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 :max-height="650">
          <el-table-column label="序号" align="center" type="index">
                <template scope="scope">
                    <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index+1 }}</span>
                </template>
          </el-table-column>
          <el-table-column label="退料单位" align="center" prop="backUnitName" width="150"/>
          <el-table-column label="退料工程" align="center" prop="projectName" width="150"/>
          <el-table-column label="维修单号" align="center" prop="repairCode" width="150px" :show-overflow-tooltip="true"/>
          <el-table-column label="维修时间" align="center" prop="repairTime" width="150px" :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="unitName" :show-overflow-tooltip="true"/>
          <el-table-column label="退料数量" align="center" prop="repairNum"/>
          <el-table-column label="抽检数量" align="center" prop="num" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="试验数量" align="center" prop="repairNum" width="100"></el-table-column>
          <el-table-column label="抽检人" align="center" prop="backMan"></el-table-column>
          <el-table-column label="试验人" align="center" prop="backMan" width="100"></el-table-column>
          <el-table-column label="额定载荷" align="center" prop="ratedLoad" :show-overflow-tooltip="true"/>
          <el-table-column label="试验载荷" align="center" prop="testLoad" :show-overflow-tooltip="true"/>
          <el-table-column label="持荷时间" align="center" prop="holdingTime" :show-overflow-tooltip="true"/>
      </el-table>
      <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          @pagination="getList"
      />
    </div>
</template>

<script>
import {
  getRepairInspectionApi
} from '@/api/stquery/stquery';
import {
    getProjectList,
    getUnitList,
} from '@/api/back/index.js'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
    name: 'RepairTest',
    dicts: [],
    components: { Treeselect },
    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: [],
            // 总条数
            total: 0,
            // 主表格数据
            tableList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                time: null,
                unitId: null,
                proId: null,
                keyWord: null,
                typeName: null, //物资名称
                statusName: null, //状态
                startTime: null,
                endTime: null,
            },
        }
    },
    created() {
        const end = new Date()
        let start = new Date()
        start.setMonth(start.getMonth() - 1)
        this.queryParams.time = [this.format(start), this.format(end)]
        this.initSelectData()
        this.getList()
    },
    methods: {
      format(date) {
        const y = date.getFullYear()
        const m = String(date.getMonth() + 1).padStart(2, '0')
        const day = String(date.getDate()).padStart(2, '0')
        return `${y}-${m}-${day}`
      },
      initSelectData() {
          this.GetUnitData()
          this.GetProData()
      },
      /** 转换菜单数据结构 */
      normalizer(node) {
            if (node.children && !node.children.length) {
                delete node.children;
            }
            return {
                id: node.id,
                label: node.name,
                children: node.children,
            };
        },
      // 获取 来往单位 列表数据
      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()
        setTimeout(()=>{
            this.GetProData()
        },500)
      },
      // 获取 工程名称 列表数据
      async GetProData() {
              const params = { unitId: this.queryParams.unitId}
              const res = await getProjectList(params)
              this.proList = res.data
              this.queryParams.proId=null
      },
      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()
        setTimeout(()=>{
            this.GetUnitData()
        },500)
      },

      // 获取列表
      async getList() {
        try {
            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.startTime,
                  endTime: this.queryParams.endTime,
                  pageSize: this.queryParams.pageSize,
                  pageNum: this.queryParams.pageNum
              }
            const res = await getRepairInspectionApi(params)
            this.loading = false
            if(res.rows.length>0){
              this.tableList = res.rows;
            }else{
              this.tableList=[]
            }
            this.total = res.total;
        } catch (error) {
            this.loading = false
        } finally {
            this.loading = false
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
          this.queryParams.pageNum = 1
          this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        const end = new Date()
        let start = new Date()
        start.setMonth(start.getMonth() - 1)
        this.queryParams.time = [this.format(start), this.format(end)]
        this.unitId=[]
        this.projectId=[]
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 导出按钮操作 */
      handleExport() {
        const formatTime = (date) => {
          const year = date.getFullYear();
          const month = String(date.getMonth() + 1).padStart(2, '0');
          const day = String(date.getDate()).padStart(2, '0');
          const hours = String(date.getHours()).padStart(2, '0');
          const minutes = String(date.getMinutes()).padStart(2, '0');
          const seconds = String(date.getSeconds()).padStart(2, '0');
          return `${year}${month}${day}_${hours}${minutes}${seconds}`;
        };
        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.startTime,
            endTime: this.queryParams.endTime,
            pageSize: this.queryParams.pageSize,
            pageNum: this.queryParams.pageNum
        }
        const currentTime = formatTime(new Date());
          this.download(
              'material/repair/getRepairInspectionExport',
              {
                  ...params,
              },
              `维修检验试验_${currentTime}.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>
