<template>
  <div class="common-page-content">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="发布状态">
          <el-select
            size="small"
            clearable
            v-model="searchForm.status"
            placeholder="请选择"
          >
            <el-option
              v-for="item in publishList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对账企业">
          <el-input
            size="small"
            v-model="searchForm.vendorName"
            clearable
            placeholder="请输入对账企业"
          ></el-input>
        </el-form-item>
        <el-form-item label="同步状态">
          <el-select
            size="small"
            clearable
            v-model="searchForm.syncStatus"
            placeholder="请选择"
          >
            <el-option
              v-for="item in syncStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="primary"
            class="btn btn-plus"
            @click="handlePush"
          >
            推送到支付平台
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleDetail"
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="isdCode"
              min-width="10%"
              label="对账单号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发起企业"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matUserName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发起人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyPhone"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发起人联系方式"
              resizable
            >
            </el-table-column>
            <!-- <el-table-column
              prop="isdCreatedate"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发货时间"
              resizable
            >
            </el-table-column> -->

            <el-table-column
              prop="vendorName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="对账单位"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorUserName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="对账人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorPhone"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="对账人联系方式"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="status"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="对账状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.status === 3" type="warning">未确认</el-tag>
                <el-tag v-if="row.status === 4" type="success">已确认</el-tag>
                <el-tag v-if="row.status === 5" type="danger">不同意</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="syncStatus"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="同步状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.syncStatus === 0" type="info">未同步</el-tag>
                <el-tag v-if="row.syncStatus === 1" type="success"
                  >已同步</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column min-width="16%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    type="primary"
                    plain
                    v-if="row.status != 3"
                    size="small"
                    @click="handleconfrim(row)"
                    >查看详情</el-button
                  >
                  <el-button
                    type="primary"
                    plain
                    v-if="row.status == 3"
                    size="small"
                    @click="handleDetail(row)"
                    >确认对账单</el-button
                  >

                  <!-- <el-button
                    type="primary"
                    plain
                    size="small"
                    @click="handlerefuse(row)"
                    >拒绝</el-button
                  > -->
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import reconciliationManagement from '@/api/BasicDataManagement/reconciliationManagement.js'
import req from '@/request.js'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      publishList: [
        {
          value: 3,
          label: '待确认',
        },
        {
          value: 4,
          label: '已确认',
        },
        {
          value: 5,
          label: '已反馈',
        },
      ],
      syncStatusList: [
        {
          value: '0',
          label: '未同步',
        },
        {
          value: '1',
          label: '已同步',
        },
      ],
      tableData: [],
      multipleSelection: [],
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      searchForm: {
        syncStatus: '0',
        vendorId: '',
        matCompanyId: '',
      }, // 顶部搜索对象
      currentDataDel: [], // 删除时排查是否有在审核的数据
    }
  },
  mounted() {},
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.searchForm.matCompanyId = this.currentUser.org[0].orgId
    this.searchForm.vendorId = this.currentUser.org[0].orgId
    this.handleSearch()
  },
  methods: {
    //售后翻译
    agreeName(val) {
      return this.publishList.filter((item) => item.value === val)[0].label
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.tableData = []
      reconciliationManagement.queryEventsList(this.params).then((res) => {
        console.log(res, 'res')
        this.tableLoading = false
        const data = (res && res.data) || {}
        this.tableData = data.value.rows || []
        console.log('返回的表格数据', this.tableData)
        this.queryParam.pageBean.page = res.data.value.page
        this.queryParam.pageBean.pageSize = res.data.value.pageSize
        this.queryParam.pageBean.total = res.data.value.total
      })
    },
    handleconfrim(rows) {
      this.$router.push({
        name: 'confirmationOpinion',
        query: {editRows: rows, pageBean: this.params},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'confirmationStatementDetail',
        query: {detailRows: rows, pageBean: this.params},
      })
    },
    //重置
    clearSearchForm() {
      this.searchForm = {
        syncStatus: '0',
        matCompanyId: this.currentUser.org[0].orgId,
        vendorId: this.currentUser.org[0].orgId,
      }
      this.handleSearch()
    },
    // 顶部搜索
    handleSearch() {
      let obj = this.searchForm
      let querys = []
      querys.push({
        group: 'main',
        operation: 'IN',
        parentGroup: '',
        property: 'status',
        relation: 'AND',
        value: [3, 4, 5],
      })
      for (let i in obj) {
        if (obj[i]) {
          if(i=='vendorId'){
            querys.push({
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'OR',
            value: obj[i],
          })
          }else{
            querys.push({
            group: 'main',
            operation:
              i == 'name' || i == 'code' || i == 'vendorName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
          }
          
        }
      }
      this.params.querys = querys
      this.loadData()
    },
    // 推送到支付平台
    handlePush() {
      if (this.currentDataDel.length == 0) {
        this.$message.error('请选择对账单数据')
        return
      }
      if (this.currentDataDel.length > 1) {
        this.$message.error('请选择一条对账单数据')
        return
      }
      this.$confirm('是否推送对账单到支付平台?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http
          .post(
            '${portal}/bizReconciliation/v1/pushToZfpt?ids=' +
              this.currentDataDel[0].id
          )
          .then((res) => {
            if (res.data.state) {
              this.$message.success(res.data.message)
              this.handleSearch()
            }
          })
      })
    },
    handleSelectionChange(currentData) {
      this.currentDataDel = currentData
      if (currentData.length === 0) return (this.deleteIds = '')
      let ids = []
      currentData.forEach((item) => {
        ids.push(item.id)
      })
      // this.$message.error(`【${item.demandTopic}】已上报待审核不能删除！`)
      this.deleteIds = ids.join(',')
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.loadData(this.params)
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.loadData(this.params)
    },
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.common-table-content {
  height: calc(100% - 90px);
}
.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  /deep/ i,
  span {
    font-size: 16px;
  }
}

/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
