<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="MemorandumManageFormConfig" 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 v-if="dialogOperationType != 'detail'" #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 {MemorandumManageForm} from "@/data/all-form";
import {elAlert, handleMes} from "@/units/element-ui/tip";
import axios from "axios";
import {
  proInsertXyxxBwl,
  proUpdateXyxxBwl
} from "@/service/policy-and-regulation-manage/memorandum-manage/memorandum-manage";
import {InsertSyslog} from "@/units/use-method/user-log";
export default {
  name: "MemorandumManageSettingDialog",
  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 MemorandumManageFormConfig = ref({...MemorandumManageForm});  //表单配置项
    onBeforeMount(() =>{
      MemorandumManageFormConfig.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","bwl")
            axios.post('http://localhost:8081/upload/xypt',formData,{'Content-type':'multipart/form-data'}).then(res =>{
              console.log(res);
              if(res.data[0].success=='true'){
                insertBwl(fileName,suffixName)
              }else{
                elAlert("文件上传失败")
              }
            })
          }else{
            updateBwl()
          }
        }
      })
    }
    function insertBwl(fileName,suffixName){
      proInsertXyxxBwl({
        wenhao: form.value.WENHAO,
        name: form.value.NAME,
        username: localStorage.getItem("xyxx_username"),
        suffixName: suffixName,
        fileName: fileName,
      }).then(res =>{
        handleMes(res,() => {
          emit('getTableData');
          InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"),"信息新增", "备忘录管理新增成功", "备忘录管理", "")
          handleClose()
        })
      })
    }
    function updateBwl(){
      proUpdateXyxxBwl({
        code:props.currentDialogObj.CODE,
        wenhao: form.value.WENHAO,
        name: form.value.NAME,
        suffixName: "",
        fileName: "",
      }).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,
      MemorandumManageFormConfig,
      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>