<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 options"
              :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 pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            height="100%"
            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="receiptCode"
              width="130"
              label="收货单号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorName"
              show-overflow-tooltip
              align="center"
              label="发货公司"
              resizable
            >
            </el-table-column>
            <!-- <el-table-column
              prop="vendorUserName"
              show-overflow-tooltip
              width="100"
              align="center"
              label="发货人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorPhone"
              show-overflow-tooltip
              width="100"
              align="center"
              label="联系方式"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="arrivalDate"
              show-overflow-tooltip
              width="100"
              align="center"
              label="发货时间"
              resizable
            >
            </el-table-column> -->
            <el-table-column
              prop="matCompanyName"
              show-overflow-tooltip
              align="center"
              label="收货公司"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matUserName"
              show-overflow-tooltip
              width="100"
              align="center"
              label="收货人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="receiptDate"
              show-overflow-tooltip
              width="100"
              align="center"
              label="收货时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="amount"
              show-overflow-tooltip
              width="100"
              align="center"
              label="金额(元)"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="status"
              show-overflow-tooltip
              width="100"
              align="center"
              label="销售状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.status == 1" type="primary">已收货</el-tag>
                <el-tag v-if="row.status == 2" type="success">已对账</el-tag>
                <!-- <el-tag v-if="row.status == 4" type="info">已结算</el-tag> -->
              </template>
            </el-table-column>
            <el-table-column align="center" width="180" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    plain
                    type="primary"
                    v-if="row.status == 1"
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                  <el-button
                    plain
                    type="primary"
                    v-else
                    size="small"
                    @click="handleDetail(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="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 salesOrder from '@/api/BasicDataManagement/salesOrder.js'
import req from '@/request.js'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'confirmationReceipt',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      options: [
        {
          value: 1,
          label: '已收货',
        },
        {
          value: 2,
          label: '已对账',
        },
      ],
      tableData: [],
      multipleSelection: [],
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      searchForm: {
        code: 0,
        matCompanyId: '',
      }, // 顶部搜索对象
      currentUser: {},
      currentDataDel: [],
    }
  },
  mounted() {},
  created() {
    if (this.$route.params.pageBean) {
      this.params = this.$route.params.pageBean
      this.pageResult = this.$route.params.pageBean.pageBean
    }
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.searchForm.matCompanyId = this.currentUser.org[0].orgId
    this.handleSearch()
  },
  methods: {
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      this.tableLoading = true
      let data = []
      if (params) {
        data = params
      } else {
        this.params.querys = [
          {
            group: 'main',
            operation: 'IN',
            parentGroup: '',
            property: 'status',
            relation: 'AND',
            value: [1, 2, 3],
          },
        ]
        data = this.params
      }
      salesOrder.getGoods(data).then((res) => {
        console.log(res, 'res')
        this.tableLoading = false
        const data = (res && res.data) || {}
        this.tableData = data.value.rows || []
        this.pageResult = {
          page: data.value.page,
          pageSize: data.value.pageSize,
          total: data.value.total,
        }
        this.tableData.forEach((item) => {
          this.$set(item, 'truing', false)
        })
      })
    },
    //添加
    handleAdd() {
      this.$router.push('confirmationReceiptAdd')
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'confirmationReceiptAdd',
        query: {editRows: rows, pageBean: this.params},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'confirmationReceiptDetail',
        query: {detailRows: rows, pageBean: this.params},
      })
    },

    // 删除
    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
        })
        salesOrder.batchDelete(ids).then((res) => {
          this.loadData(this.params)
        })
      })
    },
    //撤回
    withdrawBtn() {},
    //重置
    clearSearchForm() {
      this.searchForm = {
        matCompanyId: this.currentUser.org[0].orgId,
      }
      this.handleSearch()
    },
    // 顶部搜索
    handleSearch(searchForm) {
      let obj = this.searchForm
      let querys = [
        {
          group: 'main',
          operation: 'IN',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: [1, 2, 3],
        },
      ]
      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.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;
}
.common-table-content {
  height: calc(100% - 100px);
}
/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
