<template>
  <div>
    <basic-container v-if="show" class="page-crad">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        v-model="form"
        :page.sync="page"
        :search.sync="search"
        @search-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
        @saveColumn="saveColumn"
        :cell-style="cellStyle"
      >
        <template slot="menuLeft">
          <el-button type="info" size="small">报表</el-button>
        </template>
        <template slot="corpIdSearch">
          <select-component
            v-model="search.corpId"
            :configuration="configuration"
          ></select-component>
        </template>
        <template slot="portOfLoadSearch">
          <port-info v-model="search.portOfLoad" />
        </template>
        <template slot="portOfDestinationSearch">
          <port-info v-model="search.portOfDestination" />
        </template>
        <template slot="businesDateSearch">
          <el-date-picker
            v-model="search.businesDate"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </template>
        <template slot="plannedDeliveryDateSearch">
          <el-date-picker
            v-model="search.plannedDeliveryDate"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </template>
        <template slot="createTimeSearch">
          <el-date-picker
            v-model="search.createTime"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </template>
        <template slot-scope="{ row }" slot="createUser">
          <span>{{ row.createUserName }}</span>
        </template>
        <template slot-scope="scope" slot="corpId">
          {{ scope.row.corpName }}
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="beforeOpenPage(scope.row, 1)"
            >查看
          </el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            size="small"
            @click.stop="editOpen(scope.row, 2)"
            >编辑
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <detail-page @goBack="goBack" :detailData="detailData" v-else></detail-page>
  </div>
</template>

<script>
import option from "./config/mainList.json";
import { getList, getPorts } from "@/api/basicData/shippingInquiry";
import detailPage from "./detailsPage.vue";

export default {
  name: "customerInformation",
  data() {
    return {
      configuration: {
        multipleChoices: false,
        multiple: false,
        collapseTags: false,
        placeholder: "请点击右边按钮选择",
        dicData: []
      },
      search: {},
      option: {},
      parentId: 0,
      dataList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      show: true,
      detailData: {},
      loading: false
    };
  },
  components: { detailPage },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(12), option);
    let _this = this;
    this.option.column.forEach(e => {
      if (e.prop == "exchangeRate") {
        e.formatter = function(row) {
          return _this.textFormat(
            Number(row.exchangeRate ? row.exchangeRate : 0) / 100,
            "0.00%"
          );
        };
      }
      if (e.prop == "creditAmount") {
        e.formatter = function(row) {
          return _this.textFormat(
            Number(row.creditAmount ? row.creditAmount : 0),
            "#,##0.00"
          );
        };
      }
    });
    getPorts().then(res => {
      this.findObject(this.option.column, "portOfLoad").dicData = res.data;
      this.findObject(this.option.column, "portOfDestination").dicData =
        res.data;
    });
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    //查看跳转页面
    beforeOpenPage(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      if (params.businesDate) {
        params.orderStartDate = params.businesDate[0];
        params.orderEndDate = params.businesDate[1];
      }
      if (params.plannedDeliveryDate) {
        params.plannedDeliveryStart = params.plannedDeliveryDate[0];
        params.plannedDeliveryEnd = params.plannedDeliveryDate[1];
      }
      if (params.createTime) {
        params.createTimeStart = params.createTime[0];
        params.createTimeEnd = params.createTime[1];
      }
      delete params.businesDate;
      delete params.plannedDeliveryDate;
      delete params.createTime;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params) {
      if (this.search.businesDate && this.search.businesDate.length > 0) {
        params = {
          ...params,
          orderStartDate: this.search.businesDate[0],
          orderEndDate: this.search.businesDate[1]
        };
        delete params.businesDate;
      }
      getList(page.currentPage, page.pageSize, params).then(res => {
        this.dataList = res.data.data.records ? res.data.data.records : [];
        this.page.total = res.data.data.total;
        if (this.page.total) {
          this.option.height = window.innerHeight - 380;
        } else {
          this.option.height = window.innerHeight - 305;
        }
      });
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    goBack() {
      this.detailData = this.$options.data().detailData;
      this.show = true;
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(12),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    }
  }
};
</script>

<style scoped>
::v-deep .select-component {
  display: flex;
}
.page-crad ::v-deep .basic-container__card {
  height: 86.5vh;
}
</style>
