<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form
        size="small"
        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="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          ></el-input-number>
        </el-form-item>

        <el-form-item label="公共库" prop="pub">
          <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="samplePath">
          <el-upload
            class="upload-demo"
            :action="api + 'sampleform/file/commonupload'"
            :on-remove="handleRemoveSamplePath"
            :file-list="samplePathFileList"
            :on-success="OnsuccessSamplePath"
            :headers="{
              Authorization: token,
            }"
          >
            <!-- accept="application/vnd.openxmlformats-officedocument.wordprocessingml.document" -->
            <el-button size="small" type="primary">上传文件</el-button>
            <!-- <span class="tips">提示：请上传.docx格式</span> -->
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button
          :loading="loading"
          size="small"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import local from "@/utils/local";
import { saveMaterials } from "@/api/materials";
import { mapGetters } from "vuex";
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: "", // 部门编码
        sort: 99, // 排序
      },
      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" },
        // ],
      },
    };
  },
  computed: {
    ...mapGetters(["token"]),
    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;
          this.loading = false;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    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;
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
        this.samplePathFileList = [
          {
            name: this.form.sampleName,
            url: this.form.samplePath,
          },
        ];
      }, 10);
    },
    // 重置
    handleRest() {
      this.resetForm("form");
      this.samplePathFileList = [];
    },
    // 关闭
    handleClose() {
      this.resetForm("form");
      // this.fileUrlFileList = [];
      // this.templatePathFileList = [];
      this.samplePathFileList = [];
      this.loading = false;
      this.Visible = false;
    },
    // 上传样表
    OnsuccessSamplePath(response, file, fileList) {
      this.samplePathFileList = fileList.slice(-1);
      this.form.samplePath = response.url;
      this.form.sampleName = response.fileName;
    },
    // 删除样表
    handleRemoveSamplePath() {
      this.form.samplePath = "";
      this.form.sampleName = "";
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-select {
  width: 100%;
}
.tips {
  margin-left: 10px;
  color: #ccc;
}
/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>
