<template>
  <div>
    <p class="edit_page_title">{{team_list_type==='add'?'添加部门':'编辑部门'}}</p>
    <Form ref="change_team" :model="change_team" :rules="team_rule" :label-width="90">
      <FormItem label="部门名称" prop="name">
        <Input type="text" v-model="change_team.name" class="wid_275" placeholder="2-25个字符"></Input>
      </FormItem>
      <FormItem label="父级部门" prop="pid">
        <Select placeholder="作为一级部门" v-model="change_team.pid" class="wid_275" filterable >
          <Option  v-for="(item,index) in fatherTeamList" :value="item.id" :key="index">{{item.name}}</Option>
        </Select>
      </FormItem>
      <FormItem label="状态" prop="status">
        <RadioGroup v-model="change_team.status" class="wid_275">
          <Radio label="1">正常</Radio>
          <Radio label="0">关闭</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem class="form_btn">
        <Button type="primary" @click="editteam('change_team')">确认</Button>
        <Button class="return_btn mar_L_15" @click="return_last">返回</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import { error } from 'util';
export default {
  name: "edit_team",
  props: {
    team_list_type: String,
    studioname: String,
    team_id: Number,
    studio_id: Number,
  },
  data() {
    const confirmtname = (rule, value, cb) => {
      let reg = /^[A-Za-z0-9\u4e00-\u9fa5]+$/;
      if (!reg.test(value)) {
        cb(new Error("军团名称为2-25个字符"));
      } else if (value.length > 25 || value.length < 2) {
        cb(new Error("军团名称为2-25个字符"));
      } else {
        cb();
      }
    };
    return {
      teamlisttext:'作为一级部门',
      iseditteam:true,
      pageTitle: "",
      change_team: {
        name:'',
        pid:'',
        status: "1"
      },
      fatherTeamList: [],
      team_rule: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { required: true, validator: confirmtname, trigger: "blur" }
        ],
        // pid:[
        //    {type:'number', required: true, message: "请选择父级部门", trigger: "change" }
        // ]
      }
    };
  },
  methods: {
    // 获取下拉框列表
     getTeamList(){
      this.$api.getTeamList().then(data => {
        if(data.code===200){
          this.fatherTeamList = data.data
          if(this.team_list_type==='edit'){
            this.fatherTeamList = this.fatherTeamList.filter(item => ![this.change_team.id].includes(item.id))
          }
        }else{
          this.$Message.info(data.msg)
          return
        }
      })
    },
    // 获取部门详情
    getTeamDetail(){
      this.$api.getTeamListDetail({id:this.team_id}).then(data => {
        if(data){
          this.change_team = data
          this.change_team.status=data.status.toString()
        }
      })
    },
    // 添加部门  
    editteam(fromName) {
      this.$refs[fromName].validate(valid => {
        if (valid) {
          let api=this.team_list_type==='add'?'addTeamList':'editTeamList'
          this.$api[api](this.change_team).then(data =>{
            if(data.code===200){
              this.$Message.info(data.msg)
              this.$emit('return_last')
            }else{
              this.$Message.info(data.msg)
            }
          }).catch(error=>{ })
        }
      });
    },
    return_last(){
      this.$emit('return_last')
    }
  },
  created(){
    this.getTeamList()
    this.$nextTick(()=>{
      if(this.team_list_type==='edit'){
        this.getTeamDetail()
      }
    })
    
  }
};
</script>

