<template>
  <div>
    <basic-container v-show="!show">
      <avue-crud
          :option="option"
          :data="dataList"
          :page.sync="page"
          :table-loading="loading"
          :key="key"
          ref="crud"
          @refresh-change="refreshChange"
          @search-change="searchChange"
          @resetColumn="resetColumn"
          @saveColumn="saveColumn"
          @on-load="onLoad">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary"
                     icon="el-icon-plus"
                     size="small"
                     @click="rowSave">新增</el-button>
        </template>
        <template slot-scope="{type,size,row,index}" slot="menu">
          <el-button icon="el-icon-view" :size="size" :type="type" @click.stop="rowCell(row,index)">查看</el-button>
          <el-button icon="el-icon-delete" :size="size" :type="type" v-if="row.status === 0" @click.stop="rowDel(row)">删除</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <details-page-edit v-if="show" @backToList="backToList" :detailData="detailData" :id="id"></details-page-edit>
  </div>
</template>

<script>
import detailsPageEdit from './detailsPageEdit'
import {selectInvoiceList,removeDelegationList} from '@/api/bidingDocument/issueTender'
export default {
  components: {
    detailsPageEdit
  },
  data(){
    return {
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 50, 100, 200, 300]
      },
      key:0,
      query:{},
      dataList:[],
      detailData: {},
      loading:false,
      show:false,
      id:'',
      option:{},
      optionList:{
        addBtn:false,
        editBtn:false,
        delBtn:false,
        align: 'center',
        index: true,
        border: true,
        stripe: true,
        searchMenuPosition:"right",
        searchSpan: 8,
        searchIcon: true,
        searchIndex: 2,
        highlightCurrentRow: true,
        selection: true,
        height: "auto",
        column: [{
          label: '发布日期',
          prop: 'releaseTime',
          search: true,
          overHidden: true,
          type: "date",
          searchRange: true,
          defaultTime: ['00:00:00', '23:59:59'],
          format: "yyyy-MM-dd HH:mm",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
          width:150,
          index:1,
        },{
          label: '截止日期',
          prop: 'deadlineTime',
          overHidden: true,
          search: true,
          type: "date",
          searchRange: true,
          defaultTime: ['00:00:00', '23:59:59'],
          format: "yyyy-MM-dd HH:mm",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
          width:150,
          index:2,
        },{
          label: '起运港',
          prop: 'departureHarbor',
          overHidden: true,
          width:120,
          index:3,
        },{
          label: '目的港',
          prop: 'objectiveHarbor',
          overHidden: true,
          width:120,
          index:4,
        },{
          label: '箱型箱量',
          prop: 'boxTypeQuantity',
          overHidden: true,
          width:120,
          index:5,
        },{
          label: '装柜日期',
          prop: 'loadingTime',
          overHidden: true,
          search: true,
          type: "date",
          searchRange: true,
          defaultTime: ['00:00:00', '23:59:59'],
          format: "yyyy-MM-dd HH:mm",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
          width:150,
          index:6,
        },{
          label: '货物信息',
          prop: 'cargoInformation',
          overHidden: true,
          width:120,
          index:7,
        },{
          label: '制单日期',
          prop: 'createTime',
          search: true,
          overHidden: true,
          type: "date",
          searchRange: true,
          defaultTime: ['00:00:00', '23:59:59'],
          format: "yyyy-MM-dd HH:mm",
          valueFormat: "yyyy-MM-dd HH:mm:ss",
          width:150,
          index:8,
        }, {
          label: '状态',
          disabled:true,
          prop: 'status',
          dataType: "string",
          type: 'select',
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=main_bid_status",
          props: {
            label: "dictValue",
            value: "dictKey"
          },
          width:100,
          index:9,
        }]
      }
    }
  },
  async created() {
    if (this.$route.query.check){
      this.detailData = {
        id: this.$route.query.check.billId,
        check: this.$route.query.check,
        auditId: this.$route.query.check.id
      };
      this.id = this.$route.query.check.billId
      this.show = true
      this.$router.$avueRouter.closeTag(window.location.hash.slice(1))
    }else {
      this.detailData = {}
    }
    this.option = await this.getColumnData(this.getColumnName(201), this.optionList);
    this.key++
    let i = 0;
    if (i % 3 !== 0) {
      const num = 3 - Number(i % 3)
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
  },
  methods:{
    //自定义列保存
    async saveColumn() {
      /**
       * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
       * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
       * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
       */
      const inSave = await this.saveColumnData(this.getColumnName(201), this.option);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    //自定义列重置
    async resetColumn() {
      this.option = this.optionList;
      const inSave = await this.delColumnData(this.getColumnName(201), this.optionList);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    rowSave(){
      this.show = true
    },
    backToList(args){
      this.show = false
      this.id = ''
    },
    //查看
    rowCell(row, index) {
      this.id = row.id
      this.show = true
    },
    //列表删除
    rowDel(row) {
      this.$confirm('此操作将永久删除该单据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeDelegationList({id: row.id}).then(res => {
          this.$message.success('删除成功');
          this.onLoad(this.page)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //刷新按钮
    refreshChange(){
      this.onLoad(this.page)
    },
    //搜索
    searchChange(params, done) {
      let data = params
      if (params.releaseTime) {
        data.releaseBeginTime = params.releaseTime[0]
        data.releaseEndTime = params.releaseTime[1]
      }
      if (params.deadlineTime) {
        data.deadlineBeginTime = params.deadlineTime[0]
        data.deadlineEndTime = params.deadlineTime[1]
      }
      if (params.loadingTime) {
        data.loadingBeginTime = params.loadingTime[0]
        data.loadingEndTime = params.loadingTime[1]
      }
      if (params.createTime) {
        data.createBeginTime = params.createTime[0]
        data.createEndTime = params.createTime[1]
      }
      delete data.releaseTime
      delete data.deadlineTime
      delete data.loadingTime
      delete data.createTime
      this.query = data;
      this.onLoad(this.page, data)
      done();
    },
    onLoad(page, params = {}) {
      let queryParams = {
        size: page.pageSize,
        current: page.currentPage,
        ...Object.assign(params, this.query)
      }
      this.loading = true;
      selectInvoiceList(queryParams).then(res=>{
        this.dataList = res.data.data.records
        this.page.total = res.data.data.total
      }).finally(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped>

</style>
