<template>
  <el-dialog
    :visible.sync="transferVisible"
    width="35%"
    @close="close"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="执行部门:" prop="groupId">
        <deptSelect v-model="form.groupId" @changeGroup="searchDeptNodeClick" />
      </el-form-item>

      <el-form-item label="执行人员:" prop="qualityId">
        <el-select
          v-model="form.qualityId"
          @change="changeExecutor"
          filterable
          style="width: 100%"
        >
          <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>

    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消 </el-button>
      <el-button type="primary" @click="consfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import deptSelect from '@/components/CommomSelect/dept-select.vue';
  import { transferQualityWork } from '@/api/produce/workPlan.js';
  import { getUserPage } from '@/api/system/organization';

  export default {
    name: 'Transfer',
    props: {
      transferVisible: {
        type: Boolean,
        default: false
      },
      id: {
        type: String,
        default: ''
      }
    },
    components: {
      deptSelect
    },
    data() {
      return {
        form: {
          id: '',
          groupId: '',
          groupName: '',
          qualityId: '',
          qualityName: ''
        },
        rules: {
          groupId: [
            { required: true, message: '请选择执行部门', trigger: 'change' }
          ],
          qualityId: [
            { required: true, message: '请选择执行人员:', trigger: 'change' }
          ]
        },
        executorList: []
      };
    },
    methods: {
      searchDeptNodeClick(info, row) {
        console.log(info, row);

        if (info) {
          const params = { groupId: info };
          this.form.groupId = info;
          this.form.groupName = row.name;
          this.getUserList(params);
        } else {
          this.form.qualityId = null;
          this.form.qualityName = null;
          this.executorList = [];
        }
      },
      // 获取审核人列表、巡点检人员
      async getUserList(params) {
        try {
          let data = { pageNum: 1, size: -1 };
          // 如果传了参数就是获取巡点检人员数据
          if (params) {
            data = Object.assign(data, params);
          }
          const res = await getUserPage(data);
          this.executorList = res.list;
          this.form.qualityId = null;
          this.form.qualityName = null;
        } catch (error) {}
      },
      changeExecutor(val) {
        console.log(val);

        if (val) {
          this.form.qualityId = val;
          this.form.qualityName = this.executorList.find(
            (item) => item.id === val
          ).name;
        }
      },
      consfirm() {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            await transferQualityWork(this.form);
            this.$emit('success', this.form.id);
          }
        });
        console.log(this.form);
      },
      close() {
        this.$emit('update:transferVisible', false);
      }
    },
    created() {
      this.form.id = this.id;
    }
  };
</script>

<style lang="scss" scoped></style>
