<template>
  <div class="dialog-moveto">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div class="form">
        <el-form
          label-width="120px"
          :rules="rules"
          :model="addForm"
          ref="form"
          class="ele-form-search"
        >
          <el-form-item
            v-if="componentsType == 'move'"
            label="设备分类"
            label-width="100px"
            prop="id"
          >
            <el-select v-model="addForm.id" placeholder="请选择">
              <el-option
                v-for="item in list"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="componentsType == 'person'"
            label="片区负责人部门"
            prop="repairGroupId"
          >
            <DeptSelect v-model="addForm.repairGroupId" @input="getwhbm" />
          </el-form-item>
          <el-form-item v-if="componentsType == 'person'" label="片区负责人">
            <el-select v-model="addForm.repairUserId" placeholder="请选择">
              <el-option
                v-for="item in personList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="loading"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {
    changeSubstanceCateId,
    updateRepairUserAndGroup
  } from '@/api/ledgerAssets';
  import { getTreeByPid } from '@/api/classifyManage';
  import DeptSelect from '@/components/CommomSelect/dept-select.vue';
  import { getUserPage } from '@/api/system/organization';
  export default {
    components: { DeptSelect },
    data() {
      return {
        componentsType: '',
        dialogVisible: false,
        addForm: {},
        loading: false,
        checkoutArr: [],
        rules: {
          id: [{ required: true, message: '请选择设备分类', trigger: 'blur' }],
          repairGroupId: [
            { required: true, message: '请选择片区负责人部门', trigger: 'blur' }
          ]
        },
        list: [],
        title: '',
        personList: []
      };
    },
    methods: {
      async getwhbm() {
        if (!this.addForm.repairGroupId) return;
        let data = await getUserPage({
          pageNum: 1,
          size: 9999,
          groupId: this.addForm.repairGroupId
        });
        this.personList = data.list;
      },
      submit() {
        this.$refs.form.validate(async (flag) => {
          if (flag) {
            try {
              this.loading = true;
              if (this.componentsType == 'move') {
                const paramsArr = this.checkoutArr.map((item) => {
                  return { categoryLevelId: this.addForm.id, id: item.id };
                });
                console.log(paramsArr);
                await changeSubstanceCateId(paramsArr);
              } else {
                const listId = this.checkoutArr.map((item) => {
                  return item.id;
                });
                const newObj = {
                  ...this.addForm,
                  id: listId
                };
                console.log(newObj);
                await updateRepairUserAndGroup(newObj);
              }
              this.$message.success('成功！');
              this.dialogVisible = false;
              this.$emit('success', true);
            } finally {
              this.loading = false;
            }
          } else {
            return false;
          }
        });
      },

      open(arr, current, type) {
        this.addForm = {};
        this.dialogVisible = true;
        this.componentsType = type;
        this.checkoutArr = arr;
        if (type === 'move') {
          this.getTreeData(current);
          this.title = '移动到';
        } else {
          this.title = '批量设置片区负责人';
        }
      },
      async getTreeData(current) {
        const res = await getTreeByPid(4);
        if (res.code == 0) {
          this.list = [];
          console.log(current);
          console.log('==res.data', res.data);
          res.data.forEach((e) => {
            if (e.id == current.parentId) {
              e.children.forEach((item) => {
                if (item.id == current.preParentId) {
                  this.list = item.children;
                }
              });
            }
          });
        }
      },
      handleClose() {
        this.dialogVisible = false;
      }
    }
  };
</script>
