<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <project-section-tree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <el-input v-model="listQuery.content" placeholder="请输入内容" class="filter-item" clearable @change="getList" />
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
        </div>
        <div class="content-container">
          <el-table v-loading="listLoading" row-key="id" :data="list" border fit highlight-current-row style="width: 100%;" height="calc(100vh - 276px)">
            <div slot="empty">
              <span v-if="listQuery.projectId">没有数据</span>
              <span v-else>请选择项目或标段</span>
            </div>
            <el-table-column label="序号" type="index" align="center" width="80" />
            <el-table-column label="操作" align="center" width="260" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <el-button type="primary" size="mini" @click="handleView(row)">查看</el-button>
                <el-button v-if="$requirePermission('DEMO.SAM.S') && row.canSubmit" type="success" size="mini" @click="handleSubmit(row)">处置</el-button>
                <el-button v-if="$requirePermission('DEMO.SAM.S') && row.canRevoke" type="info" size="mini" @click="handleRevoke(row)">撤回</el-button>
              </template>
            </el-table-column>
            <el-table-column label="项目名称" prop="projectName" align="center" />
            <el-table-column label="标段名称" prop="sectionName" align="center" />
            <el-table-column label="内容" prop="content" align="center" />
            <el-table-column label="审核状态" prop="auditStatus" align="center" width="100">
              <template slot-scope="{ row }">
                <audit-status-label :audit="row" />
              </template>
            </el-table-column>
            <el-table-column label="问题原因" prop="reason" align="center" />
            <el-table-column label="处置方式" prop="processWay" align="center" />
            <el-table-column label="处置附件" prop="attachments" align="center" width="80">
              <template slot-scope="{ row }">
                <el-button v-if="row.attachments.length == 0" style="color:#9e9e9e;" type="text" size="mini">[无]</el-button>
                <attachment-popover :list="row.attachments" />
              </template>
            </el-table-column>
            <el-table-column label="提交人" prop="submitUserName" align="center" width="80" />
            <el-table-column label="提交时间" prop="submitDate" align="center" width="100">
              <template slot-scope="{ row }">
                {{ row.submitDate|datetime }}
              </template>
            </el-table-column>
            <el-table-column label="审核进度" prop="progresses" align="center" width="100">
              <template slot-scope="{ row }">
                <audit-progress-label :audit="row" @show_progress="handleProgress" />
              </template>
            </el-table-column>
          </el-table>
          <pagination :total="total" :skip-count.sync="listQuery.skipCount" :max-result-count.sync="listQuery.maxResultCount" @pagination="getList" />
        </div>
        <el-dialog title="查看" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
          <el-form ref="dataForm" :model="sample" label-position="top" label-width="120px">
            <el-row :gutter="16">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
                <el-form-item label="标段" prop="sectionId">
                  <el-select v-model="sample.sectionId" readonly class="w100p">
                    <el-option v-for="item in sections" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6">
                <el-form-item label="内容" prop="content">
                  <el-input v-model="sample.content" readonly type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
    <el-dialog :close-on-click-modal="false" title="提交闭合" :visible.sync="processDialogFormVisible">
      <el-form ref="processForm" v-loading="formLoading" :rules="rules" :model="sample" label-width="120px">
        <el-form-item label="问题原因" prop="reason">
          <el-input v-model="sample.reason" type="textarea" />
        </el-form-item>
        <el-form-item label="处理方式" prop="processWay">
          <el-input v-model="sample.processWay" type="textarea" />
        </el-form-item>
        <el-form-item label="附件" prop="attachments">
          <upload-card ref="uploadCard" v-model="sample.attachments" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="processDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitData()">确认</el-button>
      </div>
    </el-dialog>

    <audit-progress-dialog v-model="showAuditProgressDialog" :progress-id="sample.auditProgressId" />
  </el-container>
</template>

<script>
import * as sampleApi from '@/api/sample'
import { lookupConstructions } from '@/api/backbone/section'
import ProjectSectionTree from '@/views/projects/components/ProjectSectionTree'
import { deepClone } from '@/utils'

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import InnerSidebar from '@/components/InnerSidebar'
import UploadCard from '@/components/Upload/UploadCards.vue'
import AttachmentPopover from '@/components/Attachment/AttachmentPopover'
import AuditProgressDialog from '@/views/sys/audit-flow/components/AuditProgressDialog'
import AuditProgressLabel from '@/views/sys/audit-flow/components/AuditProgressLabel'
import AuditStatusLabel from '@/views/sys/audit-flow/components/AuditStatusLabel'

export default {
  name: 'SampleSubmitMng',
  directives: { waves },
  components: { Pagination,
    ProjectSectionTree,
    InnerSidebar,
    UploadCard,
    AttachmentPopover,
    AuditProgressDialog,
    AuditProgressLabel,
    AuditStatusLabel
  },
  data() {
    var validateAttachs = (rule, value, callback) => {
      if (!value || value.length === 0) {
        callback(new Error('请上传附件'))
      } else {
        callback()
      }
    }
    return {
      processDialogFormVisible: false,
      showAuditProgressDialog: false,
      list: [],
      sample: {
        id: undefined,
        projectId: undefined,
        sectionId: undefined,
        content: undefined,
        attachments: []
      },
      total: 0,
      listLoading: false,
      listQuery: {
        projectId: undefined,
        sectionId: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      dialogFormVisible: false,
      sections: [],
      formLoading: false,
      rules: {
        reason: [
          { required: true, message: '请输入原因', trigger: 'blur' },
          { max: 512, message: '长度必须少于512个字符', trigger: 'blur' }
        ],
        processWay: [
          { required: true, message: '请输入处置方式', trigger: 'blur' },
          { max: 512, message: '长度必须少于512个字符', trigger: 'blur' }
        ],
        attachments: [
          {
            required: true,
            message: '请上传张拉附件',
            validator: validateAttachs
          }
        ]
      }
    }
  },
  watch: {
    'sample.projectId': function(val) {
      lookupConstructions(val).then(res => {
        this.sections = res
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleTreeClick(data, node) {
      if (data.isProject) {
        this.listQuery.projectId = data.id
        this.listQuery.sectionId = undefined
      }
      if (data.isSection) {
        this.listQuery.projectId = data.projectId
        this.listQuery.sectionId = data.id
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      sampleApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    handleView(row) {
      this.sample = Object.assign({}, row) // copy obj
      this.dialogFormVisible = true
    },
    handleSubmit(row) {
      this.sample = deepClone(row)
      this.sample.attachments = row.attachments
      this.processDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['processForm'].clearValidate()
        this.$refs['uploadCard'].reload(this.sample.attachments)
      })
    },
    submitData() {
      var data = {
        reason: this.sample.reason,
        processWay: this.sample.processWay,
        attachments: this.sample.attachments,
        concurrencyStamp: this.sample.concurrencyStamp
      }
      this.$refs['processForm'].validate(valid => {
        if (valid) {
          this.formLoading = true
          sampleApi.submit(this.sample.id, data).then(() => {
            this.processDialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleRevoke(row) {
      this.$confirm('是否撤回提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        sampleApi.revoke(row.id, row.concurrencyStamp).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '撤回成功',
            type: 'success',
            duration: 2000
          })
        }).finally(() => {
          this.listLoading = false
        })
      })
    },
    handleProgress(row) {
      this.sample = row
      this.showAuditProgressDialog = true
    }
  }
}
</script>
