<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">
      <div v-if="dialogVisible">
        <el-row>
          <el-col :span="8" v-if="feedbackType !== '1'">
            <el-form-item :label="$t('feedBackMain.feedbackType')" prop="auditFileType" label-width="110px">
              <file-select-list disabled 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>
        <!-- 反馈批次
        <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> -->
        <!-- 反馈列表 -->
        <feedBackContentSub ref="feedback" :queryParas="currentRow" :feedbackType="feedbackType" :datas.sync="datas"></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 feedBackContentSub from '@/pages/assetoutfeedback/feedBackMain/feedBackContentSub'
import {
  DialogEdit,
  DataTable,
  authButton
} from 'newepoch-components'
import feedBackMain from '@/api/assetoutfeedback/feedBackMain/feedBackMain.js'
import fileSelectList from '@/components/element/assetoutfeedback/fileSelectList'

export default {
  extends: new BaseEdit(),
  name: 'sampleFeedBackSinglePop',
  components: {
    DialogEdit,
    DataTable,
    authButton,
    fileSelectList,
    feedBackFreqSelectList,
    feedBackContentSub
  },
  props: {
    autoSave: {
      type: [Boolean],
      default: false
    },
    // 弹窗标题
    headerTitle: {
      type: [String],
      default: ''
    },
    // 反馈文件类型
    feedbackType: {
      type: [String],
      default: '1'
    },
    // 传进表单参数
    currentRow: {
      type: [Object],
      default () {
        return null
      }
    }
  },
  computed: {
    newForm () {
      return {
        projectId: this.currentRow.projectId,
        periods: this.currentRow.periods,
        auditFileType: this.currentRow.auditFileType
      }
    }
  },
  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'
          }
        ],
        auditFileType: [
          {
            required: true,
            message: this.$t('feedBackMain.fileTypePrompt'),
            trigger: 'change'
          },
          {
            required: true,
            message: this.$t('feedBackMain.fileTypePrompt'),
            trigger: 'blur'
          }
        ]
      },
      forms: {
        freq: ''
      }
    }
  },
  methods: {
    // 暂存/提交
    handleSubmit () {
      let url = ['fileTpsave', 'fileSave'][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.datas.map(item => {
        return item
      })[0]
      if (this.submit === 1 && !this.forms.freq) {
        this.warning(this.$t('feedBackMain.freqPrompt'))
        return
      }
      // console.log(datas)
      let args = {
        'auditFileType': this.forms.auditFileType,
        'feedbackFrequency': this.forms.freq.toString(),
        'periods': datas.periods,
        'projectId': this.currentRow.projectId,
        'projectName': this.currentRow.projectName,
        'keyId': datas.keyId,
        'assetInfoId': this.currentRow.assetInfoId,
        'passFlag': datas.passFlag,
        'modifyTime': datas.modifyTime,
        'assetCode': datas.assetCode,
        'enterPoolStatus': datas.enterPoolStatus,
        'qstDetails': []
      }
      if (datas.supplementPumpStatus) {
        args.supplementPumpStatus = datas.supplementPumpStatus
      }
      // 若为不通过
      if (datas.passFlag === '1') {
        let num = 0
        // 添加意见不为空表单
        for (let index2 in datas.sampleFeedBackDetailDtos) {
          let item2 = datas.sampleFeedBackDetailDtos[index2]
          if (item2.feedbackQuestionContent) {
            num++
            args.qstDetails.push({
              feedbackAssetKey: datas.keyId,
              feedbackQuestionContent: item2.feedbackQuestionContent,
              feedbackTimes: '1',
              questionStatus: '1',
              feedbackQustionNo: datas.keyId + num.toString(),
              qusetionNumber: num.toString()
            })
          }
        }
        // 若全部为空
        if (num < 1) {
          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(() => {
          let args = {
            feedbackAssetKey: this.currentRow.keyId
          }
          let assetInfo = {
            keyId: this.currentRow.keyId,
            passFlag: '1',
            periods: this.currentRow.periods,
            assetCode: this.currentRow.assetCode,
            supplementPumpStatus: null,
            enterPoolStatus: '1',
            modifyTime: this.currentRow.modifyTime,
            isShow: true,
            sampleFeedBackDetailDtos: [{
              feedbackQuestionContent: ''
            },
            {
              feedbackQuestionContent: ''
            },
            {
              feedbackQuestionContent: ''
            }]
          }
          this.forms.freq = Number(this.currentRow.feedbackFrequency)
          this.$set(this.forms, 'auditFileType', this.currentRow.auditFileType)
          this.datas = [assetInfo]
          // console.log(this.datas)
          feedBackMain.assetInfo.getDefault(args).then(res => {
            if (res.success && res.data) {
              if (res.data.qstDetails && res.data.qstDetails.length > 0) {
                this.datas[0].sampleFeedBackDetailDtos = res.data.qstDetails
              }
              if (res.data.passFlag) {
                this.datas[0].passFlag = res.data.passFlag.toString()
              }
              if (res.data.enterPoolStatus) {
                this.datas[0].enterPoolStatus = res.data.enterPoolStatus
              }
              if (res.data.supplementPumpStatus) {
                this.datas[0].supplementPumpStatus = res.data.supplementPumpStatus
              }
            }
          })
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  @import '~@/styles/assetoutfeedback/common.scss'
</style>
