<template>
  <el-dialog :visible.sync="visible" :title="'处理'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="验收内容" style="overflow:hidden;">
        <template>
          <div style="margin: 15px 0;"></div>
          <el-checkbox-group v-model="checkedItems" @change="handleCheckedCitiesChange">
            <span style="display:inline-grid">
              <el-checkbox v-for="item in acceptanceArray" :label="item.checkLabel" :key="item.checkCode">{{item.checkLabel}}</el-checkbox>
            </span>
          </el-checkbox-group>
        </template>
      </el-form-item>
      <el-form-item label="审批" prop="checkResult" label-width="80px" >
        <el-radio-group v-model="dataForm.checkResult">
          <el-radio  :label="3">通过</el-radio>
          <el-radio  :label="4">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="验收理由" prop="checkOpinion">
        <el-input
                type="textarea"
                :rows="3"
                v-model="dataForm.checkOpinion"
                maxlength="200"
                style="width:calc(100% - 110px)"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :disabled="isAble" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        infoId: '',
        checkResult: '',
        checkOpinion: ''
      },
      acceptanceArray:[],
      checkedItems:[],
      isAble: false,
      checkedCount:'',
      allCount:''
    }
  },
  computed: {
    dataRule () {
      return {
        checkResult: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        checkOpinion: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.$route.query.id !== '' && this.$route.query.id != null) {
      this.dataForm.infoId = this.$route.query.infoId
      this.getInfo()
    }
  },
  methods: {
    init () {
      this.visible = true
      this.isAble = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.infoId) {
          this.getInfo()
        }
      })
    },
    // 获取验收内容信息
    getInfo () {
      this.$http.get(`/kpi/subcheckdictionary/getSubCheckDictList`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.acceptanceArray = res.data
      }).catch(() => {})
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {

    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        //是否全选判断
        this.allLength = this.acceptanceArray.length;
        if(this.dataForm.checkResult===3 && this.checkedItems.length != this.allLength){
          this.$message('通过请全选后提交！');
          return false
        }
        this.isAble = true
        this.$http['post']('/kpi/subpositioncheckinfo/acceptance', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            this.isAble = false
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.$emit('connectResponse')
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss">
    .el-checkbox{
        margin-left:0px ! important;
    }
</style>
