<template>
  <div class="common-page-content">
    <div
      class="common-search-container"
      ref="contentPanel"
      :style="isExpend ? {height: '160px'} : ''"
    >
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="订单编码">
          <el-input
            size="small"
            v-model="searchForm.orderCode"
            clearable
            placeholder="请输入订单编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="采购方">
          <el-input
            size="small"
            v-model="searchForm.matCompanyName"
            clearable
            placeholder="请输入采购方"
          ></el-input>
        </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-date-picker
            v-model="searchForm.orderCreatedate"
            type="daterange"
            align="right"
            size="small"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
            clearable
            v-model="searchForm.status"
            placeholder="请选择订单状态"
          >
            <el-option
              v-for="item in statusList"
              :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>
        <el-link
          :underline="false"
          class="link-more-search"
          @click="handleMoreSearch"
        >
          {{ isExpend ? '收起搜索' : '更多搜索' }}
        </el-link>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <!-- 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"
            height="65vh"
          >
            <el-table-column
              type="index"
              label="序号"
              width="50"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="orderCode"
              min-width="20%"
              label="订单编码"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCompanyName"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="采购方"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorName"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="供货方"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="orderCreatedate"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="订单时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="status"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="订单状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.status == 2" type="danger">未确认</el-tag>
                <el-tag v-if="row.status == 3">已确认</el-tag>
              </template>
            </el-table-column>
            <!-- <el-table-column
              prop="state2"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="审批状态"
              resizable
            >
              <template v-slot="{row}">
                <el-tag v-if="row.state2 == 1">审批中</el-tag>
                <el-tag v-if="row.state2 == 2" type="success">已审批</el-tag>
                <el-tag v-if="row.state2 == 3" type="danger">已撤回</el-tag>
              </template>
            </el-table-column> -->
            <el-table-column min-width="15%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    @click="handleDetail(row)"
                    >{{row.status==2?'订单确认':'查看详情'}}</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>

    <el-dialog
      title="反馈信息"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :model="feedbackForm"
        label-width="auto"
        label-position="right"
      >
        <el-row>
          <el-form-item label="反馈问题">
            <el-input
              type="textarea"
              v-model="feedbackForm.remarks"
              clearable
              placeholder="请输入反馈问题"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="附件">
            <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
              list-type="picture-card"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import purchaseOrder from '@/api/BasicDataManagement/purchaseOrder.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,
      tableData: [],
      multipleSelection: [],
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      searchForm: {
        // state: '1',
      }, // 顶部搜索对象
      deleteIds: '', // 是否选中列表解锁删除按钮
      currentDataDel: [], // 删除时排查是否有在审核的数据
      statusList: [
        {label: '未确认', value: '2'},
        {label: '已确认', value: '3'},
      ],
      isExpend: false, // 搜索展开
      dialogVisible: false,
      feedbackForm: {},
      matCompanyId:'',
      vendorId:'',
      currentUser:{}
    }
  },
  mounted() {},
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.matCompanyId = this.currentUser.org[0].orgId
    this.vendorId = this.currentUser.org[0].orgId
    this.handleSearch()
  },
  methods: {
    handleSure(rows) {
      this.$confirm('确定要确认订单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        purchaseOrder.batchConfirmation(rows.id).then((res) => {
          if (res.data.state) {
            this.$message.success('确认订单成功！')
            this.loadData()
          }
        })
      })
    },
    handleFeedback() {
      this.dialogVisible = true
    },
    handleClose(done) {
      done()
    },
    handleMoreSearch() {
      this.isExpend = !this.isExpend
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      this.tableData = []
      let data = []
      if (params) {
        data = params
      } else {
        this.params.querys = [
          {
            group: 'main',
            operation: 'NOT_EQUAL',
            parentGroup: '',
            property: 'status',
            relation: 'AND',
            value: 1,
          },
        ]
        data = this.params
      }
      purchaseOrder.queryEventsList(data).then((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,
        }
      })
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'purchaseOrderAddDialog',
        query: {editRows: rows, pageBean: this.params},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'orderConfirmationDetail',
        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(() => {
        purchaseOrder.batchDelete(this.deleteIds).then((res) => {
          this.loadData(this.params)
        })
      })
    },
    //重置
    clearSearchForm() {
      this.searchForm = {
        vendorId : this.currentUser.org[0].orgId
      }
      this.handleSearch()
    },
    // 顶部搜索
    handleSearch() {
      let obj = this.searchForm
      let querys = [
        {
          group: 'main',
          operation: 'NOT_EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: 1,
        },
        {
          group: 'mains',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'vendorId',
          relation: 'AND',
          value: this.vendorId,
        },
        {
          group: 'mains',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matCompanyId',
          relation: 'OR',
          value: this.matCompanyId,
        }
      ]
      for (let i in obj) {
        if (obj[i]) {
          if (i == 'orderCreatedate') {
            querys.push(
              {
                group: 'main',
                operation: 'GREAT_EQUAL',
                parentGroup: '',
                property: i,
                relation: 'AND',
                value: obj[i][0] + ' 00:00:00',
              },
              {
                group: 'main',
                operation: 'LESS_EQUAL',
                parentGroup: '',
                property: i,
                relation: 'AND',
                value: obj[i][1] + ' 23:59:59',
              }
            )
          } else {
            querys.push({
              group: 'main',
              operation:
                i == 'name' ||
                i == 'code' ||
                i == 'companyName' ||
                i == 'matCompanyName' ||
                i == 'vendorName'
                  ? 'LIKE'
                  : 'EQUAL',
              parentGroup: '',
              property: i,
              relation: 'AND',
              value: obj[i],
            })
          }
        }
      }
      this.params.querys = querys
      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>
.common-table-content.btn-pagenation {
  height: calc(100% - 60px);
}
.link-more-search {
  width: 68px;
}
.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;
}
/deep/ .el-dialog .el-form-item {
  text-align: left !important;
}
</style>
