<template>
  <div>
    <basic-container>
      <avue-crud :option="option" :data="dataList" ref="crud" 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" @resetColumn="resetColumn"
        :table-loading="loading">
        <template slot="moudleNameSearch">
          <el-select v-model="search.moudleName" clearable filterable>
            <el-option v-for="(item, index) in moudleOption" :label="item.dictValue" :value="item.dictKey"
              :key="index"></el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="moudleName">
          <span>{{
            scope.row.moudleName | moudleNameFormat(moudleOption)
          }}</span>
        </template>
        <template slot="menu" slot-scope="{ row, index }">
          <el-button type="text" icon="el-icon-unlock" size="small" @click="rowUnlock(row, index)">解锁</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import option from "./config/mainList.json";
import { lockList, lockRemove } from "@/api/lock/lockF";
import { getWorkDicts } from "@/api/system/dictbiz";
export default {
  name: "index",
  data() {
    return {
      option: {},
      dataList: [],
      form: {},
      page: {
        pageSize: 10,
        pagerCount: 5,
        total: 0
      },
      search: {},
      loading: false,
      moudleOption: [
        {
          dictValue: "海运出口",
          dictKey: "SE"
        },
        {
          dictValue: "海运进口",
          dictKey: "SI"
        }
      ]
    };
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(73), option);
    let i = 0;
    this.option.column.forEach(item => {
      if (item.search) i++;
    });
    if (i % 3 !== 0) {
      const num = 3 - Number(i % 3);
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
    getWorkDicts('moudleName').then(res => {
      if(res.data.data.length){
        this.moudleOption = res.data.data
      }
    })
  },
  filters: {
    moudleNameFormat(row, moudleOption) {
      let name;
      moudleOption.map(e => {
        if (row == e.dictKey) {
          name = e.dictValue;
        }
      });
      return name;
    }
  },
  methods: {
    searchChange(params, done) {
      this.onLoad(this.page, params);
      done();
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    refreshChange() {
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false);
      });
      this.page.currentPage = 1;
      this.onLoad(this.page, this.search);
    },
    onLoad(page, params) {
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false);
      });
      this.loading = true;
      lockList(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 - 260;
          }
          this.dataList.forEach(item => {
            this.$set(item, "insideList", []);
            this.$set(item, "loading", true);
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 解锁
    rowUnlock(row, index) {
      this.$confirm("是否确认解锁？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return lockRemove({ ids: row.id });
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.page.currentPage = 1;
          this.onLoad(this.page, { parentId: 0 });
        });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(73),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = option;
      const inSave = await this.delColumnData(this.getColumnName(73), option);
      if (inSave) {
        this.$message.success("重置成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    }
  }
};
</script>

<style scoped></style>
