<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <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
          maxlength="50"
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          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="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="请选择物资名称"
          style="width: 240px"
          clearable
          filterable
          @change="handleTypeNameChange"
        >
          <el-option
            v-for="item in typeNameOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="typeModelId">
        <el-select
          v-model="queryParams.typeModelId"
          placeholder="请选择规格型号"
          style="width: 240px"
          clearable
          filterable
        >
          <el-option
            v-for="item in typeModelNameOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </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-form-item>
    </el-form>
    <el-table border :data="tableList" v-loading="loading" ref="multipleTable" row-key="qrId">
      <el-table-column label="序号" align="center" type="index" width="70">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修时间" align="center" prop="repairTime" show-overflow-tooltip/>
      <el-table-column label="维修单号" align="center" prop="repairCode" show-overflow-tooltip/>
      <el-table-column label="退料单号" align="center" prop="backCode" show-overflow-tooltip/>
      <el-table-column label="退料单位" align="center" prop="backUnit" show-overflow-tooltip/>
      <el-table-column label="退料工程" align="center" prop="backPro" show-overflow-tooltip/>
      <el-table-column label="机具名称" align="center" prop="type" show-overflow-tooltip/>
      <el-table-column label="规格型号" align="center" prop="typeName" show-overflow-tooltip/>
      <el-table-column label="单位" align="center" prop="unit" width="80" show-overflow-tooltip/>
      <el-table-column
        label="任务状态"
        align="center"
        prop="repairStatus"
        width="100"
        show-overflow-tooltip
      >
        <template v-slot="scope">
    <span :style="{ color: scope.row.repairStatus === '维修完成' ? 'green' : 'orange' }">
      {{ scope.row.repairStatus }}
    </span>
        </template>
      </el-table-column>
      <el-table-column label="报修数量" align="center" prop="repairNum" width="80" show-overflow-tooltip/>
      <el-table-column label="已修数量" align="center" prop="repairedNum" width="80" show-overflow-tooltip/>
      <el-table-column label="报废数量" align="center" prop="scrapNum" width="80" show-overflow-tooltip/>
    </el-table>
    <pagination :total="total"
                v-show="total > 0"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getBackQueryList, getProjectList, getUnitList} from "@/api/back";
import {getDeviceType} from "@/api/ma/device";
import {getRepairQueryList} from "@/api/repair/repair";
import dict from "@/utils/dict";

export default {
  name: 'Code',
  dicts: ['qr_box_type'],
  components: {Treeselect},
  data() {
    return {
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0, //                总条数
      loading: false, //           遮罩层
      showSearch: true, //        显示搜索条件
      tableList: [], //            列表数据源
      queryTime: [], //           日期数据源
      selectList: [], //          列表选中数据
      // 列表查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bindStatus: '',
        endTime: '',
        keyWord: '',
        startTime: '',
        typeId:undefined,
        typeModelId: undefined
      },
      unitList: [],
      proList: [],
      typeNameOptions:[],
      typeModelNameOptions:[]
    }
  },
  created() {
    const end = new Date()
    let start = new Date()
    start.setMonth(start.getMonth() - 1)
    this.queryParams.time = [this.format(start), this.format(end)]
    this.getList()
    this.GetUnitData()
    this.GetProData()
    this.getDeviceType()
  },
  methods: {
    dict,
    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}`
    },
    unitChange(val){
      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
    },
    // 获取 来往单位 列表数据
    async GetUnitData() {
      const params = {
        // projectId: this.queryParams.proId /*  */,
      }
      const res = await getUnitList(params)
      this.unitList = res.data
    },
    proChange(val){
      setTimeout(()=>{
        this.GetUnitData()
      },500)
    },
    async handleTypeNameChange(e){
      this.queryParams.typeModelName=null
      this.typeModelNameOptions=[]
      getDeviceType({ level: 4, typeId:e }).then(response => {
        let matNameRes = response.data
        this.typeModelNameOptions = matNameRes.map((item) => {
          return {
            label: item.typeName,
            value: item.typeId
          }
        })
      })
    },
    getDeviceType() {
      getDeviceType({ level: 3 }).then(response => {
        let matNameRes = response.data
        this.typeNameOptions = matNameRes.map((item) => {
          return {
            label: item.typeName,
            value: item.typeId
          }
        })
      })
      getDeviceType({ level: 4 }).then(response => {
        let matModelRes = response.data
        this.typeModelNameOptions = matModelRes.map((item) => {
          return {
            label: item.typeName,
            value: item.typeId
          }
        })
      })
    },
    // 获取列表
    async getList() {
      this.loading = true
      // if (this.queryParams.time && this.queryParams.time.length > 0) {
      //   this.queryParams.startTime = this.queryParams.time[0]
      //   this.queryParams.endTime = this.queryParams.time[1]
      // }else{
      //   this.queryParams.startTime=undefined
      //   this.queryParams.endTime=undefined
      // }
      const res = await getRepairQueryList(this.queryParams)
      this.tableList = res.data.rows
      this.total = res.data.total
      this.loading = false
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.$refs.multipleTable.clearSelection();
      this.queryTime = []
      this.resetForm('queryForm')
      this.queryParams.pageNum = 1
      this.queryParams.endTime = ''
      this.queryParams.pageSize = 10
      this.queryParams.startTime = ''
      this.getList()
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
  },
}
</script>
<style lang="scss" scoped>
.uploadImg {
  padding-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.boxCode {
  margin-top: 10px;
  padding-bottom: 20px;
  font-size: 33px;
  font-weight: bold;
  color: #000;
}

::v-deep.el-table .fixed-width .el-button--mini {
  width: 60px !important;
  margin-bottom: 10px;
}
</style>
