 <template>
  <div class="pageBody">
    <div class="topPart">
      <div class="title">
        <div class="tltleLeft">编辑职务</div>
        <!-- <div class="isUsed">
          是否启用
          <el-radio
            v-model="FormData.isUsed"
            v-if="FormData.isUsed == 1 || form.isUsed == 3"
            :label="1"
            >是</el-radio
          >
          <el-radio v-model="FormData.isUsed" v-if="FormData.isUsed == 2" :label="2"
            >是</el-radio
          >
          <el-radio v-model="FormData.isUsed" :label="3">否</el-radio>
        </div> -->
        <div v-is="['job_btn_edit']">
          <!-- <button type="submmit" class="delBtn" @click="delate">删除</button> -->
          <button type="submmit" style="cursor: pointer;" class="saveBtn" @click="saveJob('form')">
            保存
          </button>
        </div>
      </div>
      <div class="titleContent">
        <el-form ref="form" :model="FormData" :rules="rules" label-width="80px">
          <el-row :gutter="40">
            <el-col :span="5">
              <el-form-item prop="JobName" label="职务名称">
                <el-input v-model.trim="FormData.JobName" :placeholder="`请输入${accountGroupType}名称`" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item prop="JobDesc" label="描述">
                <el-input v-model.trim="FormData.JobDesc" placeholder="描述内容" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="content">
      <el-row :gutter="25">
        <el-col :span="24">
          <div class="part">
            <Organization title="上级组织" :active="0" :data="dataObj" :defaultProps="defaultProps" nodekey="OrganId" @getTreeData="getOrganTreeData" :checkedKeys="checkedIdList" />
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import Organization from "@/components/organization";
import treeData from "./minixs/treeData";
import { GetJobDetails, EditJob } from "@/api/jobApi";
import { lengthValidator } from '@/utils/validate'
export default {
  data () {
    return {
      JobId: null, //当前岗位ID
      OrganId: null,
      FormData: {
        JobName: "",
        JobDesc: "",
      },
      rules: {
        JobName: [
          { required: true, message: `请输入${this.accountGroupType}名称`, trigger: "blur" },
          { validator: lengthValidator, max: 128, message: "长度在 1 到 128 个字符", trigger: ['change', 'blur'] },
        ],
        JobDesc: [{ validator: lengthValidator, max: 256, message: '长度在 1 到 256 个字符', trigger: ['change', 'blur'] }]
      },
      defaultProps: {
        children: "children",
        label: "OrganName",
      },
      dataObj: [], //上级组织树
      checkedIdList: [],
      title: "",
    };
  },
  mixins: [treeData],
  created () {
    const { JobId } = this.$route.query;
    this.getJobDetails(JobId);
  },
  components: {
    Organization,
  },
  watch: {
    dataList: {
      handler (val) {
        this.decompose([val], this.OrganId)
      },
      deep: true
    }
  },
  methods: {
    //点击获取组织
    getOrganTreeData (arr) {
      const { OrganId } = arr[0]
      this.OrganId = OrganId
    },
    //获取岗位详情
    async getJobDetails (id) {
      try {
        let data = {
          JobId: id,
        };
        const result = await GetJobDetails(data)
        if (result.code === 0) {
          const obj = result.returnData
          const { JobDesc, JobName, JobId, OrganId } = obj
          this.FormData.JobName = JobName
          this.FormData.JobDesc = JobDesc
          this.JobId = JobId
          this.OrganId = OrganId
          this.checkedIdList = [OrganId]
        } else {
          this.$message.error(result.message)
        }
      } catch (error) {
        this.$message.error(error)
      }
    },
    //保存
    async saveJob (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async saveData () {
      try {
        const data = {
          JobName: this.FormData.JobName,
          JobDesc: this.FormData.JobDesc,
          OrganId: this.OrganId,
          JobId: this.JobId
        }
        const result = await EditJob(data)
        if (result.code == 0) {
          this.$message.success(result.message)
          this.$store.dispatch('tagsView/delView', this.$route)
          this.$router.push("/jobs")
        } else {
          this.$message.error(result.message)
        }
      } catch (error) {
        this.$message.error(error)
      }
    },
    //获取指定数据
    decompose (data, id) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].OrganId == id) {
          delete data[i].children;
          this.dataObj = [data[i]];
        } else if (data[i].children && data[i].children.length > 0) {
          this.decompose(data[i].children, id);
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.pageBody {
  width: 100%;
  height: calc(100vh - 178px);
  padding: 0 64px;
  padding-top: 32px;
  .topPart {
    box-sizing: border-box;
    width: 100%;
    height: 184px;
    padding: 32px;
    background: #ffffff;
    box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
    border-radius: 16px;
    .title {
      display: flex;
      justify-content: space-between;
      flex-direction: row;
      position: relative;
      .isUsed {
        position: absolute;
        left: 194px;
        top: 6px;
      }
      .tltleLeft {
        font-size: 24px;
        font-weight: bold;
        color: #303133;
      }
      .saveBtn {
        width: 80px;
        height: 40px;
        background: linear-gradient(0deg, #777dba, #6983be);
        border-radius: 6px;
        color: #fff;
        border: none;
      }
      .delBtn {
        width: 80px;
        height: 40px;
        background: #f56c6c;
        border-radius: 6px;
        color: #fff;
        border: none;
        margin-right: 24px;
      }
    }
    .titleContent {
      width: 100%;
      padding-top: 35px;
      box-sizing: border-box;
      .resetPass {
        background: #6e81bc;
        color: #fff;
        margin-top: 4px;
        margin-left: -25px;
      }
    }
  }
  .content {
    width: 100%;
    height: calc(100% - 184px);
    padding-top: 24px;
    box-sizing: border-box;
    .part {
      width: 100%;
      height: 720px;
      background: #ffffff;
      box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
      border-radius: 16px;
    }
  }
}
</style>
