<!--自定义盖章配置新增-->
<template>
  <div>
    <dialog-edit :width="'900px'"
                 :visible.sync="dialogVisible" :editModel="editModel"
                 :formRules="formRules"
                 :autoSave="false"
                 :openPerms="true" :menuCode="menuCode" savePerms="btnSavePerms"
                 :loading="dialogSaveLoading"
                 @onSaved="save"
                 :footerBtnShow="true"
                 :form="formValue"
                 :title="editModel === 'C' ? $t('documentSealingConfig.editPageHead') : $t('documentSealingConfig.editPageHeadEdit')"
                 :api="api">
      <el-form :rules="formRules" :model="formValue" ref="formName">
        <el-form-item :label="$t('documentSealingConfig.stampParty')" :label-width="'100px'" class="dymForm" prop="assetSources">
          <select-dictionary v-model="formValue.assetSources"
                             :multiple="true"
                             :reload="reload"
                             :name="'assetSources'"
                             @selectChanged="selectChanged"
                             :exclude="exclude"
                             :tags="false"
                             :clearable="true" :dictTypeCode="'SCRS0138'"></select-dictionary>
        </el-form-item>
        <el-form-item :label="$t('documentSealingConfig.materialName')" :label-width="'100px'" prop="keyId">
          <div class="flex-dis-div">
            <div class="flex-dis-1">
              <!-- $t('documentSealingConfig.selectmaterialName') -->
              <select-file-name :clearable="true" :reload="NameReload" v-model="formValue.keyId" class="dymForm"
                                :placeholder="$t('common.pleaseSelect')" @selectChanged="selectChangedFileName"></select-file-name>
            </div>
            <div class="flex-dis-0">
              <auth-button size="small" class="btn-left" :label="$t('documentSealingConfig.addmaterial')"
                           :menuCode="menuCode" perms="btnAddmaterial" :openPerms="true" icon="iconfont iconxinzeng"
                           @click="addZiliao({}, 'C')" type="primary"></auth-button>
            </div>
          </div>
        </el-form-item>
        <div v-for="(item, idx) in formValue.assetCustomStampConfigEnterpriseDtoList" :key="idx">
          <div v-if="item.show">
            <span class="font-class">{{item.participant | formatDict('SCRS0138')}}</span>
            <el-divider/>
            <el-form-item label="">
              <el-radio-group v-model="item.condition" @change="radioChange(arguments, item)" class="radio-width">
                <el-col :span="12">
                  <el-radio :label="0">
                    {{$t('documentSealingConfig.sealKeywords')}}
                    <el-input class="input-left" :disabled="item.condition !== 0"
                              v-model="item.keyword"></el-input>
                  </el-radio>
                </el-col>
                <el-col :span="10">
                  <el-radio :label="1" class="radio-margin">
                    {{$t('documentSealingConfig.sealLocation')}}
                    <select-dictionary :disabled="item.condition !== 1"
                                       v-model="item.location" class="select-margin"
                                       :clearable="true" :dictTypeCode="'ZD0083'"
                                       :placeholder="''">
                    </select-dictionary>
                  </el-radio>
                </el-col>
                <el-col :span="2">
                  <auth-button v-if="orderByIndexFun() !== idx" size="mini"
                               class="iconCircle btnUpLeft" icon="iconfont iconshangchuan1" @click="up(idx)"></auth-button>
                </el-col>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
      </el-form>
<!--      <template slot="footer">-->
<!--        <auth-button size="medium" type="primary" :label="$t('common.save')" @click="save"></auth-button>-->
<!--        <auth-button size="medium" :label="$t('common.close')" @click="dialogVisible = false" :openPerms="false"></auth-button>-->
<!--      </template>-->
    </dialog-edit>
    <document-sealing-config-add-file :visible.sync="editVisible" :menuCode="menuCode"
                                      :form="formItem" :editModel="fileEditModel" @add-datum="addDatum"></document-sealing-config-add-file>
  </div>
</template>

<script>
import BasePage from '@/pages/baseEditPage'
import documentSealingConfigAddFile from '@/pages/shourang/documentSealingConfig/documentSealingConfigAddFile'
import { DialogEdit, authButton, SelectDictionary } from 'newepoch-components'
import SelectFileName from '@/components/element/shourang/SelectFileName'
import uninvoiceApi from '@/api/uninvoiceApi'
export default {
  extends: new BasePage(),
  name: 'documentSealingConfigCreate',
  components: {
    SelectFileName,
    documentSealingConfigAddFile,
    DialogEdit,
    authButton,
    SelectDictionary
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      dialogSaveLoading: false,
      fileEditModel: '',
      refreshFlag: false,
      editVisible: false,
      formItem: {},
      formValue: {},
      reload: false,
      NameReload: false,
      exclude: [],
      // exclude: [{ dictId: '2' }, { dictId: '1' }],
      formRules: {
        assetSources: [
          {
            required: true,
            message: this.$i18n.messages[this.$i18n.locale].documentSealingConfig.rulesstampParty,
            trigger: 'change'
          }
        ],
        keyId: [
          {
            required: true,
            message: this.$i18n.messages[this.$i18n.locale].documentSealingConfig.rulesmaterialName,
            trigger: 'change'
          }
        ]
      }
    }
  },
  computed: {
    orderByIndexFun () {
      return () => {
        let indexLet = 0
        this.formValue.assetCustomStampConfigEnterpriseDtoList.some((it, index) => {
          if (it.show) {
            indexLet = index
            return true
          }
        })
        return indexLet
      }
    },
    api () {
      return uninvoiceApi.customconfigpage
    }
  },
  methods: {
    radioChange (val, item) {
      if (val[0] === 1) {
        this.$set(item, 'keyword', '')
      } else {
        this.$set(item, 'location', '')
      }
    },
    addDatum () {
      this.NameReload = !this.NameReload
      this.refreshFlag = true
    },
    // 置顶该盖章
    up (index) {
      let stampOrderA = this.formValue.assetCustomStampConfigEnterpriseDtoList[index].stampOrder
      let stampOrderB = this.formValue.assetCustomStampConfigEnterpriseDtoList[index - 1].stampOrder
      this.formValue.assetCustomStampConfigEnterpriseDtoList[index].stampOrder = stampOrderB
      this.formValue.assetCustomStampConfigEnterpriseDtoList[index - 1].stampOrder = stampOrderA
      this.sortDict()
      return this.formValue
    },
    selectChangedFileName (val, name, row) {
      if (!this.UtilJs.isEmpty(val)) {
        this.formValue.assetName = row.assetName
        this.formValue.ifsEnabled = row.ifsEnabled
      }
    },
    selectChanged (a, b, c) {
      if (this.UtilJs.isEmpty(a)) return
      this.formValue.assetCustomStampConfigEnterpriseDtoList.forEach(it => {
        it.stampOrder = this.zidianCXPX(Number(it.participant))
        let sho = a.some(it1 => {
          if (it.participant === it1) {
            this.$set(it, 'show', true)
            return true
          }
        })
        if (!sho) {
          it.keyword = ''
          it.stampLocation = ''
          this.$set(it, 'show', false)
        }
      })
      this.sortDict()
    },
    save () {
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          let putData = this.formValue.assetCustomStampConfigEnterpriseDtoList.filter(it => {
            if (it.show === true) {
              it.keyword = it.condition === 1 ? '' : it.keyword
              it.stampLocation = it.condition === 0 ? '-1' : it.location
              it.assetCustomStampConfigId = this.formValue.keyId
            }
            return it.show === true
          })
          this.formValue.participateList = putData
          this.dialogSaveLoading = true
          if (this.editModel === 'C') {
            uninvoiceApi.customconfigpage.setpostEnableConfigConfig(putData).then(res => {
              if (res.data) {
                this.success(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.addseccessinfo)
                this.$emit('refresh-table')
                this.dialogVisible = false
              } else {
                this.error(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.addfialinfo)
              }
            }).finally(res => {
              this.dialogSaveLoading = false
            })
          } else {
            uninvoiceApi.customconfigpage.setEnableConfigConfig(this.formValue).then(res => {
              if (res.data) {
                this.success(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.editseccessinfo)
                this.$emit('refresh-table')
                this.dialogVisible = false
              } else {
                this.error(this.$i18n.messages[this.$i18n.locale].documentSealingConfig.editfialinfo)
              }
            }).finally(() => {
              this.dialogSaveLoading = false
            })
          }
        } else {
          this.error(this.$i18n.messages[this.$i18n.locale].common.formrulesMessage)
        }
      })
    },
    addZiliao (data, model) {
      this.fileEditModel = model
      this.formItem = data
      this.editVisible = true
    },
    // 字典重新排序
    zidianCXPX (index) {
      let result = 0
      if (index === 1) result = 1
      if (index === 3) result = 3
      if (index === 4) result = 2
      if (index === 5) result = 4
      return result
    },
    // 字典返重新排序
    zidianFCXPX (index) {
      let result = 0
      if (index === 1) result = 1
      if (index === 2) result = 3
      if (index === 3) result = 4
      if (index === 4) result = 5
      return result
    },
    // 排序
    sortDict () {
      this.formValue.assetCustomStampConfigEnterpriseDtoList.sort((a, b) => {
        return a.stampOrder - b.stampOrder
      })
    }
  },
  watch: {
    dialogVisible: {
      deep: true,
      immediate: true,
      handler (n, o) {
        if (n) {
          this.$nextTick(() => {
            this.addDatum()
            this.$refs['formName'].clearValidate()
            this.dialogSaveLoading = false
          })
          this.formValue = Object.assign({}, this.form)
          this.$store.dispatch('GetDictionary', { sys: null, dictTypeCode: 'SCRS0138' }).then(res => {
            res.forEach((it1, index) => {
              let listshow = false
              if (Array.isArray(this.formValue.assetCustomStampConfigEnterpriseDtoList)) {
                listshow = this.formValue.assetCustomStampConfigEnterpriseDtoList.some(it => {
                  if (it1.dictId === it.participant) {
                    this.$set(it, 'show', true)
                    if (it.keyword && it.keyword.length > 0) {
                      this.$set(it, 'condition', 0)
                    } else {
                      this.$set(it, 'condition', 1)
                    }
                    if (it.stampLocation === '-1' || it.stampLocation === '0') {
                      this.$set(it, 'location', '')
                    } else {
                      this.$set(it, 'location', it.stampLocation)
                    }
                    return true
                  }
                })
              }
              if (!listshow) {
                let asd = Object.assign({}, {
                  assetCustomStampConfigId: '',
                  keyId: '',
                  condition: 0,
                  show: false,
                  participant: it1.dictId,
                  keyword: '',
                  stampLocation: '',
                  location: '',
                  stampOrder: this.zidianCXPX(Number(it1.dictId))
                })
                this.formValue.assetCustomStampConfigEnterpriseDtoList.push(asd)
                this.sortDict()
              }
            })
          })
        } else {
          if (o !== undefined) {
            this.$refs['formName'].clearValidate()
            if (this.refreshFlag) {
              this.$emit('refresh-datum')
            }
          }
        }
      }
    }
  }
}
</script>

<style scoped lang="scss" src="@/styles/shourang/documentSealingConfig/documentSealingConfigCreate.scss">
</style>
