<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="areaId">
            <el-cascader
              v-model="form.areaId"
              change-on-select
              :props="cascaderProps"
              :options="areaOptions"
              placeholder="请选择区域"
              clearable
            >
            </el-cascader>
          </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";
import { getAreas } from "@/config/area";
export default {
  data() {
    return {
      dialogVisible: false,
      organList: [],
      areaOptions: [],
      form: {
        userName: "",
        areaId: [],
        permanent: true,
        date: "",
        type: 4, //1.用户账号 2.平台单位单位管理员账号 3.机顶盒账号 4.运维账号
      },
       cascaderProps: {
        label: "name",
        value: "code",
        checkStrictly: true,
      },
      rules: {
        userName: [
          { required: true, message: "请运维人员姓名", trigger: "change" },
        ],
        areaId: [
          {
            type: "array",
            required: true,
            message: "请选择运维区域",
            trigger: "change",
          },
        ],
        permanent: [
          { required: true, message: "请选择账号有效期", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.getOrgList();
    this.getAreas();
  },
  methods: {
    // 获取机构列表
    getOrgList() {
      getOrgListWithOutPage().then((res) => {
        this.organList = res;
      });
    },
    // 获取区域数据
    getAreas() {
      getAreas().then((res) => {
        this.areaOptions = res;
      });
    },

    // 弹窗关闭
    handleClose() {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.handleReset();
        })
        .catch((_) => {});
    },
    handleReset() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.form = {
        userName: "",
        areaId: [],
        permanent: true,
        date: "",
        type: 4, //1.用户账号 2.平台单位单位管理员账号 3.机顶盒账号 4.运维账号
      }
    },
    // 提交
    handleSubmit() {
      // 校验用户输入值
      let _this = this
      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.userName = this.form.userName;
          user.areaId = [...this.form.areaId].pop();
          user.permanent = this.form.permanent;
          user.type = this.form.type;
          let requetsparams = this.$qs.stringify(user)
          this.$https(
            {
              method: "post",
              url: "tUser/operationAdd",
              authType: this.backToken,
            },
            requetsparams
          )
            .then((res) => {
               this.handleReset();
              if (res.status == 200) {
                if (res.data.resultCode == 200) {
                  this.$message({
                    type: "success",
                    message: res.data.message,
                  });
                  this.$emit("success", true);
                } else {
                  this.$message.error(res.data.message || res.data.msg);
                }
              } else {
                this.$message.error(res.data);
              }
            })
            .catch((err) => {
              this.$message.error(err.message || err.msg);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>

</style>