<template>
  <el-dialog
      :modal="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      destroy-on-close
      title="政策法规"
      :model-value="dialogVisible"

      :before-close="handleClose"
  >
    <diy-el-form ref="formDialogRef" class="form" :forms="policyRegulationInformationFormConfig" v-model:model-form="form">
      <template #sltappend>
        <el-upload
        :limit="2"
        ref="upload"
        :show-file-list="false"
        class="my-el-upload"
        :on-change="onChange"
        :auto-upload="false"
        >
          <el-button class="upload-btn" size="small" type="primary">{{form.FILENAME}}</el-button>
        </el-upload>
      </template>
    </diy-el-form>

    <template #footer>
      <el-button :icon="SuccessFilled" @click="conform">确定</el-button>
      <el-button @click="handleClose" :icon="Close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {Close, Delete, Edit, Plus, SuccessFilled} from "@element-plus/icons-vue";
import DiyElForm from "@/components/common/DiyElForm.vue";
import {computed, onBeforeMount, ref} from "vue";
import {policyRegulationInformationForm} from "@/data/all-form";
import axios from "axios";
import {elAlert, handleMes} from "@/units/element-ui/tip";
import {proInsertXyxxFlag, proUpdateXyxxFlag
} from "@/service/policy-and-regulation-manage/policy-regulation-information/policy-regulation-information";
import {InsertSyslog} from "@/units/use-method/user-log";
export default {
  name: "PolicyRegulationInformationSettingDialog",
  components:{DiyElForm},
  computed: {
    Delete() {
      return Delete
    },
    Edit() {
      return Edit
    },
    Plus() {
      return Plus
    },
    SuccessFilled() {
      return SuccessFilled
    },
    Close() {
      return Close
    }
  },
  props:{
    dialogVisible:{  //是否可见

    },
    dialogOperationType:{   //操作类型

    },
    currentDialogObj:{   //当前选中信息

    },
  },
  setup(props,{emit}){
    const form = ref({});  //表单内容-值
    const formDialogRef =ref(null);  //表单ref
    const file = ref(null);
    const policyRegulationInformationFormConfig = ref({...policyRegulationInformationForm});  //表单配置项
    onBeforeMount(() =>{
      policyRegulationInformationFormConfig.value.formAttrs.disabled=props.dialogOperationType === 'detail';
      form.value = {
        ...props.currentDialogObj
      }
      form.value.slt = props.currentDialogObj.NAME;
    })
    function onChange(files){
      debugger
      file.value = files;
      form.value.slt=files.name;
      form.value.FILENAME = files.name;
    }
    function conform(){
      formDialogRef.value.refEl.validate(isValid => {
        if (isValid){
          debugger
          if(!props.currentDialogObj.CODE){
            let month = new Date().getMonth() + 1 < 10 ? "0" + (new Date().getMonth() + 1) : new Date().getMonth() + 1;
            let strDate = new Date().getDate() < 10 ? "0" + new Date().getDate() : new Date().getDate();
            let time4 = new Date().getFullYear() + "-" + month + "-" + strDate;
            let suffixName = file.value.name.substring(file.value.name.lastIndexOf(".")); //后缀名称
            let fileName = form.value.NAME + time4;
            debugger
            let formData = new FormData();
            formData.append("file",file.value.raw);
            formData.append("fileName",fileName + suffixName);
            formData.append("type",form.value.TYPE);
            axios.post('http://localhost:8081/upload/xypt',formData,{'Content-type':'multipart/form-data'}).then(res =>{
              console.log(res);
              if(res.data[0].success=='true'){
                insertFlag(fileName,suffixName)
              }else{
                elAlert("文件上传失败")
              }
            })
          }else{
            updateFlag()
          }
        }
      })
    }
    function insertFlag(fileName,suffixName){
      proInsertXyxxFlag({
        wenhao: form.value.WENHAO,
        name: form.value.NAME,
        type: form.value.TYPE,
        username: localStorage.getItem("xyxx_username"),
        suffixName: suffixName,
        fileName: fileName,
        pubTime:form.value.TIME,
      }).then(res =>{
        handleMes(res,() => {
          emit('getTableData');
          InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"),"信息新增", "政策法规信息新增成功", "政策法规信息", "")
          handleClose();
        })
      })
    }
    function updateFlag(){
      proUpdateXyxxFlag({
        code:props.currentDialogObj.CODE,
        wenhao: form.value.WENHAO,
        name: form.value.NAME,
        type: form.value.TYPE,
        suffixName: "",
        fileName: "",
        pubTime:form.value.TIME,
      }).then(res =>{
        handleMes(res,() => {
          emit('getTableData');
          InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"),"信息修改", "政策法规信息修改成功", "政策法规信息", "")
          handleClose()
        })
      })
    }
    function handleClose(){
      emit('update:dialogVisible',false);
    }
    return {
      form,
      formDialogRef,
      policyRegulationInformationFormConfig,
      conform,
      handleClose,
      onChange
    }
  }

}
</script>

<style scoped lang="scss">
:deep{
  .el-button>span{
    width: 100%;
    height: 100%;
    float: left !important;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .el-button--small{
    padding: 1px 11px;
  }
}
</style>