<template>
  <div>
    <!-- 抽样文件意见批量反馈 -->
    <dialog-edit ref="dialogEdit" :visible.sync="dialogVisible" :form="forms" :autoSave="autoSave"
      :editModel="editModel" :appendToBody="appendToBody" :title="headerTitle"
      @onSaved="handleSubmit" :formRules="rules" :footerBtnShow="false" :saveLabel="$t('feedBackMain.temprorarySave')">
      <div v-if="dialogVisible">
        <el-row>
        <!-- 文件类型 -->
        <el-col :span="8" v-if="feedbackType !== '1'">
          <el-form-item :label="$t('feedBackMain.auditFileType')" prop="auditFileType" label-width="110px">
            <file-select-list v-model="forms.auditFileType" :fileQueryType="feedbackType" stampWay="3"></file-select-list>
          </el-form-item>
        </el-col>
        <!-- 反馈批次 -->
        <el-col :span="8">
          <el-form-item :label="$t('feedBackMain.feedbackFrequency')" label-width="110px" prop="freq">
            <feed-back-freq-select-list v-model="forms.freq" :queryParas="newForm"></feed-back-freq-select-list>
          </el-form-item>
        </el-col>
        </el-row>
        <!-- 反馈列表 -->
        <feedBackContentSub ref="feedback" :queryParas="newForm" :feedbackType="feedbackType" :datas.sync="datas" :isBatch="true"></feedBackContentSub>
      </div>
      <div slot="footer" style="text-align: center;">
        <!-- 暂存 -->
        <auth-button size="medium" type="primary" :label="$t('feedBackMain.temprorarySave')" :loading="loading" @click="submit=0,handleSubmit();"></auth-button>
        <!-- 提交 -->
        <auth-button size="medium" type="primary" :label="$t('common.submit')" :loading="loading" @click="submit=1,$refs.dialogEdit.save()" :openPerms="false"></auth-button>
      </div>
    </dialog-edit>
  </div>
</template>

<script>
import BaseEdit from '@/pages/baseEditPage'
import feedBackFreqSelectList from '@/components/element/assetoutfeedback/feedBackFreqSelectList'
import fileSelectList from '@/components/element/assetoutfeedback/fileSelectList'

import feedBackContentSub from '@/pages/assetoutfeedback/feedBackMain/feedBackContentSub'
import {
  DialogEdit,
  DataTable,
  authButton
} from 'newepoch-components'
import feedBackMain from '@/api/assetoutfeedback/feedBackMain/feedBackMain.js'
export default {
  extends: new BaseEdit(),
  name: 'sampleFeedBackBatchPop',
  components: {
    DialogEdit,
    DataTable,
    fileSelectList,
    authButton,
    feedBackFreqSelectList,
    feedBackContentSub
  },
  props: {
    autoSave: {
      type: [Boolean],
      default: false
    },
    // 弹窗标题
    headerTitle: {
      type: [String],
      default: ''
    },
    // 反馈文件类型
    feedbackType: {
      type: [String],
      default: '1'
    }
  },
  computed: {
    newForm () {
      return {
        projectId: this.form.projectId,
        periods: this.form.periods,
        auditFileType: this.forms.auditFileType,
        feedbackFrequency: this.forms.freq
      }
    }
  },
  data () {
    return {
      loading: false, // 加载中
      options: [],
      submit: 0, // 请求类型
      appendToBody: true,
      menuFlag: false,
      datas: [],
      // 校验规则
      rules: {
        freq: [
          {
            required: true,
            message: this.$t('feedBackMain.freqPrompt'),
            trigger: 'change'
          },
          {
            required: true,
            message: this.$t('feedBackMain.freqPrompt'),
            trigger: 'blur'
          }
        ]
      },
      forms: {
        freq: '',
        auditFileType: ''
      }
    }
  },
  methods: {
    // 暂存/提交
    handleSubmit () {
      // 请求地址
      let url = ['tpsave', 'spbatchSave'][this.submit]
      // 成功提示
      let success = [this.$t('feedBackMain.saveSuccess'), this.$t('feedBackMain.submitSuccess')][this.submit]
      // 错误提示
      let error = [this.$t('feedBackMain.saveError'), this.$t('feedBackMain.submitError')][this.submit]
      // 筛选操作的资产
      let datas = this.UtilJs.cloneDeep(this.datas)
      // .filter(item => {
      //   return item.isShow === true
      // })
      // .map(item => {
      //   item.sampleFeedBackDetailDtos = item.sampleFeedBackDetailDtos.map((e, index) => {
      //     e.feedbackTimes = '1'
      //     e.qusetionNumber = (index + 1).toString()
      //     e.feedbackAssetKey = item.keyId
      //     e.feedbackQustionNo = item.keyId + (index + 1).toString()
      //     return e
      //   })
      //   return item
      // })
      if (datas.length < 1) {
        this.warning(this.$t('feedBackMain.noData'))
        return
      }
      // 配置请求参数
      let args = {
        'auditFileType': this.forms.auditFileType,
        // 'flag': '2',
        'feedbackFrequency': this.forms.freq.toString(),
        'periods': this.form.periods,
        'projectId': this.form.projectId,
        'sampleFeedBackListDtos': datas
      }
      // 校验资产编号
      for (let index in this.$refs.feedback.$refs.form) {
        let item = this.$refs.feedback.$refs.form[index]
        item.validate((valid) => {
          if (!valid) {
            return valid
          }
        })
      }
      // 若有不通过资产，校验意见数量是否存在
      for (let index in this.datas) {
        let item = this.datas[index]
        datas[index].sampleFeedBackDetailDtos = []
        if (item.passFlag === '1') {
          let num = 0
          // 添加意见不为空表单
          for (let index2 in item.sampleFeedBackDetailDtos) {
            let item2 = item.sampleFeedBackDetailDtos[index2]
            if (item2.feedbackQuestionContent) {
              num++
              datas[index].sampleFeedBackDetailDtos.push({
                feedbackAssetKey: item.keyId,
                feedbackQuestionContent: item2.feedbackQuestionContent,
                feedbackTimes: '1',
                // questionStatus: '1',
                feedbackQustionNo: item.keyId + num.toString(),
                qusetionNumber: num.toString()
              })
            }
          }
          // for (let index2 in item.sampleFeedBackDetailDtos) {
          //   let item2 = item.sampleFeedBackDetailDtos[index2]
          //   if (!item2.feedbackQuestionContent) {
          //     num++
          //     item.sampleFeedBackDetailDtos.splice(index2, 1)
          //     index2--
          //   }
          // }
          if (num < 0) {
            this.warning(this.$t('feedBackMain.feedbackPrompt'))
            return false
          }
        }
      }
      this.assetpkgmgrFullScreenLoading()
      feedBackMain[url].create(args).then(res => {
        this.assetpkgmgrFullScreenLoading().close()
        if (res.success && res.data) {
          this.success(success)
          this.$parent.handleFilter()
          this.dialogVisible = false
        } else {
          this.error(error)
        }
      }).catch(res => {
        this.assetpkgmgrFullScreenLoading().close()
      })
    }
  },
  watch: {
    visible (val) {
      if (val) {
        let $this = this
        // $this.$nextTick(() => {
        $this.datas = []
        $this.forms.freq = 1
        $this.forms.auditFileType = this.form.auditFileType
        // })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  @import '~@/styles/assetoutfeedback/common.scss'
</style>
