<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-scope="{ row,index}" slot="contractNo">
          <span style="color: #409EFF;cursor: pointer" @click.stop="editOpen(row, 2)">{{ row.contractNo }}</span>
        </template>
        <template slot-scope="{ row,index}" slot="corpId">
          <span>{{ row.corpName }}</span>
        </template>
        <template slot-scope="{ row,index}" slot="status">
          <el-switch v-model="row.status" active-value="0" inactive-value="1" @change="switchChange(row)">
          </el-switch>
        </template>
        <template slot="corpIdSearch">
          <crop-select v-model="search.corpId" corpType="KH" :refresh="false"></crop-select>
        </template>
        <template slot-scope="{ row, index }" slot="menu">
          <el-button type="text" size="small" :disabled="row.status==0" @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, submit } from "@/api/basicData/agreement";
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(203), option);
    this.option.height = window.innerHeight - 210;
    this.getAllWorkDicts()
  },
  methods: {
    getAllWorkDicts() {
      areaTypeTree().then(res => {
        this.findObject(this.option.column, "portOfDestination").dicData = res.data.data;
        this.$refs.crud.init();
      });
      this.getWorkDicts("cargo_type").then(res => {
        this.findObject(this.option.column, "goodsProps").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);
      });
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.search)
      )
        .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);
          }
        });
      });
    },
    switchChange(row) {
      this.$confirm("确定要" + row.stauts == 0 ? "开启" : "停用" + row.corpName + "吗?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        submit({ ...row })
          .then(res => {
            this.$message.success("修改成功");
          })
          .finally(() => {
            this.onLoad(this.page, this.search);
          });
      });

    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(203),
        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(203), option);
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.getAllWorkDicts()
        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>
