<template>
  <div class="rule-type">
    <DialogCom
      :title="id ? '编辑安全保卫部年度工作计划' : '新增安全保卫部年度工作计划'"
      :visible.sync="isShow"
      @close="onHide"
      width="700px"
    >
      <div class="page-body">
        <el-form
          :model="formData"
          :rules="formDataRules"
          size="small"
          ref="form"
          label-position="right"
          label-width="130px"
          label-prefix=":"
        >
          <el-form-item prop="orgId" label="行社名称:">
            <tree-select
              v-model="formData.orgId"
              :options="deptOptions"
              :show-count="true"
              :normalizer="tenantIdnormalizer"
              :props="{ checkStrictly: true, label: 'name' }"
              placeholder="请选择机构"
              :noChildrenText="''"
              noOptionsText="没有数据"
              noResultsText="没有搜索结果"
            />
          </el-form-item>
          <el-form-item prop="type" label="文件种类:">
            <el-input v-model="formData.type" maxlength="20"></el-input>
          </el-form-item>
          <el-form-item label="工作年度:" prop="year">
            <el-date-picker
              v-model="formData.year"
              :clearable="timeClearable"
              type="year"
              placeholder="请选择工作计划年度"
              value-format="yyyy"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="userIds" label="参与人员:" v-if="false">
            <div>
              <el-input
                :readonly="true"
                v-model="joinedStringArray"
                placeholder="点击选择"
              ></el-input>
              <el-button @click="selectUser">选择</el-button>
            </div>
          </el-form-item>
          <el-form-item prop="date" label="文件日期:">
            <el-date-picker
              v-model="formData.date"
              value-format="yyyy-MM-dd"
              placeholder="请选择文件发布日期"
              type="date"
              :clearable="false"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="文件资料" prop="images">
            <!-- <imgs-upload
              :limit="5"
              v-model="formData.images"
              :fileSize="2"
            ></imgs-upload> -->
            <!-- <imgUpload
              ref="uploadimage"
              type="alone"
              :value="formData.images"
              @input="imageListChanged"
              :fileType="[
                'pdf',
                'jpg',
                'png',
                'jpg',
                'bmp',
                'zip',
                'rar',
                '7z',
              ]"
            >
            </imgUpload> -->
            <W-file-upload
              :fileType="['docx','doc', 'pdf']"
              ref="upload"
              :defaultValue="formFileListDefualtValue"
              v-model="formData.images"
              :fileSize="25"
              @success="successHandler('form','images')"
            />
          </el-form-item>
          <div>
            <el-form-item label="内容:" prop="content" v-if="false">
              <Editor style="height: 150px" v-model="formData.content"></Editor>
            </el-form-item>
          </div>
        </el-form>
      </div>

      <div style="margin-top: 100px" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </DialogCom>
    <UserSelector
      ref="UserSelector"
      @select="onUserSelect"
      :clearSelectOnClose="false"
    ></UserSelector>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { mapGetters } from "vuex";
import { Add, edit, getaqbwbndjh } from "@/api/safetyBook/aqbwbndjh";
import { deptTreeSelect } from "@/api/system/public";
import UserSelector from "@/components/userSelector/index.vue";
import ImgsUpload from "@/components/ImgsUpload";
import Editor from "@/components/Editor";
import imgUpload from "@/components/ImageUpload/index.vue";
import WFileUpload from "@/components/W-FileUpload/index.vue";
import {noAuto} from '@/utils/fieldMxins.js'
export default {
  mixins:[noAuto],
  dicts: ["safety_book_type"],
  components: { UserSelector, ImgsUpload, Editor, imgUpload, WFileUpload },
  data() {
    return {
      formFileListDefualtValue: [],
      id: null,
      isShow: false,
      timeClearable: true,
      formData: this.reset(),
      //修改新增中的机构树
      deptOptions: [],
      formDataRules: {
        userIds: [{ required: true, message: "请选择参与人员" }],
        date: [{ required: true, message: "请选择日期" }],
        images: [{ required: true, message: "请上传年度工作计划文件" }],
        content: [{ required: true, message: "请输入内容" }],
        orgId: [{ required: true, message: "请选择机构" }],
        type: [{ required: true, message: "请输入文件种类" }],
        year: [{ required: true, message: "请选择年度" }],
      },
    };
  },

  watch: {},
  computed: {
    ...mapState([]),
    ...mapGetters(["orgId", "orgName"]),
    // joinedStringArray: {
    //   get() {
    //     if (this.formData.names == null || this.formData.names == undefined) {
    //       this.formData.names = this.formData.userNames.split(", ");
    //     }
    //     return this.formData.names.join(", ");
    //   },
    //   set(value) {
    //     this.formData.names = value.split(", ");
    //   },
    // },
  },
  methods: {
    ...mapMutations([]),
    imageListChanged(list) {
      this.formData.images = list;
      console.log("images", this.formData.images);
    },
    /** 查询机构树数据 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** treeSelect组件自定义数据*/
    tenantIdnormalizer(node, instanceId) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.shortName,
        children: node.children,
      };
    },

    reset() {
      return {
        id: null,
        // names: [],
        // userIds: [],
        date: null,
        images: [],
        // content: null,
        title: null,
        orgId: null,
        type: "工作计划",
        year: null,
      };
    },
    async refresh(id) {
      this.formFileListDefualtValue = [];
      if (id != null && id != undefined) {
        await getaqbwbndjh(id).then((res) => {
          // this.formData.names = res.data.userNames.split(",");
          this.formData = res.data;
          this.formFileListDefualtValue = res.data.images;
          // this.formData.year=res.data.year;
        });
      }
    },
    async show(id) {
      // console.log(id, "id");
      this.getDeptTree();
      this.formData = this.reset();
      this.id = id;
      await this.refresh(id);
      if (id) {
      } else {
        // console.log(this.orgId,"this.orgId")
        this.formData.orgId = this.orgId;
      }
      this.isShow = true;
    },

    // 事件
    onHide() {
      this.formData = this.reset();
      this.$refs.form.resetFields();
    },
    onSubmit() {
      console.log(this.formData, "this.formData");
      this.$refs.form.validate(async (isValidate) => {
        if (!isValidate) return;
        if (this.id == null || this.id == undefined) {
          await Add(this.formData).then((res) => {
            this.$emit("success");
            this.isShow = false;
          });
        } else {
          await edit(this.formData).then((res) => {
            this.$emit("success");
            this.isShow = false;
          });
          this.$refs["UserSelector"].clearSelected();
        }
      });
    },
    //选择用户回调
    onUserSelect(data) {
      var ids = data.map((obj) => obj.id);
      var names = data.map((obj) => obj.name);
      this.formData.userIds = ids;
      this.formData.names = names;
      this.formData.userNames = names.join(",");
      //   console.log(this.formData.userIds, "this.formData.userIds");
    },
    //用户选择
    selectUser(event) {
      //   event.preventDefault();
      this.$refs.UserSelector.show();
    },

    // 事件
    //apimark//
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.brand_info {
  .el-form {
    width: 600px;
    padding-top: 40px;
  }
}
</style>
