<template>
  <el-dialog v-dialogDrag :title="openTitle" :visible.sync="visible" @close="handleClose" class="common-dialog" :close-on-click-modal="false">
    <el-form :model="formData" :rules="rules" ref="dialogForm" label-width="80px" size="small" class="demo-ruleForm" label-position="right">
      <el-row :gutter="50">
        <el-col :span="12">
          <!-- <el-form-item label="部门名称" prop="deptName"><el-input v-model="formData.deptName" placeholder="默认登录人部门" disabled="disabled"></el-input></el-form-item> -->
          <el-form-item label="部门名称" prop="deptName">
          <el-input
          size="small"
          v-model="formData.deptName"
          @focus="treeShow"
          clearable
          placeholder="请选择部门名称">
          </el-input>
          </el-form-item>

          <el-form-item label="模块" prop="moduleId"><common-select :dictType="'CRM_Module_Code'" v-model="formData.moduleId"></common-select></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="reportType">
            <!-- :ismult="true" -->
            <common-select :dictType="'Crm_Report_Type'" v-model="formData.reportType"></common-select>
          </el-form-item>
          <el-form-item label="应交人员" prop="informerList" ref="infoList">
            <aws-org
              v-model="personnelData"
              :isDialog="true"
              :editable="openType == 'view' ? false : true"
              @change="personnelDataChange"
              ref="awsorgRef"
              @submit="coordination"
              :sid="sid"
              :users="personnelData"
            ></aws-org>
          </el-form-item>
        </el-col>

        <org-tree
        v-if="showTree"
        @close="orgTreeClose"
        :is-one-select="isOneSelect"
        :init-tree-data="initTreeData" />
      </el-row>
      <el-form-item style="margin-bottom: 0;text-align: center" label-width="0px">
        <el-button @click="handleClose" type="info">取消</el-button>
        <el-button @click="doSave" type="warning">保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import commonSelect from '../../../../components/common-select.vue';
import commonSelectList from '../../../../common/commonSelectList.js';
import OrgTree from '../../../../components/org-tree.vue';
export default {
  name: 'add-update',
  mounted: function() {},
  components: {
    commonSelect,
    OrgTree
  },
  props: {
    submitInfo: {
      type: Object
    },
    openTitle: {
      type: String
    }
  },
  computed: {
    sid() {
      return this.$common.getUrlParams('sid');
    }
  },
  data: function() {
    return {
      showTree:false,
      isOneSelect: true, //树形部门为单选
      initTreeData: [], //初始化部门树形数据
      //默认关闭
      visible: true,
      disabled: false,
      isEdit: true, //任务来源是否可编辑
      //表单属性
      options: [
        {
          value: 1,
          label: '是'
        },
        {
          value: 0,
          label: '否'
        }
      ],
      personnelData: [],
      openType: '',
      formData: {
        deptName: '',
        deptId: '',
        moduleId: '',
        reportType: '',
        deptSuperiorId:'',
        deptSuperiorName:'',
        id:'',
        informerList: []
      },

      // 表单具体校验
      rules: {
        deptName: [
          {
            required: true,
            message: '请选择部门',
            trigger: 'change'
          }
        ],
        moduleId: [
          {
            required: true,
            message: '请选择模块名称',
            trigger: 'change'
          }
        ],
        reportType: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }
        ],
        informerList: [
          {
            required: true,
            message: '请选择应交报告人员',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  created() {
    this.getSubmitInfo();
  },
  methods: {
    //打开部门树形选择
    treeShow() {
      this.showTree = true;
    },
    //关闭部门树形选择
    orgTreeClose(data) {
      console.log("data===>",data)
      //树形为单选，只有一个对象
      this.showTree = false;
      if (data !== null && data.length > 0) {
        this.formData.deptId = data[0].id;
        this.formData.deptName = data[0].label;
        this.formData.deptSuperiorId = data[0].deptSuperiorId;
        this.formData.deptSuperiorName = data[0].deptSuperiorName;
        this.initTreeData = data;
      } else {
        this.formData.deptId = '';
        this.formData.deptName = '';
        this.formData.deptSuperiorId = '';
        this.formData.deptSuperiorName = '';
        this.initTreeData = [];
      }
    },
    getSubmitInfo() {
      for(let key in this.formData){
        if(this.submitInfo[key]){
          this.formData[key]=this.submitInfo[key]
        }
      }
      console.log("this.formData",this.formData)
      this.setData(this.formData);
    },
    // 保存按钮
    doSave: function() {
      var _this = this;
      this.$refs['dialogForm'].validate(valid => {
        if (valid) {
          _this.addOrEdit(this.formData);
        }
      });
    },
    addOrEdit: function(param) {
      let msg = '成功!';
      if (param.id != null && param.id != '') {
        msg = '编辑成功!';
      } else {
        msg = '添加成功!';
      }
      this.$ajax.post({
        domain: 'crmUrl',
        url: 'reportSubmitTending.saveOrUpdateTending',
        data: param,
        success: function(res) {
          console.log(res);
          if (res.errorCode === 200) {
            this.$message.success(msg);
            this.$emit('callback');
          } else {
            this.$message.error(res.msg);
          }
        }.bind(this),
        error: function(res) {
          this.$message({
            type: 'error',
            message: res.body.msg || res.msg
          });
        }.bind(this)
      });
    },

    //协同人员修改
    personnelDataChange() {
      if (this.personnelData && this.formData.informerList) {
        if (this.formData.informerList.length > 0) {
          this.formData.informerList = this.formData.informerList.filter(item => {
            for (let row of this.personnelData) {
              if (item.userId === row.uid) {
                return true;
              }
            }
          });
        }
      }
    },
    //获取 AWS 提交人员信息
    coordination(data) {
      var that = this;
      this.formData.informerList = [];
      var deleteIndex = -1;
      for (var i = 0; i < this.personnelData.length; i++) {
        this.formData.informerList.push({
          userId: this.personnelData[i].uid,
          userName: this.personnelData[i].name,
          email: this.personnelData[i].email,
          informerType: '5'
        });
        if (deleteIndex > -1) {
          this.$refs.awsorgRef.closeItemTag(deleteIndex);
          this.personnelData.splice(deleteIndex, 1);
          this.$refs.awsorgRef.reset(this.personnelData);
        }
      }
    },
    //获取 AWS 提交人员信息
    setData(data) {
      console.log('data info==>', data);
      if (data != null && data.informerList!=null&&data.informerList.length > 0) {
        let personnelData = [];
        for (var i = 0; i < data.informerList.length; i++) {
          personnelData.push({
            uid: data.informerList[i].userId,
            name: data.informerList[i].userName,
            email: data.informerList[i].email,
            id: data.informerList[i].userId
          });
        }
        this.personnelData = personnelData;
      }
    },
    //关闭弹窗
    handleClose() {
      this.$emit('callback','close');
    },
    close: function() {
      this.clear();
    },

    // 当前数据clear
    clear: function() {
      this.formData = {};
    }
  }
};
</script>

<style type="text/css"></style>
