<template>
  <div class="detail_contain">
    <p class="now_page_title">
      权限管理 / 组织管理 /
      <span
        >组织{{ $route.path.indexOf("edit") !== -1 ? "编辑" : "新增" }}</span
      >
    </p>
    <div class="info_contain">
      <el-form ref="form" :rules="rules" :model="form" label-width="0px">
        <el-form-item prop="name">
          <p class="formname">组织机构名称：</p>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item prop="shortname">
          <p class="formname">组织机构简称：</p>
          <el-input v-model="form.shortname"></el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="edit_flag">
          <p class="formname">组织机构编码：</p>
          <el-input v-model="form.code" disabled></el-input>
        </el-form-item>

        <el-form-item prop="fileList">
          <p class="formname">组织机构图标：</p>
          <upload-file
            :multiple="false"
            :max="1"
            type="cropper"
            :readOnly="false"
            fit="fill"
            :list="imgList"
            @getNewList="getNewList"
            ref="servicepic"
            directory="manage"
          ></upload-file>
        </el-form-item>

        <el-form-item prop="desc">
          <p class="formname">说明：</p>
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>

        <el-form-item prop="person">
          <p class="formname">创建人：</p>
          <el-input v-model="form.person" disabled></el-input>
        </el-form-item>

        <el-form-item prop="date">
          <p class="formname">创建日期：</p>
          <el-input v-model="form.date" disabled></el-input>
        </el-form-item>

        <el-form-item class="apaas_button">
          <el-button type="primary" @click="onSubmit('form')" class="right"
            >保存</el-button
          >
          <el-button class="right" @click="backPage()">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import uploadFile from "@/components/general/upload_file";
import { getRole, formatDateTime_date } from "@/utils/common";
export default {
  props: {},
  components: {
    uploadFile,
  },
  data() {
    return {
      form: {
        name: "",
        shortname: "",
        code: "",
        desc: "",
        person: "",
        date: "",
        fileList: "",
      },
      now_user: 2, //0：普通用户，1：组织管理员，2：超级管理员
      edit_flag: 0, //0:新增,1:编辑:2详情
      imgList: [],
      area_arr: [
        {
          label: "应急领域",
          value: 0,
        },
        {
          label: "公安领域",
          value: 1,
        },
        {
          label: "交通领域",
          value: 2,
        },
        {
          label: "全领域",
          value: 3,
        },
      ],
      type_arr: [
        {
          label: "基础工具",
          value: 0,
        },
        {
          label: "通用应用",
          value: 1,
        },
        {
          label: "业务应用",
          value: 2,
        },
        {
          label: "其他",
          value: 3,
        },
      ],
      origin_arr: [],
      rules: {
        name: [
          { required: true, message: "请输入组织机构名称", trigger: "blur" },
          { max: 10, message: "长度小于10个字符", trigger: "blur" },
        ],
        shortname: [
          // { required: true, message: '请输入组织机构简称', trigger: 'blur' },
          { max: 10, message: "长度小于10个字符", trigger: "blur" },
        ],
        desc: [
          // { required: true, message: '请输入组织说明', trigger: 'blur' },
          { max: 500, message: "长度小于500个字符", trigger: "blur" },
        ],
        fileList: [
          //   {required: true, message: '请上传图片', trigger: 'change'}
        ],
      },
    };
  },
  watch: {},
  computed: {},
  created() {
    this.get_now_page();
  },
  mounted() {},
  methods: {
    get_now_page() {
      console.log(this.$route);
      if (this.$route.path.indexOf("add") !== -1) {
        this.edit_flag = 0;
        this.get_now_user();
      }
      if (this.$route.path.indexOf("edit") !== -1) {
        this.edit_flag = 1;
        this.get_user();
      }
      if (this.$route.path.indexOf("detail") !== -1) {
        this.edit_flag = 2;
        this.get_user();
      }
    },
    get_now_user() {
      this.$api.user.getNowUser().then(({ data }) => {
        if (data.success == 1) {
          this.form = {
            person: data.data.user_id,
            date: formatDateTime_date(new Date()),
          };
        } else {
          console.log(data.errMsg);
        }
      });
    },
    get_user() {
      this.$http
        .get(
          `/apaas/backmgt/department/detail?department_id=${this.$route.params.id}`
        )
        .then((response) => {
          console.log(response);
          if (!response.data.success) {
            this.$message.error(response.data.errMsg);
            return;
          }
          let data = response.data.data;
          this.form = {
            name: data.department_name,
            shortname: data.department_display,
            code: this.$route.params.id,
            desc: data.description,
            person: data.create_user,
            date: data.create_date,
            fileList: data.picture_path,
          };
          if(data.picture_path){
            this.imgList = data.picture_path.split(";");
          }else{
            this.imgList = []
          }
          console.log(this.imgList);
        })
        .catch((response) => {});
    },
    set_user() {
      var temp = {
        department_id: this.form.code,
        department_name: this.form.name,
        description: this.form.desc,
        department_display: this.form.shortname,
        picture_path: this.form.fileList,
      };
      this.$http
        .put("/apaas/backmgt/department", temp)
        .then((response) => {
          console.log(response);
          if (!response.data.success) {
            this.$message.error(response.data.errMsg);
            return;
          }
          this.$message.success("保存成功");
          this.$router.back(-1);
        })
        .catch((response) => {
          this.$message.error("保存失败");
        });
    },
    backPage() {
      this.$router.back(-1);
    },
    add_user() {
      var temp = {
        department_name: this.form.name,
        description: this.form.desc,
        department_display: this.form.shortname,
        picture_path: this.form.fileList,
      };
      this.$http
        .post("/apaas/backmgt/department", temp)
        .then((response) => {
          console.log(response);
          if (!response.data.success) {
            this.$message.error(response.data.errMsg);
            return;
          }
          this.$message.success("保存成功");
          this.$router.back(-1);
        })
        .catch((response) => {
          this.$message.error("保存失败");
        });
    },
    getOriginArr() {
      this.$http
        .get("/apaas/backmgt/department/list")
        .then((response) => {
          if (!response.data.success) {
            this.$message.error(response.data.errMsg);
            return;
          }
          let data = response.data.data;
          this.origin_arr = data;
        })
        .catch(function(response) {});
    },
    removepic() {
      this.$refs.servicepic.remove_pic();
    },
    onSubmit(formName) {
      console.log("submit!");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.edit_flag == 0) {
            this.add_user();
          }
          if (this.edit_flag == 1) {
            this.set_user();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getNewList(val) {
      this.form.fileList = val[0].url;
      console.log(this.form.fileList);
    },
  },
};
</script>

<style>
.info_contain .el-input__inner {
  background-color: #f7f8f9;
  width: 1022px;
}
.info_contain .el-textarea__inner {
  background-color: #f7f8f9;
  width: 1022px;
}
.limitsd .timeslect .el-input__inner {
  width: 150px;
  border-radius: 0 8px 8px 0;
  background-color: rgba(15, 38, 131, 1);
  color: rgba(248, 249, 253, 1);
  border: 0;
}
.info_contain .el-input.is-disabled .el-input__inner {
  background-color: #e3e4e6;
}
</style>

<style scoped>
.detail_contain {
  width: 100%;
  padding: 0 20px;
  height: calc(100% - 55px);
}
.info_contain {
  padding: 25px 20px;
  background-color: #fff;
  width: 100%;
  box-shadow: 0px 3px 6px 0px #f4f7fc;
  border-radius: 12px;
  min-height: calc(100% - 20px);
  margin-bottom: 20px;
  position: relative;
}
.now_page_title {
  margin: 15px 0;
  color: #898d9e;
}
.now_page_title span {
  color: #242c43;
}
.formname {
  color: #58617a;
}
.right {
  float: right;
  margin-left: 20px;
}
.removepic {
  display: inline-block;
  margin-top: -40px;
  float: left;
  margin-left: 180px;
  width: 76px;
  height: 32px;
  background-color: #495feb;
  border-radius: 8px;
  line-height: 32px;
  text-align: center;
  color: #e6ebfe;
  cursor: pointer;
}
.edit_change {
  width: 100%;
  border-bottom: 2px solid #f4f7fc;
  overflow: hidden;
}
.edit_change_box {
  width: 163px;
  height: 48px;
  line-height: 48px;
  background-color: #e3e5ef;
  border-radius: 8px;
  color: #8890a7;
  text-align: center;
  float: left;
  margin-right: 20px;
  font-weight: 600;
  margin-bottom: 25px;
  font-size: 16px;
  cursor: pointer;
}
.edit_change_box img {
  vertical-align: -3px;
  margin-right: 5px;
}
.safe_title {
  width: 100%;
  height: 65px;
  line-height: 65px;
  border-bottom: 2px solid #f4f7fc;
}
.safe_select {
  float: left;
  width: auto;
  margin-right: 40px;
  height: 65px;
  cursor: pointer;
}
.safe_box_select {
  padding: 30px 10px 10px 10px;
  width: 100%;
  height: 100%;
}
.openbgc {
  width: 56px;
  height: 23px;
  background-image: url("~@/assets/imgs/btn_on_hov.png");
  background-size: contain;
  cursor: pointer;
}
.formtitle {
  color: #58617a;
  margin: 15px 0;
}

.savebtn {
  position: absolute;
  right: 20px;
  bottom: 20px;
  width: 278px;
  overflow: hidden;
}
.btnsty {
  width: 124px;
  height: 44px;
  line-height: 44px;
  background-color: rgba(195, 202, 248, 0.5);
  border-radius: 8px;
  color: rgba(15, 38, 131, 1);
  text-align: center;
  float: left;
  margin-right: 30px;
  cursor: pointer;
}
.savebtn .btnsty:nth-last-of-type(1) {
  margin-right: 0px;
  background-color: rgba(15, 38, 131, 1);
  color: rgba(248, 249, 253, 1);
}
.limitsd {
  position: relative;
  width: 1022px;
}
.limitsd .timeslect {
  position: absolute;
  right: 0;
}
</style>
