<template>
  <div>
    <basic-container v-show="show" class="page-crad">
      <avue-crud ref="crud" :option="option" :data="dataList" :page.sync="page" :search.sync="search"
        @search-change="searchChange" @current-change="currentChange" @size-change="sizeChange"
        @refresh-change="refreshChange" @on-load="onLoad" :table-loading="loading" @saveColumn="saveColumn"
        @resetColumn="resetColumn" :cell-style="cellStyle" @search-criteria-switch="searchCriteriaSwitch">
        <template slot="menuLeft">
          <el-button type="primary" icon="el-icon-plus" size="small" @click.stop="newAdd()">新增
          </el-button>
        </template>
        <template slot="outHarborNameSearch">
          <port-info v-model="search.outHarborName" />
        </template>
        <template slot-scope="{ row, index }" slot="menu">
          <el-button type="text" size="small" @click.stop="editOpen(row,2)">
            查看
          </el-button>
          <el-button type="text" size="small" @click.stop="rowDel(row, index)">
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <details-page v-if="!show" @goBack="backToList" :detailData="detailData" />
  </div>
</template>

<script>
import detailsPage from "./detailsPage";
import { option } from "./js/optionList";
import { getList, remove } from "@/api/maintenance/landFreight";
import { areaTypeTree } from "@/api/basicData/customerInformation";
export default {
  name: "index",
  data() {
    return {
      show: true,
      loading: false,
      form: {},
      search: {},
      detailData: {},
      dataList: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 50, 100, 200, 300, 400, 500]
      },
      option: {},
    };
  },
  components: {
    detailsPage
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(205), option);
    this.getAllWorkDicts()
    this.option.height = window.innerHeight - 210;
  },
  methods: {
    getAllWorkDicts() {
      this.getWorkDicts("cargo_type").then(res => {
        this.findObject(this.option.column, "goodsAttribute").dicData =
          res.data.data;
      });
    },
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    newAdd() {
      this.show = false;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false);
      });
      let data = this.deepClone(Object.assign(params, this.search));
      data.feesType = 2
      getList(
        page.currentPage,
        page.pageSize,
        data
      )
        .then(res => {
          if (res.data.data.records) {
            res.data.data.records.forEach(e => {
              e.itemLoading = true;
            });
          }
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    rowDel(row, index, done) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        remove(row.id).then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.onLoad(this.page, this.search);
          }
        });
      });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(205),
        this.option
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = option;
      const inSave = await this.delColumnData(this.getColumnName(205), option);
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    //返回列表
    backToList() {
      this.detailData = this.$options.data().detailData;
      this.show = true;
      this.onLoad(this.page, this.search);
    }
  }
};
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}
</style>
