<template>
  <el-dialog
    custom-class="party-dialog"
    title="修改"
    width="468px"
    :visible.sync="formVisible"
    :before-close="close"
  >
    <div class="dialog-content">
      <el-form
        :model="formItem"
        class="party-form"
        ref="formItem"
        label-width="125px"
        label-position="top"
        :rules="rules"
      >
        <el-form-item label="所属单位：" prop="orgId">
          <el-input v-model="formItem.organName" disabled></el-input>
        </el-form-item>
        <el-form-item label="机构地理位置：" prop="areaName">
          <el-input v-model="formItem.areaName" disabled></el-input>
        </el-form-item>
        <el-form-item label="账号有效期" prop="permanent">
          <el-radio-group v-model="formItem.permanent">
            <el-radio :label="true">永久有效</el-radio>
            <el-radio :label="false">设置有效期</el-radio>
          </el-radio-group>
          <div v-if="!formItem.permanent">
            <el-date-picker
              class="mt16"
              v-model="formItem.date"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer btn-group">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitForm('formItem')">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      formVisible: false,
      formItem: {
          date:'',
          permanent:true
      },
      rules: {
        permanent: [
          { required: true, message: "请选择账号有效期", trigger: "change" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    backFn(item) {
      this.formItem = {
        date:'',
        permanent:true
      },
      this.formVisible = true;
      for (let key in item) {
        this.formItem[key] = item[key];
      }
      // 编辑
      if (this.formItem.exiredDate) {
        this.formItem.date = [this.formItem.effectiveDate, this.formItem.exiredDate];
        this.formItem.permanent = false
      }
      this.formItem = {...this.formItem}
      console.log(this.formItem);
    },
    // 关闭
    close() {
      this.formVisible = false;
      for (let key in this.formItem) {
        this.formItem[key] = null;
      }
      this.$refs["formItem"].resetFields();
    },
    // 保存编辑信息
    submitForm() {
      let _this = this;
    if (!this.formItem.permanent && !this.formItem.date) {
    this.$message.error("请选择有效期");
    return false;
    }
    let searchObj = {};
    for (let key in _this.formItem) {
    if (this.formItem[key]) {
        searchObj[key] = _this.formItem[key];
    }
    }
    if (!this.formItem.permanent) {
    searchObj.effectiveDate = this.formItem.date[0];
    searchObj.exiredDate = this.formItem.date[1];
    }else{
      delete searchObj.effectiveDate;
      delete searchObj.exiredDate;
    }
    searchObj.orgName = searchObj.organName
    delete searchObj.organName;
    delete searchObj.date;
    console.log(searchObj);
    _this
    .$https(
        {
        url: "tUser/boxUpdate",
        method: "put",
        authType: this.backToken,
        },
        searchObj
    )
    .then(
              (res) => {
                if (res.data.resultCode === "200") {
                  _this.$message.success(res.data.message);
                  _this.formVisible = false;
                  _this.$emit("refreshFn");
                } else {
                  _this.$message.error(res.data.message);
                }
              },
              (error) => {
                console.log(error);
              }
            );

    },
  },
};
</script>

<style lang="less">
</style>