<template>
  <div class="inputs ht-component">
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      class="dialog-selector__wrapper"
      top="6vh"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :append-to-body="appendToBody"
      :destroy-on-close="destroyOnClose"
    >
      <el-container>
        <el-main>
          <selector
            ref="selector"
            @load="load"
            :data="data"
            :pagination="pagination"
            :table-columns="tableColumns"
            :select-label="selectLabel"
            :single="single"
            :quick-search-props="quickSearchProps"
            :search-placeholder="searchPlaceholder"
            v-model="selectors"
          />
        </el-main>
      </el-container>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleDialogSure">确 定</el-button>
        <el-button size="small" @click="handleDialogCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import utils from "@/utils.js";
import Selector from "@/components/common/Selector.vue";
import { setTimeout } from "timers";

export default {
  name: "ht-dialog",
  props: {
    value: String,
    single: {
      type: Boolean,
      default: false
    },
    data: {
      type: Array,
      default: () => {
        return [];
      }
    },
    tableColumns: {
      type: Array,
      default: () => {
        return [];
      }
    },
    pagination: {
      type: Object,
      default: () => {
        return {
          page: 1,
          pageSize: 50,
          total: 0
        };
      }
    },
    selectLabel: {
      type: String,
      default: "name"
    },
    searchPlaceholder: {
      type: String,
      default: "请输入查询内容"
    },
    dialogTitle: {
      type: String,
      default: "对话框"
    },
    appendToBody:{
      type: Boolean,
      default: false
    },
    quickSearchProps: {
      type: String,
      required:true
    },
    destroyOnClose: {
      type: Boolean,
      default: false
    }
  },
  components: {
    Selector
  },
  data() {
    return {
      inputName: null,
      selectors: [],
      dialogVisible: false,
    };
  },
  methods: {
    // 同步value到当前所选数据中
    showDialog(selectors) {
      this.dialogVisible = true;
      if(!selectors){
       selectors = [];
      }
      setTimeout(() => {
        this.$refs.selector.onShow(selectors);
      });
    },
    handleClose(done) {
      this.$refs.selector.onHide();
      done && done();
    },
    handleDialogSure() {
      this.dialogVisible = false;
      this.$refs.selector.onHide(true);
      this.$emit("onConfirm", this.selectors);
    },
    handleDialogCancel() {
      this.dialogVisible = false;
      this.$refs.selector.onHide();
    },
    handleRemove(item) {
      this.selectors.remove(item);
    },
    load(param, cb) {
      this.$emit("load", param, cb);
    }
  }
};
</script>
<style lang="scss" scoped>
.inputs {
  display: block;
}

.el-select__tags {
  background: #fff;
  margin-left: 1px;
}

.el-select__tags_readonly {
  position: relative;
  top: 50%;
}

.el-input__inner[aria-invalid='true'] {
  border-color: #f56c6c;
}
</style>
