<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <KLPTable v-loading="loading" :data="checkTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="taskId" v-if="true"/>
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </KLPTable>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 检查任务详情弹窗（可复用task页面的实现） -->
    <!-- ...如需详情弹窗可参考task/index.vue... -->
    <el-dialog :title="'检查任务详情'" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="任务名称">{{ detailData.taskName }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ detailData.remark }}</el-descriptions-item>
        <el-descriptions-item label="检查项">
          <div v-for="item in detailData.itemList" :key="item.itemId" style="margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between;">
            <div style="display: flex; align-items: center;">
              <el-tag type="info" style="margin-right: 12px;">{{ item.itemName }}</el-tag>
              <el-tag
                :type="item.status == 1 ? 'success' : (item.status == 2 ? 'danger' : 'info')"
                style="margin-right: 16px; min-width: 60px; text-align: center;"
              >
                <span v-if="item.status == 0">未检测</span>
                <span v-else-if="item.status == 1">通过</span>
                <span v-else-if="item.status == 2">不通过</span>
                <span v-else>未知</span>
              </el-tag>
            </div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDetailClose">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listHistoryCheckTask, getCheckTask } from '@/api/mes/qc/checkTask'

export default {
  name: 'CheckTaskHistory',
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      checkTaskList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        taskName: undefined,
      },
      detailOpen: false,
      detailData: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listHistoryCheckTask(this.queryParams).then(response => {
        this.checkTaskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleExport() {
      this.download('qc/checkTask/export', {
        ...this.queryParams
      }, `checkTask_${new Date().getTime()}.xlsx`)
    },
    handleDetail(row) {
      this.loading = true
      getCheckTask(row.taskId).then(response => {
        this.detailData = response.data
        this.detailOpen = true
      }).finally(() => {
        this.loading = false
      })
    },
    handleDetailClose() {
      this.detailOpen = false
      this.getList()
    }
  }
}
</script>
