<template>
  <div>
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :search.sync="query"
        v-model="form"
        :header-cell-class-name="headerClassName"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @resetColumn="resetColumn('crud', 'option', 'optionBack', 539)"
        @saveColumn="saveColumn('crud', 'option', 'optionBack', 539)"
        @on-load="onLoad"
      >
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="allClick('excel导入')">import</el-button>
          <el-button type="warning" size="small" @click="outExport">export</el-button>
        </template>

        <template slot="cntrNoSearch">
          <el-input v-model="query.cntrNo" size="small" placeholder="请输入箱号 多个箱号用空格区分" type="textarea" autosize></el-input>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog title="导入数据" append-to-body :visible.sync="excelBox" width="555px" :close-on-click-modal="false" v-dialog-drag>
      <avue-form
        v-if="excelBox"
        :option="excelOption"
        v-model="excelForm"
        :table-loading="excelLoading"
        :upload-before="uploadBefore"
        :upload-after="onSuccess"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleGet"> 点击下载<i class="el-icon-download el-icon--right"></i> </el-button>
        </template>
      </avue-form>
      <p style="text-align: center;color: #DC0505">
        温馨提示 第一次导入时请先下载模板
      </p>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/iosBasicData/containerReminder.js";
import dicSelect from "@/components/dicSelect/main";
import { getToken } from "@/util/auth";
import { getBcorpsattnList } from "@/api/iosBasicData/bcorpsattn";
export default {
  name: "ReturnContainerReminder",
  data() {
    return {
      excelBox:false,
      excelForm: {},
      excelLoading: false,
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
            display: true
          },
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data"
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/blade-los/boxdynamicsrecord/importExcel?type=JC"
          }
        ]
      },
      firstPage: false,
      form: {},
      query: {},
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      selectionList: [],
      option: {},
      optionBack: {
        height: "auto",
        calcHeight: 30,
        menuWidth: 140,
        menu: false,
        tip: false,
        searchShow: true,
        searchMenuSpan: 18,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        searchIcon: true,
        align: "center",
        searchIndex: 3,
        column: [
          {
            label: "branchName",
            prop: "branchId",
            hide: true,
            type: "select",
            filterable: true,
            dicUrl: "/api/blade-system/dept/lazy-list",
            props: {
              label: "deptName",
              value: "id"
            },
            search: false
          },
          {
            label: "ContainerNO",
            prop: "cntrNo",
            search: true,
            width: 120,
            overHidden: true
          },
          {
            label: "ReleaseRef",
            prop: "containerNumber",
            overHidden: true,
            width: 120,
            search: true
          },
          {
            label: "ContainerType",
            prop: "boxType",
            width: 140,
            overHidden: true
          },
          {
            label: "ATA",
            prop: "actualEta",
            overHidden: true,
            width: 100,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          {
            label: "port",
            prop: "podCnName",
            overHidden: true
          },
          {
            label: "depot",
            prop: "podStationCname",
            overHidden: true
          },
          {
            label: "depotLink",
            prop: "podCyContact",
            overHidden: true
          },
          {
            label: "depotTel",
            prop: "podCyTel",
            overHidden: true
          },
          {
            label: "depotEMail",
            prop: "podCyEmail",
            width: 120,
            overHidden: true
          },
          {
            label: "MB/L NO",
            prop: "mblno",
            overHidden: true,
            search: true
          },
          {
            label: "HB/L NO",
            prop: "hblno",
            overHidden: true,
            search: true
          },
          {
            label: "ReleaseRefType",
            prop: "boxSrcType",
            search: true,
            searchLabelWidth: 90,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=boxCategory",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            hide: true,
            overHidden: true
          }
        ]
      },
      data: [],
      saberUserInfo: {}
    };
  },
  components: {
    dicSelect
  },
  async created() {
    this.saberUserInfo = JSON.parse(localStorage.getItem("saber-userInfo")).content;
    this.option = await this.getColumnData(this.getColumnName(539), this.optionBack);
    if (this.saberUserInfo.role_name.indexOf("admin") != -1 || this.saberUserInfo.role_name.indexOf("总部") != -1) {
      this.option.searchMenuSpan = 12;
      this.option.column.forEach(item => {
        if (item.prop == "branchId") {
          item.search = true;
        }
      });
    }
    this.$nextTick(() => {
      this.$refs.crud.doLayout();
      this.$refs.crud.dicInit();
    });
  },
  methods: {
    allClick(name) {
      if (name == "excel导入") {
        this.excelBox = true;
      }
    },
    
    // 下载模板
    handleGet() {
      window.open(`/api/blade-los/boxReminderStatistics/returnContainerReminderExport?${this.website.tokenHeader}=${getToken()}`);
    },
    uploadBefore(file, done, loading) {
      done();
      loading = true;
    },
    // 上传成功
    onSuccess(res, done, loading, column) {
      if (res == "操作成功" || res.msg == "操作成功") {
        this.$message.success("上传成功！");
      }
      this.excelBox = false;
      // this.$message.success("导入成功！");
      loading = false;
      this.onLoad(this.page, this.query);
      done();
    },
    searchReset() {
      this.query = {};
    },
    // 搜索按钮点击
    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),
        cntrNo: this.query.cntrNo ? this.query.cntrNo.split(" ").join(",") : null
      };
      this.loading = true;
      getList(page.currentPage, page.pageSize, obj)
        .then(res => {
          this.data = res.data.data.records;
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    outExport() {
      let config = { params: { ...this.query } };
      if (config.params) {
        for (const propName of Object.keys(config.params)) {
          const value = config.params[propName];
          if (value !== null && typeof value !== "undefined") {
            if (value instanceof Array) {
              for (const key of Object.keys(value)) {
                let params = propName + "[" + key + "]";
                config.params[params] = value[key];
              }
              delete config.params[propName];
            }
          }
        }
      }
      const routeData = this.$router.resolve({
        path: "/api/blade-los/boxReminderStatistics/returnContainerReminderExport", //跳转目标窗口的地址
        query: {
          ...config.params, //括号内是要传递给新窗口的参数
          cntrNo: this.query.cntrNo ? this.query.cntrNo.split(" ").join(",") : null
        }
      });
      window.open(routeData.href.slice(1, routeData.href.length) + "&" + `${this.website.tokenHeader}=${getToken()}`);
    },
    //自定义列保存
    async saveColumn(ref, option, optionBack, code) {
      /**
       * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
       * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
       * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
       */
      const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    //自定义列重置
    async resetColumn(ref, option, optionBack, code) {
      this[option] = this[optionBack];
      const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
      if (this.saberUserInfo.role_name.indexOf("admin") != -1 || this.saberUserInfo.role_name.indexOf("总部") != -1) {
        this.option.searchMenuSpan = 12;
        this.option.column.forEach(item => {
          if (item.prop == "branchId") {
            item.search = true;
          }
        });
      }
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    // 更改表格颜色
    headerClassName(tab) {
      //颜色间隔
      let back = "";
      if (tab.columnIndex >= 0 && tab.column.level === 1) {
        if (tab.columnIndex % 2 === 0) {
          back = "back-one";
        } else if (tab.columnIndex % 2 === 1) {
          back = "back-two";
        }
      }
      return back;
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep #out-table .back-one {
  background: #ecf5ff !important;
  text-align: center;
}

::v-deep #out-table .back-two {
  background: #ecf5ff !important;
  text-align: center;
}

.pointerClick {
  cursor: pointer;
  color: #1e9fff;
}

::v-deep .el-col-md-8 {
  width: 24.33333%;
}
</style>
