<template>
  <el-dialog title="批量修改" :close-on-click-modal="false" append-to-body :visible.sync="visible" width="600px" @close="closeDialog">
    <el-form ref="dialogForm" :model="dialogForm" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="跟进人员" prop="followUsers">
            <el-select v-model="dialogForm.followUsers" multiple placeholder="请选择" clearable>
              <el-option v-for="dict in options.userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button plain @click="(visible = false)">取消</el-button>
      <el-button v-jclick type="primary" :disabled="!canSubmit" @click="dialogFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { batchUpdate } from '@/api/zs/clue'; export default {
  name: 'BatchUpdateDialog',
  props: {
    options: {
      type: Object,
      default: () => ({
        userOptions: []
      })
    }
  },
  data() {
    return {
      visible: false,
      canSubmit: true,
      dialogForm: {},
      rules: {
        followUsers: {
          required: true,
          message: '跟进人员不能为空',
          trigger: 'blur'
        }
      }
    };
  },
  methods: {
    init(info = undefined) {
      this.visible = true;
      this.$nextTick(() => {
        this.resetDialogForm();
        this.$refs['dialogForm'].resetFields();
        if (info) {
          this.dialogForm = this.deepClone(info);
        }
      });
    },
    resetDialogForm() {
      this.dialogForm = {
        followUsers: [],
        clueIds: this.clueIds
      };
    },
    closeDialog() {
      this.$emit('update:dialog.batchUpdateVisible', false);
    },
    // 表单提交
    dialogFormSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          // 校验完成，调接口
          batchUpdate(this.dialogForm)
            .then((resp) => {
              this.canSubmit = true;
              if (resp.code == 200) {
                this.$message.success('修改成功');
                this.$emit('refreshDataList');
                this.visible = false;
              }
            })
            .catch(() => {
              this.canSubmit = true;
            });
        }
      });
    }
  }
};
</script>

