<template>
  <div>
    <basic-container>
      <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :search.sync="query"
        v-model="form" id="out-table" :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', 500)"
        @saveColumn="saveColumn('crud', 'option', 'optionBack', 500)" @on-load="onLoad">
        <template slot="menuLeft">
          <el-button type="success" size="small" :disabled="selectionList.length == 0"
            @click="allClick('批量修改POD场站')">批量修改POD场站
          </el-button>
          <el-button type="warning" size="small" @click="outExport">导 出
          </el-button>
        </template>
      </avue-crud>
      <el-dialog append-to-body title="批量修改POD场站" :visible.sync="podDialog" width="30%" v-dialog-drag>
        <span>
          <avue-form v-if='podDialog' ref="form2" v-model="podForm" :option="podOption">
            <tempalte slot="podStationCname">
              <dic-select v-model="podForm.podStationCname" placeholder="场站" label="cnName" res="records"
                :slotRight="true" rightLabel="code"
                url="/blade-los/bcorps/selectList?current=1&size=5&corpTypeName=场站&status=0" :filterable="true"
                :remote="true" dataName="cnName" @selectChange="dicChange('podStationCname', $event)"></dic-select>
            </tempalte>
          </avue-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="podDialog = false" size="mini">取 消</el-button>
          <el-button type="primary" @click.stop="podSubmit" size="mini">确 定</el-button>
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { getList, batchUpdatePodStation } from "@/api/iosBasicData/podStation.js";
import { defaultDate2 } from "@/util/date";
import dicSelect from "@/components/dicSelect/main";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      podDialog: false,
      podForm: {},
      podOption: {
        menuBtn: false,
        span: 24,
        labelWidth: 120,
        disabled: false,
        column: [
          {
            label: 'POD场站',
            prop: 'podStationCname',
            rules: [{
              required: true,
              message: " ",
              trigger: "blur"
            }],
          },
          {
            label: '还箱日期',
            prop: 'returnEmptyTime',
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd 00:00:00",
            span: 24,
            rules: [{
              required: true,
              message: " ",
              trigger: "blur"
            }],
          },
          {
            label: '目的港场站地址',
            prop: 'podCyAddress',
            disabled: false
          },
          {
            label: '目的港场站联系人',
            prop: 'podCyContact',
            disabled: false
          },
          {
            label: '目的港场站邮箱',
            prop: 'podCyEmail',
            disabled: false
          },
          {
            label: '目的港场站电话',
            prop: 'podCyTel',
            disabled: false
          },
        ]
      },
      form: {},
      query: {
        whether: '0',
        businessType: "SE",
        boxBelongsTo: 'SOC',
        actualEtdList: defaultDate2()
      },
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {},
      optionBack: {
        height: 'auto',
        calcHeight: 30,
        menuWidth: 140,
        menu: false,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        searchIcon: true,
        align: 'center',
        searchIndex: 3,
        column: [
          {
            label: '客户',
            prop: 'corpName',
            overHidden: true,
          },
          {
            label: '箱号',
            prop: 'cntrNo',
            overHidden: true,
            search: true,
          },
          {
            label: '铅封号',
            prop: 'sealNo',
            overHidden: true,
          },
          {
            label: 'HB/L NO',
            prop: 'hblno',
            overHidden: true,
            search: true,
          },
          {
            label: 'POD场站',
            prop: 'podStationCname',
            overHidden: true,
            search: true,
            type: "select",
            filterable: true,
            remote: true,
            dicUrl: "/api/blade-los/bcorps/selectList?current=1&size=5&corpTypeName=场站&status=0&cnName={{key}}",
            props: {
              label: "cnName",
              value: "cnName",
              desc: 'code',
              res: "data.records"
            }
          },
          {
            label: 'MB/L NO',
            prop: 'mblno',
            overHidden: true,
            search: true,
          },
          {
            label: 'ETD',
            prop: 'etd',
            width: 120,
            search: true,
            searchProp: 'actualEtdList',
            overHidden: true,
            type: "date",
            searchRange: true,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd"
          },
          // {
          //   label: '单据类型',
          //   prop: 'billType',
          //   hide: true,
          //   overHidden: true,
          //   search: true,
          //   type: 'select',
          //   dicData: [
          //     {
          //       label: '直单',
          //       value: 'DD'
          //     },
          //     {
          //       label: '分单',
          //       value: 'MH'
          //     }
          //   ]
          // },
          {
            label: '船名',
            prop: 'vesselCnName',
            overHidden: true,
            search: true,
            type: "select",
            filterable: true,
            remote: true,
            dicUrl: "/api/blade-los/bvessels/list?status=0&size=5&current=1&cnName={{key}}",
            props: {
              label: "cnName",
              value: "cnName",
              desc: 'code',
              res: "data.records"
            }
          },
          {
            label: '航次',
            prop: 'voyageNo',
            overHidden: true,
            search: true,
          },
          {
            label: '起运港',
            prop: 'polName',
            overHidden: true,
            search: true,
            type: "select",
            filterable: true,
            remote: true,
            dicUrl: "/api/blade-los/bports/list?status=0&size=5&current=1&cnName={{key}}",
            props: {
              label: "cnName",
              value: "cnName",
              desc: 'code',
              res: "data.records"
            }
          },
          {
            label: '卸货港',
            prop: 'podName',
            overHidden: true,
            search: true,
            type: "select",
            filterable: true,
            remote: true,
            dicUrl: "/api/blade-los/bports/list?status=0&size=5&current=1&cnName={{key}}",
            props: {
              label: "cnName",
              value: "cnName",
              desc: 'code',
              res: "data.records"
            }
          },
          {
            label: '放箱号',
            prop: 'containerNumber',
            overHidden: true,
          },
          {
            label: '箱属',
            prop: 'boxBelongsTo',
            overHidden: true,
            search: true,
            type: 'select',
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=boxBelongsTo",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
          },
          {
            label: '箱型',
            prop: 'cntrTypeCode',
            overHidden: true,
          },
          {
            label: '是否录入',
            prop: 'whether',
            hide: true,
            overHidden: true,
            search: true,
            type: 'select',
            dicData: [{
              label: '否',
              value: '0'
            }, {
              label: '是',
              value: '1'
            }]
          },
        ]
      },
      data: [],
    };
  },
  components: {
    dicSelect
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(500), this.optionBack);
  },
  activated() {
    setTimeout(() => {
    }, 100);
  },
  methods: {
    allClick(name) {
      if (name == '批量修改POD场站') {
        this.podForm = {}
        this.podDialog = true
      }
    },
    dicChange(name, row) {
      if (name == 'podStationCname') {
        if (row) {
          this.podForm.podStationId = row.id
          this.podForm.podStationCode = row.code
          this.podForm.podStationEname = row.enName
        } else {
          this.podForm.podStationId = null
          this.podForm.podStationCode = null
          this.podForm.podStationEname = null
          this.podForm.podStationCname = null
        }
      }
    },
    podSubmit() {
      this.$refs["form2"].validate((valid, done) => {
        done();
        if (valid) {
          // let ids = []
          // for (let item of this.selectionList) {
          //   ids.push(item.id)
          // }
          let obj = {
            // ids: ids.join(','),
            businessType: 'SE',
            list: this.selectionList,
            ...this.podForm,
          }
          const loading = this.$loading({
            lock: true,
            text: '加载中',
            spinner: 'el-icon-loading',
            background: 'rgba(255,255,255,0.7)'
          });
          batchUpdatePodStation(obj).then(res => {
            this.$message.success("操作成功！");
            this.refreshChange()
            this.podDialog = false
          }).finally(() => {
            loading.close();
          })
        } else {
          return false;
        }
      });


    },
    searchReset() {
      this.query = {
        businessType: "SE"
      };
      this.onLoad(this.page);
    },
    // 搜索按钮点击
    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;
      getList(page.currentPage, page.pageSize, obj).then(res => {
        this.data = res.data.data.records;
        this.page.total = res.data.data.total;
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
          this.$refs.crud.dicInit();
        });
      }).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/UpdatePodStation/listExport',      //跳转目标窗口的地址
        query: {
          ...config.params,    //括号内是要传递给新窗口的参数
          identification: this.url
        }
      })
      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 (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>