<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="申请单号">
          <el-input
            size="small"
            v-model="querys[1].value"
            clearable
            placeholder="请输入申请单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="申请类型">
          <dict-select
            dictCode="cgsq-sqlx"
            :hasDefaultOption="true"
            clearable
            size="small"
            v-model="querys[4].value"
            placeholder="请选择申请类型"
          />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select
            size="small"
            clearable
            v-model="querys[2].value"
            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>
      <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"
            :loading="submiting"
            :disabled="handleDeleteActive"
            v-if="querys[2].value == 2"
            class="btn btn-plus"
            @click="handleSubmit"
          >
          审核
          </el-button>
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="danger"
            v-if="querys[2].value == 2"
            :loading="rejecting"
            :disabled="handleDeleteActive"
            class="btn btn-plus"
            @click="clickReject"
          >
            驳回
          </el-button>
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="danger"
            :loading="rejecting"
            :disabled="handleDeleteActive"
            v-if="querys[2].value == 3"
            class="btn btn-plus"
            @click="handleReject"
          >
            反审核
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation" ref="tableContent">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            :max-height="tableHeight"
            @row-dblclick="handleDetail"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
            
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="applicationNum"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="申请单号"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="preparedUserName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="申请人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="preparedDate"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="制单日期"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="applicationDate"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="需求日期"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="applicationType"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="申请类型"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cgsq-sqlx', row.applicationType) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="procurementMethod"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="定价方式"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cgsq-djfs', row.procurementMethod) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="auditingDate"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="审核时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="auditingUser"
              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 type="danger" v-if="row.status == 1">草稿</el-tag>
                <el-tag type="warning" v-else-if="row.status == 2"
                  >审批中</el-tag
                >
                <el-tag type="" v-else-if="row.status == 3">已审批</el-tag>
                <el-tag type="danger" v-else-if="row.status == 8">驳回</el-tag>
                <el-tag type="success" v-else>已立项</el-tag>
              </template>
            </el-table-column>
            <el-table-column min-width="14%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button @click="handleDetail(row)" icon="el-icon-view" size="small" type="primary">详情</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>
    <el-dialog
      title="明细"
      :visible.sync="dialogVisible"
      width="70%"
      top="6vh"
      :close-on-click-modal="false"
      :before-close="dialogBeforeClose"
    >
      <el-table
        ref="singleTable"
        class="table"
        :data="detailsList"
        style="width: 100%"
        :max-height="tableHeight"
        highlight-current-row
        :border="true"
        stripe
        fit
        :cell-style="{'text-align': 'center'}"
        :header-cell-style="{'text-align': 'center'}"
      >
        <el-table-column type="index" width="55" label="序号" />
        <el-table-column
          label="企业物料编号"
          min-width="120"
          show-overflow-tooltip
          prop="matCode"
        >
        </el-table-column>
        <el-table-column
          label="平台物料编号"
          min-width="120"
          show-overflow-tooltip
          prop="matPlatcode"
        >
        </el-table-column>
        <el-table-column label="物料名称"  show-overflow-tooltip min-width="110" prop="matName">
        </el-table-column>
        <el-table-column label="物料牌号"  show-overflow-tooltip min-width="110" prop="matMaterial">
        </el-table-column>
        <el-table-column
          label="数量"
          min-width="110"
          prop="matNum"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="最高限价(元)"
          min-width="110"
          prop="maximumPriceLimit"
        >
        </el-table-column>
        <el-table-column label="需求日期"  show-overflow-tooltip min-width="130" prop="demandDate">
        </el-table-column>
        <el-table-column label="计划编号"  show-overflow-tooltip min-width="100" prop="planCode">
        </el-table-column>
        <el-table-column label="规格型号"  show-overflow-tooltip min-width="130" prop="matSpec">
        </el-table-column>
        <el-table-column label="计量单位" min-width="80" prop="matUnit">
        </el-table-column>
        <el-table-column label="品牌/产地" show-overflow-tooltip min-width="100" prop="matBrand">
        </el-table-column>
        <el-table-column label="基础扩展" show-overflow-tooltip min-width="140" prop="matBaseExt">
        </el-table-column>
        <el-table-column label="其他扩展" show-overflow-tooltip min-width="140" prop="matOtherExt">
        </el-table-column>
        <el-table-column label="税率" min-width="80" prop="taxRate">
        </el-table-column>
        <el-table-column label="挂账时间" min-width="100" prop="pendingTime">
        </el-table-column>
        <el-table-column label="承兑时间" v-if="form.payMethod==2" min-width="100" prop="acceptanceTime">
        </el-table-column>
        <el-table-column label="送货地址" show-overflow-tooltip min-width="180" prop="unloadAddress">
        </el-table-column>
        <el-table-column label="企业补充说明" show-overflow-tooltip min-width="180" prop="remarks">
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import procurementManagement from '@/api/procurementManagement'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      tableHeight: 1000,
      tableData: [],
      handleDeleteActive: true,
      dialogVisible: false,
      handleDeleteId: [],
      multipleSelection: [],
      detailsList: [],
      addressList:[],
      submiting: false,
      tableLoading: false,
      form:{},
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'preparedOrgName',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'applicationNum',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'IN',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: '2',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matIfPlat',
          relation: 'AND',
          value: '0',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'applicationType',
          relation: 'AND',
          value: '',
        },
      ],
      options:[{
          value:'1',label:'草稿'
        },{
          value:'2',label:'审批中'
        },{
          value:'3',label:'已审批'
        },
        {
          value: '4',
          label: '已立项',
        },{
          value:'8',label:'驳回'
        }],
    }
  },
  mounted() {
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.loadData()
    this.getAddress()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcTableHeight)
  },
  methods: {
    // 动态计算表格的高度，自适应当前容器
    calcTableHeight() {
      this.tableHeight = this.$refs.tableContent.offsetHeight
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      if (this.querys[3].value) {
        this.queryParam.querys.push(this.querys[3])
      }
      if (this.querys[4].value) {
        this.queryParam.querys.push(this.querys[4])
      }
      procurementManagement
        .bizPurchasingApplicationPage(this.queryParam)
        .then((res) => {
          this.queryParam.pageBean.total = res.value.total
          this.tableLoading = false
          this.tableData = res.value.rows
        })
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'purchaseAddDialog',
        query: {editRows: rows, type: 0},
      })
    },
    handleCommand(params) {
      if (params.command == 'audience') {
        this.$confirm('是否进行审核？', '提示', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.$http
              .post(
                '${portal}/purchasingApplication/v1/updateStatus?ids=' +
                  params.row.id +
                  '&status=3'
              )
              .then((res) => {
                if (res.data.state) {
                  this.$message.success(res.data.message)
                }
                this.loadData()
              })
          })
          .catch((action) => {})
      } else if (params.command == 'reject') {
        this.$confirm('是否进行反审核？', '提示', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          type: 'warning',
          cancelButtonText: '取消',
        })
          .then(() => {
            this.$http
              .post(
                '${portal}/purchasingApplication/v1/updateStatus?ids=' +
                  params.row.id +
                  '&status=8'
              )
              .then((res) => {
                if (res.data.state) {
                  this.$message.success(res.data.message)
                }
                this.loadData()
              })
          })
          .catch((action) => {})
      }
    },
    handleDetail(row) {
      this.$router.push({
        name: 'applyDetails',
        query: {detailRows: row, type: 0},
      })
    },
    getAddress() {
      let params = {
        pageBean: {
          pageSize: -1,
          page: -1,
        },
      }
      this.$http
        .post('${portal}/bizDeliveryAddress/v1/queryByPageOrg', params)
        .then((res) => {
          this.addressList = res.data.rows
        })
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    //审核
    handleSubmit() {
      let ids = this.handleDeleteId.join(',')
      if(this.handleDeleteId.length>1){
        this.$message.warning('请选择一条数据进行审核')
        return
      }
      this.$confirm('是否进行审核？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.submiting = true
          this.$http
            .post(
              '${portal}/purchasingApplication/v1/updateStatus?ids=' +
                ids +
                '&status=3'
            )
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
              }
              this.loadData()
              this.submiting = false
            })
        })
        .catch((action) => {})
    },
    // 驳回
    clickReject(){
      let ids = this.handleDeleteId.join(',')
      if (this.handleDeleteId.length > 1) {
        this.$message.warning('请选择一条数据进行驳回')
        return
      }
      this.$confirm('是否进行驳回？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.rejecting = true
          this.$http
            .post(
              '${portal}/purchasingApplication/v1/updateStatus?ids=' +
                ids +
                '&status=8'
            )
            .then((res) => {
              if (res.data.state) {
                this.$message.success(res.data.message)
              }
              this.loadData()
              this.rejecting = false
            })
        })
        .catch((action) => {})
    },
    // 反审核驳回
    handleReject(){
      let ids = this.handleDeleteId.join(',')
      if(this.handleDeleteId.length>1){
        this.$message.warning('请选择一条数据进行处理')
        return
      }
      this.rejecting = true
      this.$confirm('是否进行反审核？', '提示', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          type: 'warning',
          cancelButtonText: '取消',
        })
          .then(() => {
            this.$http
              .post(
                '${portal}/purchasingApplication/v1/updateStatus?ids=' +
                  ids +
                  '&status=8'
              )
              .then((res) => {
                if (res.data.state) {
                  this.$message.success(res.data.message)
                }
                this.rejecting = false
                this.loadData()
              })
          })
          .catch((action) => {})
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = '2'
      this.querys[3].value = '0'
      this.querys[4].value = ''
      this.loadData()
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(val) {
      if (val.length !== 0) {
        this.handleDeleteActive = false
        this.handleDeleteId = val.map((item) => {
          return item.id
        })
      } else {
        this.handleDeleteActive = true
      }
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
  },
  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;
}
/deep/.el-dialog__body{
  height: 580px;
  overflow-y: auto;
}
</style>
  