<template>
  <div class="common-page-content">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        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>
      <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-plus"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            size="small"
            slot="reference"
            icon="el-icon-delete"
            type="danger"
            class="btn btn-delete"
            @click="handleDelete"
          >
            {{ this.$t('common.delete') }}
          </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="status"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发布状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.status === 1" type="warning"
                  >未发布</el-tag
                >
                <el-tag v-if="row.status === 4" type="success"
                  >卖方已确认</el-tag
                >
                <el-tag v-if="row.status === 2">审核中</el-tag>
                <el-tag v-if="row.status === 3">待卖方确认</el-tag>
                <el-tag v-if="row.status === 5" type="danger"
                  >卖方不同意</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column min-width="10%" align="center" label="操作">
              <template v-slot="{row}">
                <!-- <template v-if="row.status === 3">
                  <el-button
                    type="primary"
                    plain
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                  <el-button
                    type="primary"
                    plain
                    size="small"
                    @click="shipmentsBtn(row)"
                    >发布</el-button
                  >
                </template>
                <template v-if="row.status === 0">
                  <el-button
                    type="primary"
                    plain
                    size="small"
                    @click="withdrawBtn(row)"
                    >撤回</el-button
                  >
                </template>

                <template v-if="row.status === 4">
                  <el-button
                    disabled
                    type="primary"
                    plain
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                  
                  <el-button
                    disabled
                    type="info"
                    plain
                    size="small"
                    @click="handleEdit(row)"
                    >查看反馈记录</el-button
                  >
                </template> -->
                  <el-button
                    type="primary"
                    plain
                    v-if="row.status == 1"
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                <el-button
                    type="primary"
                    plain
                    v-if="row.status == 1"
                    size="small"
                    @click="shipmentsBtn(row)"
                    >发布</el-button
                  >
                  <el-button
                    type="primary"
                    plain
                    size="small"
                    v-if="row.status != 1"
                    @click="handleDetail(row)"
                    >查看详情</el-button
                  >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageResult.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="pageResult.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageResult.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: 'statementManagement',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      publishList: [
        {
          value: 1,
          label: '未发布',
        },
        {
          value: 2,
          label: '审核中',
        },
        {
          value: 3,
          label: '待确认',
        },
        {
          value: 4,
          label: '已确认',
        },
        {
          value: 5,
          label: '不同意',
        },
      ],
      tableData: [],
      multipleSelection: [],
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      searchForm: {
        matCompanyId:''
      }, // 顶部搜索对象
      currentDataDel: [], // 删除时排查是否有在审核的数据
      pageResult: {page: 1, pageSize: 20, total: 0},
      currentUser:{}
    }
  },
  mounted() {},
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.searchForm.matCompanyId = this.currentUser.org[0].orgId
    this.handleSearch()
  },
  methods: {
    //售后翻译
    agreeName(val) {
      return this.publishList.filter((item) => item.value === val)[0].label
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      reconciliationManagement.queryEventsList(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.pageResult = {
          page: data.value.page,
          pageSize: data.value.pageSize,
          total: data.value.total,
        }
      })
    },
    //添加
    handleAdd() {
      this.$router.push('statementManagementAddDialog')
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'statementManagementAddDialog',
        query: {editRows: rows, pageBean: this.params},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'statementManagementDetail',
        query: {detailRows: rows, pageBean: this.params},
      })
    },
    //发布
    shipmentsBtn(row) {
      this.$confirm('确定要发布对账单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http.post('${portal}/bizReconciliation/v1/updateStatus?ids='+row.id+'&status='+3).then(res=>{
          if (res.data.state) {
            this.$message.success('发布成功！')
            this.handleSearch()
          }
        })
      })
    },
    // 删除
    handleDelete() {
      this.$confirm(this.$t('common.sureDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      }).then(() => {
        let ids = this.currentDataDel.map(item=>{
          return item.id
        })
        reconciliationManagement.batchDelete(ids).then((res) => {
          this.loadData(this.params)
        })
      })
    },
    //撤回
    withdrawBtn(rows) {
      this.$confirm('确定要撤回吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        reconciliationManagement.batchReceipt(rows.id).then((res) => {
          if (res.data.state) {
            this.$message.success('撤回成功！')
            this.loadData()
          }
        })
      })
    },
    //重置
    clearSearchForm() {
      this.searchForm = {
        matCompanyId : this.currentUser.org[0].orgId
      }
      this.handleSearch()
    },
    // 顶部搜索
    handleSearch() {
      let obj = this.searchForm
      let querys = []
      for (let i in obj) {
        if (obj[i]) {
          querys.push({
            group: 'main',
            operation:
              i == 'name' || i == 'code' || i == 'vendorName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
        }
      }
      console.log(querys, 'querys')
      this.params.querys = querys
      console.log(this.params, 'params')
      this.loadData(this.params)
    },
    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>
.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>
