<template>
  <el-dialog v-dialogDrag :title="ArchiveScopeTitle +'归档范围'" :close-on-click-modal="false"
    :visible.sync="ArchiveScopeStatus" width="40%" top="10px" :before-close="cancel">
    <el-form :model="form" ref="ruleForm" label-width="100px" :rules="rules" style="padding:20px">
      <el-form-item label="分类号：">
        <el-input clearable style="width:80%" size="mini" disabled v-model="form.archiveCtgCode"></el-input>
      </el-form-item>
      <el-form-item label="归档范围：">
        <el-input clearable style="width:80%" size="mini" type="textarea" v-model="form.archiveScope"></el-input>
      </el-form-item>
      <el-form-item label="保管期限：" prop="retention">
        <el-select clearable style="width:80%" v-model="form.retention" size="mini">
          <el-option v-for="(item,key) in findList" :key="key" :label="item.label" :value="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密级：" prop="securityClass">
        <el-select clearable style="width:80%" v-model="form.securityClass" size="mini">
          <el-option v-for="(item,key) in securityList" :key="key" :label="item.label" :value="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="存储参数：">
        <el-input clearable style="width:80%" size="mini" v-model="form.storageParameter"></el-input>
      </el-form-item>
      <el-form-item v-for="(item,key) in keworld" :key="key" :label="item.lable+'：'">
        <el-input clearable style="width:80%" size="mini" v-model="item.value"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-row>
          <el-col :span='14'>
            <el-button @click="btnClick(1)" size="mini" type="success">新增关键字</el-button>
            <el-button @click="btnClick(2)" size="mini" type="danger">删除关键字</el-button>
          </el-col>
          <el-col :span='10' style="text-align:right">
            <el-button :loading="btnloading" @click="btnClick(3)" size="mini" type="primary">保存</el-button>
            <el-button @click="cancel" size="mini" type="info">取消</el-button>
          </el-col>
        </el-row>

      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
  import {
    clearObjValue
  } from '@/utils/common';
  import {
    mapState
  } from "vuex";
  export default {
    props: {
      treeRow: {
        type: Object,
        default: () => {},
      },
      treeNode: {
        type: Object,
        default: () => {},
      },
      ArchiveScopeStatus: {
        type: Boolean,
        default: () => false,
      },
      tableRow: {
        type: Object,
        default: () => {},
      },
      ArchiveScopeTitle: {
        type: String,
        default: () => '新增',
      },
      fondsname: {
        type: String,
        default: () => '',
      },
      securityList:{
        type:Array,
        default:()=>[]
      }
    },
    computed: {
      ...mapState({
        findList: (state) => state.fileSettings.findList,
      })
    },
    data() {
      return {
        btnloading: false,
        form: {
          archiveCtgCode: "",
          archiveScope: "",
          retention: "",
          securityClass:"",
          storageParameter:"",
        },
        keworld: [{
          lable: '关键字',
          value: ''
        }],
        rules: {
          retention: [{
            required: true,
            message: '请选择保管期限',
            trigger: 'change'
          }],
          securityClass: [{
            required: true,
            message: '请选择密级',
            trigger: 'change'
          }]
        }
      }
    },
    methods: {
      btnClick(val) {
        switch (val) {
          case 1:
            let info = {
              lable: '关键字',
              value: ''
            }
            this.keworld.push(info)
            break;
          case 2:
            this.keworld.pop()
            break;
          case 3:
            this.$refs['ruleForm'].validate((valid) => {
              if (valid) {
                let data = {
                  archiveCtgCode: this.form.archiveCtgCode,
                  archiveScope: this.form.archiveScope,
                  retention: this.form.retention,
                  securityClass:this.form.securityClass,
                  storageParameter:this.form.storageParameter,
                  fondscode: this.fondsname,
                  titleKeyword: ""
                }
                let str = []
                this.keworld.forEach(item => {
                  if (item.value) {
                    str.push(item.value)
                  }
                })
                data.titleKeyword = str.join(",")
                this.btnloading = true;
                if (this.ArchiveScopeTitle === '新增') {
                  data.dataFlag = 2
                  this.$store.dispatch('fileSettings/saveArchiveScope', data).then(res => {
                    this.btnloading = false;
                    this.cancel()
                    this.$emit('handleNodeClick', this.treeRow, this.treeNode)
                  }).catch(err=>{
                    this.btnloading = false;
                  })
                } else {
                  data.id = this.tableRow.id
                  this.$store.dispatch('fileSettings/updateArchiveScope', data).then(res => {
                    this.btnloading = false;
                    this.cancel()
                    this.$emit('handleNodeClick', this.treeRow, this.treeNode)
                  }).catch(err=>{
                    this.btnloading = false;
                  })
                }
              } else {
                return false;
              }
            });
            break;
        }
      },
      cancel() {
        clearObjValue(this.form)
        this.keworld = [{
            lable: '关键字',
            value: ''
          }],
          this.$refs['ruleForm'].resetFields();
        this.$emit('cancel')
      }
    }
  }

</script>
