<!--预分配-->
<template>
  <div>
    <dialog-edit
      :visible.sync="dialogVisible"
      :autoSave="false"
      :autoClose="false"
      :editModel="editModel"
      :form="{}"
      @onSaved="onSaved"
      width="50%"
      class="preAssignPop"
      center
      :title="$t('coordManageMain.preAssign')">
    <div class="form-div">
      <div class="add-btn">
        <auth-button type="primary" :label="$t('coordManageMain.addNew')" class="iconfont iconxinzeng" size="medium" @click="addPreRules"></auth-button>
      </div>
      <el-card shadow="never" class="card-div card-header-color" v-for="(item,index) in submitForm" :key="item.form.keyId">
        <div slot="header" class="clearfix" v-if="submitForm.length>1">
          <span class="iconfont iconshanchu11 show-cursor close-icon" @click="closeCard(index)"></span>
        </div>
        <div>
          <el-form :ref="'forms'" :model="item.form" label-width="80px">
              <el-form-item :label="$t('coordManageMain.fileType')" :prop="'fileType'"
                :rules="{ type: 'array', required: true, message: $t('preAssignPop.fileTypeRule'), trigger: 'blur' }">
                <file-select-list v-model="item.form.fileType" width="100%" :isAll="true" :multiple="true"></file-select-list>
              </el-form-item>
              <el-form-item :label="$t('coordManageMain.fkpc')" :prop="'feedbackFrequency'"
                :rules="{ type: 'array', required: true, message: $t('preAssignPop.fkpcRule'), trigger: 'blur' }">
                <feed-back-freq-select-list v-model="item.form.feedbackFrequency"
                                          width="100%"
                                          :multiple="true"
                                          :auditFileType="item.form.fileType"
                                          :collapsetags="true"
                                          :queryParas="form"></feed-back-freq-select-list>
              </el-form-item>
              <el-form-item :label="$t('coordManageMain.preUser')" :prop="'preUserId'"
                :rules="{ type: 'array', required: true, message: $t('preAssignPop.preUserRule'), trigger: 'blur' }">
                <trace-user-select-list v-model="item.form.preUserId"
                                        :queryParas="form"
                                        :multiple="true"
                                        :collapsetags="true"
                                        :multiplelimit="10"
                                        width="100%"></trace-user-select-list>
              </el-form-item>
              <el-form-item :label="$t('coordManageMain.preRule')" :prop="'preAssignRule'"
                :rules="[{ required: true, message: $t('preAssignPop.preRule'), trigger: 'blur' }]">
                <!-- <pre-assign-rule-pop v-model="item.form.preAssignRule" :key="index" :queryParas="form" width="100%"></pre-assign-rule-pop> -->
                <select-dictionary dictTypeCode="AUDIT_OUT_ASSIGN_RULES" v-model="item.form.preAssignRule" :key="index"></select-dictionary>
              </el-form-item>
              <el-form-item :label="$t('coordManageMain.isStop')" :prop="'enableFlag'">
                <el-checkbox v-model="item.form.enableFlag" true-label="1" false-label="2"></el-checkbox>
              </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </dialog-edit>
  </div>
</template>
<script>
import { DialogEdit, authButton, SelectDictionary } from 'newepoch-components'
import BasePage from '@/pages/baseEditPage'
import fileSelectList from '@/components/element/assetoutfeedback/fileSelectList'
import preAssignRulePop from '@/pages/assetoutfeedback/coordManage/preAssignRulePop'
import coordManageApi from '@/api/assetoutfeedback/coordManage/coordManageApi'
import feedBackFreqSelectList from '@/components/element/assetoutfeedback/feedBackFreqSelectList'
import traceUserSelectList from '@/components/element/assetoutfeedback/traceUserSelectList'
export default {
  name: 'preAssignPop',
  extends: new BasePage(),
  components: {
    DialogEdit,
    authButton,
    fileSelectList,
    preAssignRulePop,
    feedBackFreqSelectList,
    traceUserSelectList,
    SelectDictionary
  },
  props: {
    editModel: {
      type: [String],
      default () {
        return 'E'
      }
    },
    form: {
      type: [Object],
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      subForm: {
        form: {
          fileType: [],
          feedbackFrequency: [],
          prePersonnel: [],
          preRule: '',
          ennable: false
        },
        formVisible: true // 表单显示
      },
      newArr: [],
      submitForm: [],
      rules: {
        fileType: [{ type: 'array', required: true, message: '文件类型不能为空', trigger: 'blur' }],
        feedbackFrequency: [{ type: 'array', required: true, message: '反馈批次不能为空', trigger: 'blur' }],
        preUserId: [{ required: true, message: '预设人员不能为空', trigger: 'change' }],
        preAssignRule: [{ required: true, message: '预设规则不能为空', trigger: 'change' }]
      }

    }
  },
  mounted () {
    // this.submitForm.push(this.UtilJs.cloneDeep(this.subForm))
    this.loadRules()
  },
  methods: {
    // 加载已有规则
    loadRules () {
      if (JSON.stringify(this.form) !== '{}') {
        this.taskAssignRuleApi.getList(this.form).then(res => {
          if (res.success) {
            if (res.data.length > 0) {
              this.submitForm = this.loadForm(res.data)
            } else {
              this.addPreRules()
            }
          }
        })
      }
    },
    loadForm (data) {
      let formDatas = []
      data.forEach((item, index) => {
        let frequency = []
        item.feedbackFrequency.forEach(it => {
          frequency.push(Number(it))
        })
        let form = {
          form: {
            keyId: item.keyId,
            modifyTime: item.modifyTime,
            fileType: item.fileType,
            feedbackFrequency: frequency,
            preUserId: item.preUserId,
            preAssignRule: item.preAssignRule,
            enableFlag: item.enableFlag
          },
          formVisible: true // 表单显示
        }
        formDatas.push(form)
      })
      return formDatas
    },
    // 添加规则
    addPreRules () {
      this.submitForm.push(this.UtilJs.cloneDeep(this.subForm))
    },
    // 验证保存
    onSaved () {
      let formDatas = []
      let flag = []
      this.submitForm.forEach((item, index) => {
        this.$refs.forms[index].validate((valid) => {
          if (valid) {
            let formData = {
              fileType: item.form.fileType,
              feedbackFrequency: item.form.feedbackFrequency,
              preUserId: item.form.preUserId,
              preAssignRule: item.form.preAssignRule,
              enableFlag: item.form.enableFlag,
              keyId: item.form.keyId,
              modifyTime: item.form.modifyTime
            }
            formDatas.push(formData)
          } else {
            flag.push(0)
          }
        })
      })
      if (flag.length === 0) {
        let submitDatas = {
          periods: this.form.periods,
          projectId: this.form.projectId,
          preAssignRuleDTOS: formDatas
        }
        this.assetpkgmgrFullScreenLoading()
        this.taskAssignRuleInfoApi.defaultSave(submitDatas).then(res => {
          this.assetpkgmgrFullScreenLoading().close()
          if (res.success) {
            this.success('预设规则成功')
            this.dialogVisible = false
          } else {
            this.error('预设规则失败')
          }
        }).catch(res => this.assetpkgmgrFullScreenLoading().close())
      } else {
        this.error('请正确填写表格')
      }
    },
    // 删除表单
    closeCard (item) {
      this.submitForm.splice(item, 1)
      // let newData = this.submitForm.filter(it => {
      //   if (it !== item) {
      //     return it
      //   }
      // })
      // this.submitForm = this.UtilJs.cloneDeep(newData)
      // item.formVisible = false
    }
  },
  computed: {
    taskAssignRuleApi () {
      return coordManageApi.taskAssignRule
    },
    taskAssignRuleInfoApi () {
      return coordManageApi.taskAssignRuleInfo
    }
  }
}
</script>
<style lang="scss" scoped>
@import '~@/styles/assetoutfeedback/common.scss'
</style>
