<template>
  <div class="rule-type">
    <DialogCom
      :title="id ? '编辑' : '新增'"
      :visible.sync="isShow"
      @close="onHide"
      width="600px"
    >
      <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="partyA" label="甲方:">
            <div>
              <el-input
                v-model="formData.partyA"
                placeholder="请输入甲方:"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="partyAOrg" label="甲方机构:">
            <div>
              <el-input
                v-model="formData.partyAOrg"
                placeholder="请输入甲方机构:"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="partyB" label="乙方:">
            <div>
              <el-input
                v-model="formData.partyB"
                placeholder="请输入乙方:"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="partyBOrg" label="乙方机构:">
            <div>
              <el-input
                v-model="formData.partyBOrg"
                placeholder="请输入乙方机构:"
              ></el-input>
            </div>
          </el-form-item>
          <!-- <el-form-item prop="userIds" label="签署人:">
            <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="time" label="签署时间:">
            <el-date-picker
              v-model="formData.time"
              :clearable="true"
              type="date"
              placeholder="选择时间"
              value-format="yyyy-MM-dd hh:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
          <el-col :xs="24" :sm="24" :md="24" :lg="24">
            <el-form-item label="上传文件" prop="files">
              <!-- <imgs-upload
                :limit="10"
                v-model="formData.files"
                :fileSize="2"
              ></imgs-upload> -->
              <W-file-upload
                ref="upload"
                :defaultValue="formFileListDefualtValue"
                v-model="formData.files"
              />
            </el-form-item>
          </el-col>
        </el-form>
      </div>

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

<script>
import { mapState, mapMutations } from "vuex";
import ImgsUpload from "@/components/ImgsUpload";
import UserSelector from "@/components/userSelector/index.vue";
import imgUpload from "@/components/ImageUpload/index.vue";
import WFileUpload from "@/components/W-FileUpload/index.vue";
export default {
  data() {
    return {
      formFileListDefualtValue: [],
      tem: null,
      id: null,
      isShow: false,
      formData: this.reset(),
      formDataRules: {
        partyA: [{ required: true, message: "请输入甲方" }],
        partyAOrg: [{ required: true, message: "请输入甲方机构" }],
        partyB: [{ required: true, message: "请输入乙方" }],
        partyBOrg: [{ required: true, message: "请输入乙方机构" }],
        userIds: [{ required: true, message: "请选择用户" }],
        time: [{ required: true, message: "请选择时间" }],
        files: [{ required: true, message: "请上传责任书" }],
      },
    };
  },

  watch: {},
  computed: {
    joinedStringArray: {
      get() {
        return this.formData.names.join(", ");
      },
      set(value) {
        this.formData.names = value.split(", ");
      },
    },
    ...mapState([]),
  },
  methods: {
    ...mapMutations([]),
    imageListChanged(list) {
      this.formData.files = list;
      console.log("files", this.formData.files);
    },
    //选择用户回调
    onUserSelect(data) {
      var ids = data.map((obj) => obj.id);
      var names = data.map((obj) => obj.name);
      this.formData.userIds = ids;
      this.formData.names = names;
      //   console.log(this.formData.userIds, "this.formData.userIds");
    },
    //用户选择
    selectUser(event) {
      console.log("ww");
      //   event.preventDefault();
      this.$refs.UserSelector.show();
    },

    reset() {
      return {
        id: null,
        userIds: [],
        time: null,
        files: [],
        names: [],
        partyA:null,
        partyAOrg:null,
        partyB:null,
        partyBOrg:null,
      };
    },
    async show(row) {
      this.tem = row;
      this.formFileListDefualtValue = [];
      // console.log(row);
      if (row != undefined && row != null) {
        this.id = row.id;
        this.formData = row;
        this.formFileListDefualtValue = row.files;
      } else {
        this.formData = this.reset();
      }

      // this.id = id;
      this.isShow = true;
    },

    // 事件
    onHide() {
      //   this.formData = this.reset();
      //   this.$refs.form.resetFields();
    },
    onSubmit() {
      this.$refs.form.validate(async (isValidate) => {
        if (!isValidate) return;
        this.$refs["UserSelector"].clearSelected();
        this.$emit("success", this.formData, this.tem);
        this.isShow = false;
      });
    },

    // 事件
    //apimark//
  },
  mounted() {},
  components: { ImgsUpload, UserSelector, imgUpload, WFileUpload },
};
</script>

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