<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="impUnitName">
            <el-select v-model="queryParams.impUnitName" placeholder="请选择分公司" clearable style="width: 240px">
                <el-option
                    v-for="item in impUnitNameList"
                    :key="item.impUnitName"
                    :label="item.impUnitName"
                    :value="item.impUnitName"
                ></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 :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="repairTime" width="180px":show-overflow-tooltip="true"/>
          <el-table-column label="维修单号" align="center" prop="repairCode"  :show-overflow-tooltip="true"/>
          <el-table-column label="分公司" align="center" prop="impUnitName" width="150px":show-overflow-tooltip="true"/>
          <el-table-column label="退料单位" align="center" prop="backUnitName" width="180px" :show-overflow-tooltip="true"/>
          <el-table-column label="退料工程" align="center" prop="backProjectName" width="220px"/>
          <el-table-column label="机具类型" align="center" prop="maTypeName" :show-overflow-tooltip="true"/>
          <el-table-column label="机具规格" align="center" prop="maTypeModel" :show-overflow-tooltip="true"/>
          <el-table-column label="机具单位" align="center" prop="unitName" width="100px" :show-overflow-tooltip="true"/>
          <el-table-column label="在修数量" align="center" prop="repairNum" :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 {
  getRepairProjectListApi,
} from '@/api/repair/repair';
import {
    getImpUnitNameListApi,
} from '@/api/back/index.js'

export default {
    name: 'ProjectRepairing',
    dicts: [],
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 弹出层标题
            title: '查看',
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            total: 0,
            // 主表格数据
            tableList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                time: null,
                keyWord: null,
                startTime: null,
                endTime: null,
                impUnitName: null,
            },
            impUnitNameList: [],
        }
    },
    computed: {
        groupPrintData() {
            const result = [];
            for (let i = 0; i < this.printTableData.length; i += 2) {
                const group = this.printTableData.slice(i, i + 2);
                // 若分组不足 2 个元素，补充空对象
                while (group.length < 2) {
                    group.push({ partName: '', status: '' });
                }
                result.push(group);
            }
            return result;
        },
    },
    created() {
        const end = new Date()
        let start = new Date()
        start.setMonth(start.getMonth() - 1)
        this.queryParams.startTime = this.format(start)
        this.queryParams.endTime = this.format(end)
        this.getImpUnitNameList()
        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}`
      },

        // 定义格式化日期的方法
      formatDate(dateStr) {
        if (!dateStr) return '';
        const date = new Date(dateStr);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      },

       // 分公司下拉
      async getImpUnitNameList () {
          try {
              const res = await getImpUnitNameListApi()
              this.impUnitNameList = res.data
          } catch (error) {
              console.log('🚀 ~ error:', error)
          }
      }, 

      // 获取列表
      async getList() {
        try {
            this.loading = true
            const params = {
                  keyWord: this.queryParams.keyWord,
                  impUnitName: this.queryParams.impUnitName,
                  startTime: this.queryParams.startTime,
                  endTime: this.queryParams.endTime,
                  pageSize: this.queryParams.pageSize,
                  pageNum: this.queryParams.pageNum
              }
            const res = await getRepairProjectListApi(params)
            this.loading = false
            if(res.data.rows.length>0){
              this.tableList = res.data.rows;
            }else{
              this.tableList=[]
            }
            this.total = res.data.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.startTime = this.format(start)
        this.queryParams.endTime = this.format(end)
        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 = {
            keyWord: this.queryParams.keyWord,
            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/exportRepairProjectList',
              {
                  ...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>
