<template>
  <div>
    <basic-container v-show="!detailsOpen">
      <avue-crud
          :option="option"
          :search.sync="search"
          v-model="form"
          :table-loading="loading"
          :data="dataList"
          ref="crud"
          :key="key"
          @on-load="onLoad"
          @search-change="searchChange"
          @row-del="rowDel"
          @refresh-change="refreshChange"
          @resetColumn="resetColumnTwo('crud','option','optionList',252)"
          @saveColumn="saveColumnTwo('crud','option','optionList',252)"
          :page.sync="page">
        <template slot="purchaseCompanyIdSearch">
          <crop-select v-model="search.purchaseCompanyId" corpType="GS"/>
        </template>
        <template slot="purchaseCompanyId" slot-scope="{row}">
          {{ row.purchaseCompanyName }}
        </template>
        <template slot-scope="{type,size,row,$index}" slot="menu">
          <el-button icon="el-icon-view" :size="size" :type="type" @click="check(row)">查看</el-button>
          <el-button icon="el-icon-delete" :size="size" v-if="row.status == 0" :type="type" @click="$refs.crud.rowDel(row,$index)">删除
          </el-button>
        </template>
        <template slot-scope="{type,size,row,$index}" slot="menuLeft">
<!--          <el-button icon="el-icon-plus" type="primary" :size="size" @click="detailsOpen = true">创建单据</el-button>-->
<!--          <el-button class="el-icon-document-copy" type="success" size="small">复制单据</el-button>-->
          <el-button class="el-icon-download" type="warning" size="small" @click="outExport">导出</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <detailsPage v-if="detailsOpen" :onLoad="form" :detailData="detailData" @backToList="backToList"></detailsPage>
  </div>
</template>

<script>
import {getList, remove} from "@/api/boxManagement/stockpilingManage/index.js";
import detailsPage from "./detailsPage"
import {getToken} from "@/util/auth";

export default {
  name: "index",
  components: {
    detailsPage
  },
  data() {
    return {
      detailsOpen: false,
      loading: false,
      search: {},
      form: {},
      dataList: [],
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      key:0,
      option: {},
      optionList: {
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        index: true,
        span: 8,
        border: true,
        height:"auto",
        searchMenuPosition: "right",
        align: "center",
        menuWidth:140,
        searchSpan: 8,
        searchIcon: true,
        searchIndex: 2,
        highlightCurrentRow: true,
        dialogWidth: "70%",
        showSummary: true,
        sumColumnList: [{
          name: 'boxNumber',
          type: 'sum',
          decimals:0
        }],
        column: [{
          label: '系统号',
          prop: 'sysNo',
          width: 220,
          search: true,
          searchOrder:2
        },{
          label: '箱号',
          prop: 'code',
          width: 100,
          searchOrder:1,
          overHidden: true,
          filterable: true,
          remote: true,
          type: "select",
          search: true,
          allowCreate: true,
          dicUrl: "/api/blade-box-tube/archives/list?size=10&current=1&code={{key}}",
          props: {
            label: "code",
            value: "code",
            res:"data.records"
          }
        }, {
          label: '合同号',
          prop: 'contractNo',
          width: 170,
          search: true,
          searchOrder:1,
        }, {
          label: '箱数',
          prop: 'boxNumber',
          width: 170
        }, {
          label: '箱型箱量',
          prop: 'boxTypeNumber',
          overHidden: true,
          width: 170
        }, {
          label: '付费对象',
          prop: 'purchaseCompanyId',
          width: 200,
          searchOrder:1,
          search: true,
        }, {
          label: '堆存地点',
          prop: 'address',
          width: 200
        }
        // , {
        //   label: '状态',
        //   prop: 'status',
        //   width: 140,
        //   overHidden: true,
        //   filterable: true,
        //   type: 'select',
        //   dataType: 'number',
        //   dicUrl: "/api/blade-system/dict-biz/dictionary?code=approval_status",
        //   props: {
        //     label: "dictValue",
        //     value: "dictKey"
        //   }
        // }
        , {
          label: '备注',
          prop: 'remarks',
          width: 500
        }, {
          label: '合同日期',
          prop: 'effectiveDateList',
          width: 100,
          search: true,
          hide:true,
          showColumn:false,
          overHidden: true,
          type: "date",
          searchOrder:1,
          searchRange: true,
          searchDefaultTime: ["00:00:00", "23:59:59"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss"
        }, {
          label: "制单人",
          prop: "createUserName",
          searchProp:"createUser",
          overHidden: true,
          width: 100,
          searchOrder:1,
          search: true,
          filterable: true,
          remote: true,
          type: "select",
          dicUrl: "/api/blade-user/page?size=20&current=1&account={{key}}",
          props: {
            label: "account",
            value: "id",
            res: 'data.records'
          }
        }, {
          label: "制单日期",
          prop: "createTime",
          searchProp:"createTimeList",
          type: "date",
          overHidden: true,
          width: 200,
          searchRange: true,
          searchDefaultTime: ["00:00:00", "23:59:59"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss"
        }, {
          label: "更新人",
          prop: "updateUserName",
          searchProp:"updateUser",
          overHidden: true,
          width: 100,
          search: true,
          searchOrder:1,
          filterable: true,
          remote: true,
          type: "select",
          dicUrl: "/api/blade-user/page?size=20&current=1&account={{key}}",
          props: {
            label: "account",
            value: "id",
            res: 'data.records'
          }
        }, {
          label: "更新日期",
          prop: "updateTime",
          searchProp:"updateTimeList",
          type: "date",
          overHidden: true,
          width: 200,
          searchRange: true,
          searchDefaultTime: ["00:00:00", "23:59:59"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss"
        }]
      }
    }
  },
  activated() {
    if (this.$route.query.check) {
      this.detailsOpen = false
      this.detailData = {
        id: this.$route.query.check.billId,
        check: this.$route.query.check,
      };
      this.form = {
        id:this.$route.query.check.billId
      }
      this.detailsOpen = true
      this.$router.$avueRouter.closeTag(window.location.hash.slice(1))
    }
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(252), this.optionList);
    this.key++
    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";
    }
  },
  methods: {
    check(row){
      this.form = row
      this.detailsOpen = true
    },
    backToList(type) {
      this.form = {}
      this.detailsOpen = false
      if (type === 0){
        this.detailData = {}
      }
      this.onLoad(this.page,this.search)
    },
    //刷新
    refreshChange() {
      this.onLoad(this.page, this.search)
    },
    rowDel(form, index) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(form.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      }).catch(() => {
      });
    },
    searchChange(params, done) {
      done();
      this.onLoad(this.page, params)
    },
    onLoad(page, params = {}) {
      params = {
        ...params,
        current: page.currentPage,
        size: page.pageSize,
        type: "DCF",
        ...Object.assign(params, this.search)
      }
      this.loading = true
      getList(params).then(res => {
        this.dataList = res.data.data.records
        this.page.total = res.data.data.total
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    },
    outExport() {
      let config = {params: {...this.search}}
      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-box-tube/transfer/exportTradingBoxOut',      //跳转目标窗口的地址
        query: {
          ...config.params,    //括号内是要传递给新窗口的参数
        }
      })
      window.open(routeData.href.slice(1, routeData.href.length) + '&' + `${this.website.tokenHeader}=${getToken()}`);
    },
//自定义列保存
    async saveColumnTwo(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 resetColumnTwo(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;
      }
    }
  }
}
</script>

<style scoped>

</style>
