<template>
  <el-dialog :close-on-click-modal="false" v-dialogDrag title="简化组件" :visible.sync="fileStatus" width="40%"
    :before-close="cancel">
    <el-form :model="form" label-width="100px" :rules="rules" ref="ruleForm">
      <el-form-item label="全宗号：" prop="fondscode">
        <el-select size="mini" v-model="form.fondscode" @change="changeFonds" disabled>
          <el-option v-for="(op,index) in fondsList" :label="op.codeName" v-model="op.fondscode" :key="index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年度：" prop="year">
        <el-input-number size="mini" v-model="form.year" controls-position="right" disabled>
        </el-input-number>
      </el-form-item>
      <!-- <el-form-item label="目标门类：" prop="targetClassId">
        <el-select size="mini" v-model="form.targetClassId" @change="changeClass">
          <el-option v-for="(op,index) in classList" :label="op.className" :value="op.id" :key="index">
          </el-option>
        </el-select>
      </el-form-item> -->
      <div v-for="(item,key) in dhForm" :key="key">
        <el-form-item
          v-if="item.segmentName!='fonds_no'  && item.segmentName!='filing_year' && item.segmentName!='piece_no'"
          label-width="100px" :prop="item.segmentName">
          <span slot="label" class="biaoshi">{{item.segmentChnName}}</span>
          <el-input v-if="item.segmentName==='archive_ctg_no'" @click.native="focus" style="width:60%" size="mini"
            v-model="item.value">
          </el-input>
          <el-select v-if="item.segmentName==='retention'" style="width:60%" size="mini" v-model="item.value">
            <el-option v-for="(ele,index) in safekeeping" :key="index" :label="ele.label"
              :value="item.segmentVal==='值'?ele.value:ele.label">
            </el-option>
          </el-select>
          <el-input
            v-if="item.segmentName!='archive_ctg_no' &&item.segmentName!='retention' && item.segmentName!='piece_no' "
            style="width:60%" size="mini" v-model="item.value"></el-input>
        </el-form-item>
      </div>
      <el-form-item label=" ">
        <el-button v-loading="btnloading" :disabled="!dhForm.length" @click="submit()" size="mini" type="primary">确定
        </el-button>
        <el-button @click="cancel" size="mini" type="info">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog v-dialogDrag width="60%" title="选择实体分类号" :close-on-click-modal="false" :visible.sync="NumFlage"
      :before-close="cancels" append-to-body top="10px">
      <div class="enClass">
        <el-card class="treeClass">
          <el-tree v-loading="treeLoading" highlight-current :data="treeData" :props="defaultProps" default-expand-all
            @node-click="handleNodeClick">
            <span slot-scope="{ node, data }" class="custom-tree-node">
              <!--可以根据情况，以数据类型来显示不同的图标或图片-->
              <svg-icon icon-class="wjj" style="margin-right: 5px; color: rgb(255, 207, 92)"
                v-if="data.classType === 'L'" class="iconfont icon-folder-open-fill" />
              <svg-icon icon-class="wj" style="margin-right: 5px; color: rgb(255, 207, 92)" v-else
                class="iconfont icon-wenjian" />
              <span>{{ node.label }}</span>
            </span>
          </el-tree>
        </el-card>
        <el-card class="tableClass">
          <!-- <el-table border :data='entityData' max-height="300">
            <el-table-column align="center" type="index" width="60" label="序号"></el-table-column>
            <el-table-column align="center" prop="archiveScope" label="归档范围"></el-table-column>
            <el-table-column align="center" prop="retention" label="保管期限"></el-table-column>
          </el-table> -->
          <div style="padding:5px 0;text-align:right">
            <el-button @click="submitEntity" size="mini" type="primary" :disabled="JSON.stringify(entreeRow)==='{}'">确定
            </el-button>
          </div>
        </el-card>
      </div>
    </el-dialog>
  </el-dialog>
</template>
<script>
  import {
    getArchiveCtgTree,
    getTableList
  } from "@/api/system-management/fileSettings"
  import {
    jianhuaZujian
  } from "@/api/archives-management/ungroupedManagement/simplifiedPaper"
  import {
    message
  } from "@/utils/common";
  export default {
    props: {
      safekeeping: {
        type: Array,
        default: () => [],
      },
      fondsList: {
        type: Array,
        default: () => [],
      },
      fileStatus: {
        type: Boolean,
        default: () => false,
      },
      form: {
        type: Object,
        default: () => {},
      },
      classList: {
        type: Array,
        default: () => [],
      },
      dhForm: {
        type: Array,
        default: () => [],
      },
      treeRow: {
        type: Object,
        default: () => {},
      },
      Alltable: {
        type: Array,
        default: () => [],
      },
    },
    data() {
      return {
        btnloading: false,
        defaultProps: {
          children: "childArchive",
          label: "codeAndName",
        },
        rules: {
          fondscode: [{
            required: true,
            message: '请输入全宗号',
            trigger: 'blur'
          }],
          year: [{
            required: true,
            message: '请输入归档年度',
            trigger: 'blur'
          }],
          targetClassId: [{
            required: true,
            message: '请选择门类',
            trigger: 'change'
          }],
        },
        treeData: [],
        treeLoading: false,
        NumFlage: false,
        entityData: [],
        entreeRow: {},
      }
    },
    methods: {
      submitEntity() {
        this.dhForm.forEach(item => {
          if (item.segmentChnName === "实体分类号") {
            item.value = this.entreeRow.codeAndName
          }
        })
        this.cancels()
      },
      submit() {


        let data = {
          classId: this.treeRow.id,
          filingYear: this.form.year,
          fondscode: this.form.fondscode,
          archivenoFormatList: this.dhForm,
          jianhuaIdList: this.Alltable[0].selectTableTwo.map(ele => ele.id),
        }
        let flage = false
        try {
          this.dhForm.forEach(ele => {
            if (!ele.value && ele.segmentName != 'piece_no') {
              flage = true
              throw (ele.segmentChnName);
            }
          })
        } catch (err) {
          message("warning", `请填写${err}`, 3000)
        }
        if (flage) {
          return
        }
        this.btnloading = true
        jianhuaZujian(data).then(res => {
          message("success", "组件成功", 3000)
          this.$emit("Refresh")
          this.cancel()
        }).finally(res => {
          this.btnloading = false
        })
      },
      // 点击树节点
      handleNodeClick(data, node) {
        this.entreeRow = data
        let info = {
          archiveCtgCode: data.archiveCtgCode,
          fondscode: this.form.fondscode,
          page: 1,
          limit: 1000
        }
        getTableList(info).then(res => {
          this.entityData = res.body.list
        })
      },
      cancels() {
        this.NumFlage = false
      },
      focus() {
        this.treeLoading = true
        let data = {
          fondscode: this.form.fondscode,
          year: this.form.year
        }
        getArchiveCtgTree(data).then(res => {
          if (!res.body.list.length) {
            message("warning", "请先配置当前年份的实体分类号", 3000)
            return
          }
          this.treeData = res.body.list
          this.NumFlage = true
          this.treeLoading = false
        }).catch(err => {
          this.treeLoading = false
        })
      },
      blur() {
        if (this.form.year.toString().length != 4) {
          message('warning', '请输入正确的年度', 2000)
          return
        }
        if (this.form.fondscode && this.form.targetClassId) {
          let data = {
            fondscode: this.form.fondscode,
            targetClassId: this.form.targetClassId,
            filingYear: this.form.year
          }
          this.$store.dispatch('filing/showFileArchive', data).then(res => {

            this.dhForm.forEach(item => {
              if (item.segmentName === "fonds_no") {
                item.value = this.form.fondscode
              }
              if (item.segmentName === "filing_year") {
                item.value = this.form.year
              }
              item.filingYear = this.form.year
              item.id = 0
            })
          }).catch(err => {
            this.dhForm = []
          })
        }
      },
      cancel() {
        this.$emit("cancel")
      },
      changeFonds() {
        if (this.form.targetClassId && this.form.year) {
          let data = {
            fondscode: this.form.fondscode,
            targetClassId: this.form.targetClassId,
            filingYear: this.form.year
          }
          this.$store.dispatch('filing/showFileArchive', data).then(res => {
            this.dhForm.forEach(item => {
              if (item.segmentName === "fonds_no") {
                item.value = this.form.fondscode
              }
              if (item.segmentName === "filing_year") {
                item.value = this.form.year
              }
              item.filingYear = this.form.year
              item.id = 0
            })
            // dhForm
          }).catch(err => {
            this.dhForm = []
          })
        }
      },
    }
  }

</script>
<style lang="scss" scoped>
  .biaoshi:before {
    content: '*';
    color: #FB3847;
    margin-right: 4px;
  }

</style>
