<template>
  <div class="app-container">
    <h2>
      生产部门日常得分 - {{ $route.query.type === '1' ? '查看' : '评分'  }}

      <el-button
        type="primary"
        style="float: right; margin-bottom: 10px"
        @click="$router.back()"
        >返 回</el-button
      >
    </h2>

    <el-table :data="tableData" border style="max-height: 60vh; overflow: auto" :header-cell-style="{background:'#1352b6'}">
      <el-table-column
        align="center"
        :label="`${new Date().getFullYear()}年各运行部、化验中心日常得分`"
      >
        <el-table-column align="center" label="专业" width="100">
          <template #default="{ row }">
            {{ initType(row.dutyType) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="权重" width="100">
          <template #default="{ row }">
            <span v-if="row.scoreWeight">
              {{ row.scoreWeight }}% 
            </span>
            <span v-else>
              <!-- - -->
            </span>
            </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="item.byDeptName"
          v-for="(item, index) in tableData[0].children"
          :key="index"
        >
          <template slot-scope="scope">
            <span v-if="$route.query.type === `1`">
              {{ scope.row.children[index].byDeptScore }}
            </span>
            <el-input
              v-else
              :disabled="scope.row.children[index].id ===666"
              v-model="scope.row.children[index].byDeptScore"
              @input="inputVal(scope.row.children[index].byDeptScore,index)"
            ></el-input>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

    <div
      style="text-align: right; margin-top: 20px"
      v-if="$route.query.type !== `1`"
    >
      <el-button type="primary" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import {
  postAssessmentRatingOtherDetail,
  productionDepartmentSub
} from '@/api/allApi/index.js'

export default {
  name: 'PostCheckWebProduceScore',

  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      loading: false,
      tableData: [
        {
          children:[]
        }
      ]
    }
  },

  mounted() {
    this.initList()
  },

  methods: {
    async initList() {
      this.loading = true
      const res = await postAssessmentRatingOtherDetail(this.$route.query.id)
      if (res.code !== 200) return this.$message.error(res.msg)
      this.tableData = res.data.children
      let arr = []
      this.tableData.forEach((item, index) => {
        if (index === 0) {
          item.children.forEach((it, ind) => {
            arr.push({
              byDeptName: it.byDeptName,
              byDeptId: it.byDeptId,
              id: 666,
              byDeptScore: it.byDeptScore
            })
          })
        } else {
          item.children.forEach((it, ind) => {
            arr.forEach((val, i) => {
              if (it.byDeptId === val.byDeptId) {
                val.byDeptScore = val.byDeptScore += +it.byDeptScore
              }
            })
          })
        }
      })

      this.tableData.push({
        dutyType: 999,
        scoreWeight: '',
        rule: '',
        children: arr
      })
      this.total = res.total
      this.loading = false
    },
    initType(val) {
      switch (val) {
        case 1:
          return 'HSE'
        case 2:
          return '生产'
        case 3:
          return '设备'
      }
    },
    inputVal(val,index){
      this.tableData[this.tableData.length-1].children[index].byDeptScore = 0
      let num = 0
      this.tableData.forEach((item,idx)=>{
        num+=  +item.children[index].byDeptScore
      })
      this.tableData[this.tableData.length-1].children[index].byDeptScore = num
      this.$forceUpdate()
    },
    async submit() {
      console.log(this.tableData,888);
      let arr  = this.tableData.splice(this.tableData.length - 1, 1)
      const res  = await productionDepartmentSub(this.tableData)
      if (res.code !== 200) return this.$message.error(res.msg)
      this.$message.success('提交成功!')
      this.$router.push(`/examineCompetition/produce`)
    }
  }
}
</script>

<style lang="scss" scoped></style>
