<template>
  <el-dialog
    custom-class="party-dialog"
    title="编辑统计账号"
    :visible.sync="dialogVisible"
    width="468px"
    :before-close="handleClose"
  >
    <div class="dialog-content">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        label-position="top"
        class="party-form"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="form.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="所在机构" prop="orgId">
            <el-select v-model="form.orgId" filterable placeholder="请选择">
              <el-option
                v-for="item in organList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="账号有效期" prop="permanent">
          <el-radio-group v-model="form.permanent">
            <el-radio :label="true">永久有效</el-radio>
            <el-radio :label="false">设置有效期</el-radio>
          </el-radio-group>
          <div v-if="!form.permanent">
            <el-date-picker
              class="mt16"
              v-model="form.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="handleClose()">取 消</el-button>
      <el-button type="primary" @click="handleSubmit()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getOrgListWithOutPage } from "@/config/organ";
export default {
  data() {
    return {
      dialogVisible: false,
      organList: [],
      form: {
        userName: "",  
        orgId: "",
        permanent: true,
        date: "",
      },
      rules: {
        userName: [
          { required: true, message: "请填写管理员姓名", trigger: "change" },
        ],
        orgId: [
          { required: true, message: "请输入所在机构", trigger: "change" },
        ],
        permanent: [
          { required: true, message: "请选择账号有效期", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.getOrgList();
  },
  methods: {
    // 获取机构列表
    getOrgList() {
      getOrgListWithOutPage().then((res) => {
        this.organList = res;
      });
    },
    // 弹窗关闭
    handleClose() {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.handleReset();
        })
        .catch((_) => {});
    },
    handleReset() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.form = {
       userName: "",  
        orgId: "",
        permanent: true,
        date: "",
      };
    },
    // 提交
    handleSubmit() {
      // 校验用户输入值
      this.$refs.form.validate((valid) => {
        if (valid) {
          let user = {};
          if (!this.form.permanent && !this.form.date) {
            this.$message.error("请选择有效期");
            return false;
          }
          if (!this.form.permanent) {
            user.effectiveDate = this.form.date[0];
            user.exiredDate = this.form.date[1];
          }
          user.permanent = this.form.permanent;
          user.userName = this.form.userName;
          user.orgId = this.form.orgId;
           let requetsparams = this.$qs.stringify(user)
          this.$https(
            {
              method: "post",
              url: "tUser/statisticsAdd",
              authType: this.backToken,
            },
           requetsparams
          )
            .then((res) => {
              if (res.status == 200) {
                if (res.data.resultCode == 200) {
                  this.$message({
                    type: "success",
                    message: res.data.message,
                  });
                  this.handleReset();
                  this.$emit("success", true);
                } else {
                  this.$message.error(res.data.message);
                }
              } else {
                this.$message.error(res.data);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>

</style>