<template>
  <ele-modal
    :centered="true"
    :visible.sync="auditDialog"
    title="提交"
    :close-on-click-modal="false"
    width="25%"
    append-to-body
    @close="handleClose"
  >
    <el-form ref="form" :model="formData" label-width="80px" style="height: 200px;">
      <el-form-item
        label="部门"
        prop="planExecuteGroupId"
        :rules="{
          required: true,
          message: '请选择部门',
          trigger: 'change'
        }"
      >
        <deptSelect
          v-model="formData.planExecuteGroupId"
          @changeGroup="searchDeptNodeClick"
        />
      </el-form-item>
      <el-form-item
        label="执行人"
        prop="planExecuteUserId"
        :rules="{
          required: true,
          message: '请选择执行人',
          trigger: 'change'
        }"
      >
        <el-select
          v-model="formData.planExecuteUserId"
          size="small"
          style="width: 100%"
          filterable
          @change="changeUser"
        >
          <el-option
            v-for="item in executorList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer">
      <el-button type="primary" @click="handleSubmit" :loading="loading"
        >确认</el-button
      >
      <el-button @click="handleClose" :loading="loading">取消</el-button>
    </div>
  </ele-modal>
</template>
<script>
  import deptSelect from '@/components/CommomSelect/dept-select.vue';
  import { getUserPage } from '@/api/system/organization';
  import { getCurrentUser } from '@/utils/token-util';
  import {
    demandSubmit,
    getSalesDemandById
  } from '@/api/salesServiceManagement/index';

  let submitData = {};
  export default {
    components: { deptSelect },
    computed: {
      // 部门下拉
      loginChangeGroupVOList() {
        return this.$store.state.user?.info?.loginChangeGroupVOList;
      }
    },
    data() {
      return {
        auditDialog: false,
        formData: {
          planExecuteUserId: '',
          planExecuteGroupId: ''
        },
        loading: false,
        executorList: [],
        submitSource: 1 // 提交/派单来源(1:新增；2：修改; 列表: 3) none( 不需要这个字段 )
      };
    },
    methods: {
      handleClose() {
        this.auditDialog = false;
        this.formData = {
          planExecuteUserId: ''
        };
        this.$refs.form.resetFields();
      },
      async open(data, type, source) {
        this.submitSource = type == 'add' ? 1 : type == 'edit' ? 2 : 3;
        this.auditDialog = true;
        // if (source) {
        //   const res = await getSalesDemandById(data.id);
        //   submitData = res;
        // }else{
        //    submitData = data;
        // }
        submitData = data;
        let currentUser = getCurrentUser();
        if (
          this.loginChangeGroupVOList &&
          this.loginChangeGroupVOList.length > 0
        ) {
          let obj = this.loginChangeGroupVOList.find(
            (el) => el.groupId == currentUser.currentGroupId
          );
          this.formData.planExecuteGroupId = currentUser.currentGroupId || '';
          this.searchDeptNodeClick(currentUser.currentGroupId, {
            name: obj.groupName
          });
        }
      },
      //选择部门(搜索)
      searchDeptNodeClick(info, data) {
        if (info) {
          // 根据部门获取人员
          this.formData.planExecuteGroupName = data.name;
          const params = { groupId: info };
          this.getUserList(params);
        } else {
          this.formData.planExecuteGroupId = null;
        }
      },
      // 获取审核人列表、巡点检人员
      async getUserList(params) {
        try {
          let data = { pageNum: 1, size: -1 };
          // 如果传了参数就是获取巡点检人员数据
          if (params) {
            data = Object.assign(data, params);
          }
          const res = await getUserPage(data);
          if (params) {
            this.executorList = res.list;
          }
        } catch (error) {}
      },
      //   选择执行人
      changeUser(val) {
        this.executorList.map((item) => {
          if (item.id == val) {
            this.formData.planExecuteUserName = item.name;
          }
        });
      },
      handleSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let data = {
              ...this.formData,
              submitSource: this.submitSource
            };
            if (data.submitSource == 3) {
              data.id = submitData.id;
            } else {
              data = { ...submitData, ...data };
            }
            // if (this.submitSource == 'none') {
            //   delete data.submitSource;
            // }
            this.loading = true;
            demandSubmit(data)
              .then((res) => {
                if (res) {
                  this.loading = false;
                  this.$message.success('操作成功');
                  this.handleClose();
                  this.$emit('savExit');
                }
              })
              .catch((err) => {
                this.loading = false;
              });
          } else {
            return false;
          }
        });
      }
    }
  };
</script>
