<template>
  <!-- 中文下拉 -->
  <div>
    <div style="display: flex;">
      <el-input v-model="value" @input="inputChange" :placeholder="'请选择 ' + placeholder" :disabled="true" :size="size" readonly>
        <el-button slot="prepend" icon="el-icon-search" @click="open()" :disabled="disabled"></el-button>
      </el-input>
    </div>
    <el-dialog :title="titleDialog" :visible.sync="dialogVisible" width="70%" :before-close="false" @closed="closed" append-to-body>
      <span>
        <avue-crud
          v-if="dialogVisible"
          :option="option"
          :table-loading="loading"
          :data="dataList"
          :search.sync="query"
          v-model="form"
          id="out-table"
          ref="crud"
          @row-del="rowDel"
          @search-change="searchChange"
          @search-reset="searchReset"
          @on-load="onLoad"
          @current-row-change="handleCurrentRowChange"
        >
        </avue-crud>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="importData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDicinit } from "@/api/dicSelect/index";
import { bcorpstypedefineList } from "@/api/iosBasicData/bcorpstypedefine";
export default {
  data() {
    return {
      quotationObj: null,
      dialogVisible: false,
      options: [],
      loading: false,
      data: {},
      form: {},
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      dataList: [],
      selectionList: [],
      option: {
        height: 400,
        calcHeight: 30,
        tip: false,
        searchIcon: true,
        searchIndex: 3,
        searchShow: true,
        searchMenuSpan: 12,
        border: true,
        index: true,
        // selection: true,
        highlightCurrentRow: true,
        header: false,
        menu: false,
        column: [
          {
            label: "放箱号",
            prop: "containerNumber",
            width: 100,
            overHidden: true,
            search: true
          },
          {
            label: "放箱号类型",
            prop: "busType",
            width: 100,
            overHidden: true
          },
          {
            label: "场站",
            prop: "polStationId",
            width: 100,
            overHidden: true,
            hide:true,
            search: true,
            type: "select",
            filterable: true,
            remote: true,
            dicUrl: "/api/blade-los/bcorps/selectList?corpTypeName=场站&status=0&size=10&current=1&cnName={{key}}",
            props: {
              label: "cnName",
              value: "id",
              desc: "code",
              res: "data.records"
            }
          },
          {
            label: "场站",
            prop: "polStationCname",
            width: 100,
            overHidden: true,
          },
          {
            label: "箱型",
            prop: "boxType",
            width: 100,
            overHidden: true
          },
          {
            label: "可用箱数",
            prop: "remainingNum",
            width: 100,
            overHidden: true
          },
          {
            label: "占用箱量",
            prop: "occupyNum",
            width: 100,
            overHidden: true
          },
          {
            label: "优先等级",
            prop: "priorityLevel",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=priorityLevel",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            width: 100,
            overHidden: true
          },
          {
            label: "箱管备注",
            prop: "boxRemarks",
            width: 100,
            overHidden: true
          },
          {
            label: "备注",
            prop: "remarks",
            width: 100,
            overHidden: true
          }
        ],
        designer: null
      },
      treeOption: {
        nodeKey: "id",
        lazy: true,
        treeLoad: function(node, resolve) {
          const parentId = node.level === 0 ? 0 : node.data.id;
        },
        addBtn: false,
        menu: false,
        size: "small",
        props: {
          labelText: "标题",
          label: "cnName",
          value: "id",
          children: "children"
        }
      },
      treeData: [],
      selectWidth: 0,
      obtained: true
    };
  },
  props: {
    activateCreated: {
      type: Boolean,
      default: true
    },
    slotRight: {
      type: Boolean,
      default: false
    },
    diySlot: {
      type: Boolean,
      default: false
    },
    key: {
      type: [String, Number],
      default: null
    },
    label: {
      type: String,
      default: ""
    },
    rightLabel: {
      type: String,
      default: ""
    },
    keyValue: {
      type: [String, Number],
      default: null
    },
    res: {
      type: String,
      default: ""
    },
    placeholder: {
      type: String,
      default: "请输入"
    },
    clearable: {
      type: Boolean,
      default: true
    },
    value: {
      type: [String, Number],
      default: ""
    },
    method: {
      type: String,
      default: "GET"
    },
    url: {
      type: String,
      default: ""
    },
    dataName: {
      type: String,
      default: ""
    },
    multiple: {
      type: Boolean,
      default: false
    },
    filterable: {
      type: Boolean,
      default: false
    },
    remote: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: "small"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    searchShow: {
      type: Boolean,
      default: false
    },
    treeShow: {
      type: Boolean,
      default: false
    },
    mockData: {
      type: Array,
      default: function() {
        return [];
      }
    },
    collapseTags: {
      type: Boolean,
      default: false
    },
    default: {
      type: Boolean,
      default: false
    },
    defaultValue: {
      type: String,
      default: ""
    },
    disabledLabel: {
      type: String,
      default: ""
    },
    dataType: {
      type: String,
      default: ""
    },
    titleDialog: {
      type: String,
      default: "客户"
    },
    multipleStrings: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: "select"
    },

  },
  model: {
    prop: "value",
    event: "selectedValue"
  },
  created() {},
  mounted() {
    if (this.activateCreated) {
      // this.getDicData()
    } else {
      this.options = this.mockData;
    }
    if (this.diySlot) {
      this.$nextTick(() => {
        this.selectWidth = this.$refs.mySelect.$el.offsetWidth;
        this.$refs.mySelect.$el.querySelector(".el-select-dropdown").style.width = `${this.selectWidth}px`;
      });
    }
  },
  watch: {
    value: {
      handler(val, oldVal) {
        if (this.dataType == "string") {
          if (val) {
            if (typeof val == "string") {
              this.value = val.split(",");
            }
          } else {
            this.value = [];
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleCurrentRowChange(val) {
      this.quotationObj = val;
    },
    visibleChange(status) {
      if (status) {
        if (this.options.length == 0) {
          this.data = {};
          this.getDicData();
        }
      }
    },
    inputChange() {
      if (this.dataType == "string") {
        if (this.value && this.value.length) {
          this.$emit("selectedValue", this.value.join(","));
        } else {
          this.$emit("selectedValue", []);
        }
      } else {
        this.$emit("selectedValue", this.value);
      }
    },
    open() {
      //   this.selectionList = [];
      this.dialogVisible = true;
      this.quotationObj = null;
      this.getTree();
    },
    // 获取客户类别
    getTree() {
      bcorpstypedefineList(1, 50).then(res => {
        this.treeData = res.data.data.records;
      });
    },
    nodeClick(data) {
      this.query.corpTypeName = data.cnName;
      this.onLoad(this.page, this.query);
    },
    closed() {
      this.query = {};
      this.selectionList = [];
    },
    remoteMethod(query) {
      if (query !== "") {
        setTimeout(() => {
          this.data[this.dataName] = query;
          this.getDicData();
        }, 200);
      } else {
        setTimeout(() => {
          this.data = this.$options.data().data;
          this.getDicData();
        }, 200);
      }
    },
    getDicData() {
      if (this.url) {
        this.loading = true;
        getDicinit(this.method, this.url, this.data)
          .then(res => {
            if (this.res) {
              res.data.data[this.res].forEach(e => {
                if (this.disabledLabel.split(",").some(item => item == e[this.label])) {
                  e.disabled = true;
                }
              });
              this.options = res.data.data[this.res];
            } else {
              res.data.data.forEach(e => {
                if (this.disabledLabel.split(",").some(item => item == e[this.label])) {
                  e.disabled = true;
                }
              });
              this.options = res.data.data;
            }
          })
          .finally(() => {
            this.loading = false;
          });
      } else {
        this.options = this.mockData;
      }
    },
    IdGetDicData(data) {
      this.loading = true;
      getDicinit(this.method, this.url, data)
        .then(res => {
          if (this.res) {
            this.options = res.data.data[this.res];
          } else {
            this.options = res.data.data;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    selectChange(row) {
      if (this.dataType == "string") {
        let data = [];
        let ids = [];
        let multipleData = [];
        this.options.forEach(e => {
          if (row.length) {
            if (this.keyValue) {
              row.forEach(item => {
                if (item == e[this.keyValue]) {
                  data.push(e[this.label]);
                  this.$emit("selectChange", data.join(","));
                }
              });
            }
            if (this.label) {
              row.forEach(item => {
                if (item == e[this.label]) {
                  ids.push(e.id);
                  data.push(e[this.label]);
                  if (this.multipleStrings) {
                    multipleData.push(e);
                    this.$emit("selectChange", {
                      ids: ids.join(","),
                      names: data.join(","),
                      multipleData: multipleData
                    });
                  } else {
                    this.$emit("selectChange", {
                      ids: ids.join(","),
                      names: data.join(",")
                    });
                  }
                }
              });
            }
          } else {
            this.$emit("selectChange", null);
          }
        });
      } else {
        this.options.forEach(e => {
          if (this.keyValue) {
            if (row == e[this.keyValue]) {
              this.$emit("selectChange", e);
            }
          }
          if (this.label) {
            if (row == e[this.label]) {
              this.$emit("selectChange", e);
            }
          }
        });
      }
    },
    clear() {
      if (this.url) {
        this.data = this.$options.data().data;
        this.getDicData();
      }
      this.$emit("selectChange", null);
    },
    importData() {
      if (!this.quotationObj) {
        return this.$message.error("请选择数据");
      }
      this.dialogVisible = false;
      this.$emit("selectChange", this.quotationObj);
    },
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let obj = {};
      obj = {
        ...Object.assign(params, this.query)
      };
      this.loading = true;
      getDicinit(this.method, this.url, obj)
        .then(res => {
          this.dataList = res.data.data.filter(item=>item.remainingNum>0);
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/ .el-col-md-8 {
  width: 24.33333%;
}
</style>
