<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="材料简称" prop="materialName">
          <el-input
            size="small"
            v-model="form.materialName"
            placeholder="请输入材料简称"
          ></el-input>
        </el-form-item>
        <el-form-item label="材料全称" prop="materiaFullName">
          <el-input
            size="small"
            v-model="form.materiaFullName"
            placeholder="请输入材料全称"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属事项">
          <el-input disabled size="small" v-model="form.matterName"></el-input>
        </el-form-item>
        <el-form-item label="事项编码">
          <el-input disabled size="small" v-model="form.matterNo"></el-input>
        </el-form-item>
        <el-form-item label="公共库" prop="name">
          <el-switch
            class="tableScopeSwitch"
            inactive-text="不加"
            active-text="加入"
            :active-value="1"
            :inactive-value="0"
            v-model="form.pub"
          >
          </el-switch>
        </el-form-item>
        <!--
        <el-form-item label="上传附件" prop="fileUrl">
          <el-upload
            class="upload-demo"
            :action="api + 'sampleform/file/commonupload'"
            :on-remove="handleRemoveFileUrl"
            :limit="1"
            :file-list="fileUrlFileList"
            :on-success="OnsuccessfileUrl"
            :headers="headers"
            :before-upload="beforeUpload"
            accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
          >
            <el-button size="small" type="primary">上传Word文件</el-button>
          </el-upload>
        </el-form-item>
       <el-form-item label="上传模板" prop="templatePath">
          <el-upload
            class="upload-demo"
            :action="api + 'sampleform/file/commonupload'"
            :on-remove="handleRemoveTemplatePath"
            :limit="1"
            :file-list="templatePathFileList"
            :on-success="OnsuccesstemplatePath"
            :headers="headers"
            :before-upload="beforeUpload"
            accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
          >
            <el-button size="small" type="primary">上传Word文件</el-button>
          </el-upload>
        </el-form-item>-->
        <el-form-item label="上传样表" prop="samplePath">
          <el-upload
            class="upload-demo"
            :action="api + 'sampleform/file/commonupload'"
            :on-remove="handleRemoveSamplePath"
            :limit="1"
            :file-list="samplePathFileList"
            :on-success="OnsuccessSamplePath"
            :headers="headers"
            :before-upload="beforeUpload"
            accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
          >
            <el-button size="small" type="primary">上传Word文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          :loading="loading"
          size="small"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
        <el-button size="small" @click="handleRest">重 置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import local from "@/utils/local";
import { saveMaterials } from "@/api/materials";
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    addMaterialsVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      api: process.env.VUE_APP_API_BASE_URL + "/",
      form: {
        materialName: "", // 材料名称
        materiaFullName: "", // 材料全名
        source: 1, // 来源
        matterId: "", // 事项id
        siteId: local.getLocal("sampleSiteId")
            ? local.getLocal("sampleSiteId")
            : "",
        matterNo: "", // 事项编号
        fileName: "", // 附件名称
        fileUrl: "", // 附件地址
        samplePath: "", // 样表地址
        sampleName: "", // 样表名称
        templatePath: "", // 模板地址
        templateName: "", // 模板名称
        matterName: "", // 事项名称
        deptName: "", // 部门名称
        deptId: "", // 部门id
        deptCode: "", // 部门编码
      },
      fileUrlFileList: [],
      templatePathFileList: [],
      samplePathFileList: [],
      loading: false,
      rules: {
        materialName: [
          { required: true, message: "请输入简称", trigger: "blur" },
        ],
        materiaFullName: [
          { required: true, message: "请输入全称", trigger: "blur" },
        ],
        // fileUrl: [{ required: true, message: "请上传表单", trigger: "change" }],
        samplePath: [
          { required: true, message: "请上传样表", trigger: "change" },
        ],
        // templatePath: [
        //   { required: true, message: "请上传模板", trigger: "change" },
        // ],
      },
      headers: {
        Authorization: local.getLocal("sampleToken")
          ? local.getLocal("sampleToken")
          : "",
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.addMaterialsVisible;
      },
      set(val) {
        this.$emit("update:addMaterialsVisible", val);
      },
    },
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let res = await saveMaterials(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.loading = false;
            this.handleClose();
          } else {
            this.loading = false;
          }
        }
      });
    },
    // 新增
    onAdd(row) {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
      this.form.matterId = row.id;
      this.form.matterNo = row.matterNo;
      this.form.matterName = row.matterName;
      this.form.deptName = row.deptName;
      this.form.deptId = row.deptId;
      this.form.deptCode = row.deptCode;
      console.log(row);
    },
    // 编辑
    onEdit(row) {
      this.form = { ...row };
      this.fileUrlFileList = [
        {
          name: this.form.fileName,
          url: this.form.fileUrl,
        },
      ];
      this.templatePathFileList = [
        {
          name: this.form.templateName,
          url: this.form.templatePath,
        },
      ];
      this.samplePathFileList = [
        {
          name: this.form.sampleName,
          url: this.form.samplePath,
        },
      ];
    },
    // 重置
    handleRest() {
      this.$refs.form.resetFields();
      this.fileList = [];
    },
    // 关闭
    handleClose() {
      this.$refs.form.resetFields();
      this.fileUrlFileList = [];
      this.templatePathFileList = [];
      this.samplePathFileList = [];
      this.Visible = false;
    },
    // 上传之前
    beforeUpload(file) {
      console.log(file);
    },
    // 上传附件
    OnsuccessfileUrl(response) {
      this.form.fileUrl = response.url;
      this.form.fileName = response.fileName;
    },
    // 上传模板
    OnsuccesstemplatePath(response) {
      this.form.templatePath = response.url;
      this.form.templateName = response.fileName;
    },
    // 上传样表
    OnsuccessSamplePath(response) {
      this.form.samplePath = response.url;
      this.form.sampleName = response.fileName;
    },
    // 删除附件
    handleRemoveFileUrl() {
      this.form.fileUrl = "";
      this.form.fileName = "";
    },
    // 删除模板
    handleRemoveTemplatePath() {
      this.form.templatePath = "";
      this.form.templateName = "";
    },
    // 删除样表
    handleRemoveSamplePath() {
      this.form.samplePath = "";
      this.form.sampleName = "";
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-select {
  width: 100%;
}

/deep/.tableScopeSwitch .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
}
/*打开时文字位置设置*/
/deep/.tableScopeSwitch .el-switch__label--right {
  z-index: 1;
  left: 0px; /*不同场景下可能不同，自行调整*/
}
/*关闭时文字位置设置*/
/deep/.tableScopeSwitch .el-switch__label--left {
  z-index: 1;
  right: 0px; /*不同场景下可能不同，自行调整*/
}
/*显示文字*/
/deep/.tableScopeSwitch .el-switch__label.is-active {
  display: block;
}
/deep/.tableScopeSwitch.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 60px !important; /*开关按钮的宽度大小*/
}
</style>