<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', 267)"
        @saveColumn="saveColumnTwo('crud', 'option', 'optionList', 267)" :page.sync="page">
        <template slot-scope="{ row, index }" slot="sysNo">
          <span style="color: #409EFF;cursor: pointer" @click.stop="check(row)">{{ row.sysNo }}
          </span>
        </template>
        <template slot-scope="{ row, index }" slot="corpName">
          <span style="color: #409EFF;cursor: pointer" @click.stop="check(row)">{{ row.corpName }}
          </span>
        </template>
        <template slot-scope="{type,size,row,index}" slot="menu">
          <!-- <el-button :size="size" :type="type" @click="check(row)">查看</el-button> -->
          <el-button :size="size" :type="type" :disabled="row.financeStatus == '已收款' || item >= 1"
            @click="$refs.crud.rowDel(row, index)">删除</el-button>
        </template>
        <!-- <template slot="corpNameSearch">
          <crop-select v-model="search.corpId" corpType="KH" :refresh="false"></crop-select>
        </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 type="warning" icon="el-icon-download" 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/oceanShipping/maritimeExport/index.js";
import { getList, remove } from "@/api/collectionSettlement/index.js";
import detailsPage from "./detailsPage"

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",
        size: "small",
        menuWidth: 50,
        searchSpan: 8,
        searchIcon: true,
        searchIndex: 5,
        highlightCurrentRow: true,
        dialogWidth: "70%",
        column: [{
          label: '单据编号',
          prop: "sysNo",
          searchOrder: 3,
          search: true,
          overHidden: true,
        }, {
          label: '合同号',
          prop: "contractNumber",
          searchOrder: 4,
          search: true,
          overHidden: true,
        }, 
        {
          label: "客户",
          prop: "corpId",
          search: true,
          // remote: true,
          filterable:true,
          searchOrder: 1,
          type: 'select',
          overHidden: true,
          props: {
            label: 'cname',
            value: 'id'
          },
          showColumn: false,
          hide: true,
          dicUrl: '/api/blade-sales-part/corpsDesc/listAll?corpType=KH',
        }, 
        {
          label: "客户",
          prop: "corpName",
          search: true,
          // remote: true,
          // filterable:true,
          // searchOrder: 1,
          // type: 'select',
          overHidden: true,
          // props: {
          //   label: 'cname',
          //   value: 'cname'
          // },
          // dicUrl: '/api/blade-sales-part/corpsDesc/listAll?corpType=KH',
        }, 
        {
          label: "结算日期",
          prop: "settlementDate",
          searchProp: "settlementDateList",
          searchOrder: 2,
          type: "date",
          search: true,
          overHidden: true,
          width: 100,
          searchRange: true,
          unlinkPanels: true,
          searchDefaultTime: ["00:00:00", "23:59:59"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss"
        }, {
          label: "所属公司",
          prop: "salesCompanyId",
          search: true,
          type: 'select',
          searchOrder: 6,
          props: {
            label: 'fullName',
            value: 'id'
          },
          dicUrl: '/api/blade-system/dept/top-list'
        }, {
          label: '账户',
          prop: "accountName",
          searchOrder: 4,
          search: true,
          overHidden: true,
          type: 'select',
          props: {
            label: 'cname',
            value: 'id',
            res: 'data.records'
          },
          dicUrl: '/api/blade-sales-part/accountManagement/list?enableOrNot=1&cname={{key}}&billType=HYCK',
          // dicFormatter: (res => {
          //   return res.data.records
          // }),
        }, {
          label: '金额',
          prop: "amount",
          // search: true,
          overHidden: true,
        }, {
          label: '经办人',
          prop: "handledByName",
          // search: true,
          overHidden: true,
          props: {
            label: "name",
            value: "id"
          },
          dicUrl: "/api/blade-user/client/gainUser",
        }, {
          label: '状态',
          prop: "financeStatus",
          search: true,
          searchOrder: 5,
          overHidden: true,
          type: 'select',
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=put_Status",
          props: {
            label: "dictValue",
            value: "dictKey"
          }
        },
        {
          label: "制单日期",
          prop: "createTime",
          // searchProp: "settlementDateList",
          searchOrder: 5,
          type: "date",
          // search:true,
          overHidden: true,
          width: 100,
          searchRange: true,
          searchDefaultTime: ["00:00:00", "23:59:59"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd HH:mm:ss"
        },
          // {
          //   label: "应结日期",
          //   prop: "dueDate",
          //   searchProp: "updateTimeList",
          //   type: "date",
          //   overHidden: true,
          //   width: 100,
          //   searchRange: true,
          //   searchDefaultTime: ["00:00:00", "23:59:59"],
          //   format: "yyyy-MM-dd",
          //   valueFormat: "yyyy-MM-dd HH:mm:ss"
          // }
        ]
      }
    }
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(267), 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";
    }
    // this.findObject(this.optionList.column,'corpId').click = ()=>{
    //   console.log('点击了');
    // }
  },
  // 解决AVue表格错位问题
  activated() {
    setTimeout(() => {
      this.$nextTick(() => {
        this.$refs.crud.doLayout()
        // this.$refs.crud.refreshTable()
      })
    }, 100)
  },
  mounted() {
    // 页面尺寸改变,重新渲染avue表格,避免表格错位
    window.addEventListener(
      'resize',
      () => {
        if (Math.abs(this.screenWidth - document.body.clientWidth) > 20) {
          this.$nextTick(() => {
            this.$refs.originPlace.refreshTable();
          })
        }
        this.screenWidth = document.body.clientWidth;
      }
    )
  },
  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)
      this.$nextTick(() => {
        this.$refs.crud.doLayout()
      })
    },
    //刷新
    refreshChange() {
      this.onLoad(this.page, this.search)
    },
    rowDel(form, index) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({ ids: form.id }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.dataList.splice(index, 1);
          this.onLoad(this.page)
        })
      }).catch(() => {
      });
    },
    searchChange(params, done) {
      // console.log(params,285)
      this.page.currentPage = 1;
      done();
      this.onLoad(this.page, params)
    },

    onLoad(page, params = {}) {
      this.search.accountId = this.search.accountName
      params = {
        ...params,
        current: page.currentPage,
        size: page.pageSize,
        billType: "SK",
        dc: 'd',
        ...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
      })
    },
    //自定义列保存
    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>
