<template>
  <!--快速组卷-->
  <el-dialog class="tip-dialog" title="快速组卷" :close-on-click-modal="false" :modal-append-to-body="false" append-to-body :visible.sync="quickVisible">
    <div class="setting-dialog">
      <div class="fourTest-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="选择年月" prop="date">
            <el-date-picker
              v-model="form.date"
              type="month"
              placeholder="选择年月"
              value-format="yyyy-MM"
              style="width: 360px;"
              @change="changeDate"
            />
          </el-form-item>
          <div v-if="form.date !== ''" class="tip-result">总共为您找到<span>{{ searchNumber }}</span>条数据！</div>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="quickVisible = false">取消</el-button>
        <el-button type="primary" @click.native="handleComfireQuick">确定</el-button>
      </div>
    </div>
  </el-dialog>

</template>

<script>
import { FetchPreFastToArchives } from '@/api/collect/collect'
export default {
  name: 'QuickPaper',
  components: { },
  inject: ['parentsData'],
  props: {
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    },
    arcId: {
      type: String,
      default: function() {
        return ''
      }
    },
    collectLevel: {
      type: Number,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      quickVisible: false,

      form: {
        date: ''
      },
      searchNumber: 0,
      archivesIds: [],
      rules: {
        date: [
          { required: true, message: '请选择年月', trigger: 'change' }
        ]
      }
    }
  },
  created() {

  },
  mounted() {
  },
  methods: {
    handleComfireQuick() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.searchNumber) {
            this.$message({
              message: '当前年月没有数据！',
              type: 'warning'
            })
            return false
          }
          this.quickVisible = false
          this.form.date = ''
          this.searchNumber = 0
          this.$nextTick(() => {
            this.$parent.handleForm('add')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changeDate(val) {
      if (!val) {
        this.$nextTick(() => {
          this.form.date = ''
        })
      } else {
        let parentsId
        if (this.selectedCategory.arrangeType === 3) {
          parentsId = this.parentsData.parentsProjectId
        } else {
          parentsId = null
        }
        const params = {
          'categoryId': this.selectedCategory.id,
          'date': val,
          'parentsId': parentsId
        }
        FetchPreFastToArchives(params).then(res => {
          if (res.length === 0) {
            this.searchNumber = 0
            this.archivesIds = []
            this.$parent.quickPaper = false
            this.$parent.quickPaperArcId = []
          } else {
            this.searchNumber = res.length
            this.archivesIds = res
            this.$parent.quickPaper = true
            this.$parent.quickPaperArcId = res
          }
        })
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.tip-result{
  margin-left: 12px;
  font-size: 12px;
  color: #0C0E1E;
  span{
    padding: 0 6px;
    font-weight: bold;
    color: #ED4A41;
  }
}
</style>
