<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="上级科室" prop="parentId" v-if="form.parentId !== 0">
        <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级科室" />
      </el-form-item>
      <el-form-item label="科室名称" prop="deptName"><el-input v-model="form.deptName" placeholder="请输入科室名称" /></el-form-item>
      <div v-if="form.parentId != 0 && form.ancestors.length == 3">
        <el-form-item label="科室介绍" prop="brief">
          <editor v-model="form.brief" placeholder="请输入"></editor>
        </el-form-item>
      </div>
      <el-row>
        <el-col :span="10">
          <el-form-item label="显示排序" prop="orderNum"><el-input-number v-model="form.orderNum" controls-position="right" :min="0" /></el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="科室状态">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from '@/api/system/dept';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
export default {
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      form: { ancestors: [], brief: '', status: '0' },
      deptOptions: [],
      rules: {
        parentId: [{ required: true, message: '上级部门不能为空', trigger: 'blur' }],
        deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
        orderNum: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }]
      }
    };
  },
  mounted() {
    if (this.param.id) {
      getDept(this.param.id).then((response) => {
        this.form = response.data;
        this.form.brief=this.form.brief||'';
        this.form.ancestors = this.form.ancestors.split(',') || [];
        listDeptExcludeChild(this.param.id).then((response) => {
          this.deptOptions = this.handleTree(response.data, 'deptId');
          if (this.deptOptions.length == 0) {
            const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
            this.deptOptions.push(noResultsOptions);
          }
        });
      });
    } else {
      listDept().then((response) => {
        this.deptOptions = this.handleTree(response.data, 'deptId');
      });
      this.form.parentId = this.param.parentId;
      this.form.ancestors = this.param.ancestors;
      this.form.ancestors.push(1);
    }
  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.ancestors = data.ancestors.toString();
          if (this.form.deptId != undefined) {
            updateDept(data).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            addDept(data).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
