<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        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"
            @change="seleteType"
            clearable
            v-model="search.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>
      <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
            :disabled="handleDeleteActive"
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete"
          >
            删除
          </el-button>
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="primary"
            v-if="querys[2].value == 1 || querys[2].value == 8"
            :loading="submiting"
            class="btn btn-plus"
            :disabled="handleDeleteActive"
            @click="handleSubmit"
          >
            提交审批
          </el-button>
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="primary"
            v-if="querys[2].value == 2"
            class="btn btn-plus"
            :loading="submiting"
            :disabled="handleDeleteActive"
            @click="handleWithdraw"
          >
            撤回提交
          </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"
            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="applicationNum"
              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="isAgencyProcure"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="是否代采"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cgsq-sfdc', row.isAgencyProcure) }}
              </template>
            </el-table-column>
            <!-- <el-table-column
              prop="applicationSource"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="来源类型"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cgsq-lylx', row.applicationSource) }}
              </template>
            </el-table-column> -->
            <el-table-column
              prop="isTax"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="报价方式"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cgsq-bjfs', row.isTax) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="payMethod"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="付款方式"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cglx-fkfs', row.payMethod) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="pendingTime"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="挂账时间(月)"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cdsj', row.pendingTime) }}
              </template>
            </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="10%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    v-if="row.status == 1 || row.status == 8"
                    icon="el-icon-edit"
                    type="primary"
                    plain
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    v-else
                    @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="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>
    <!-- 流程弹窗 -->
    <purchaseDialog ref="flowDialog" @reload="loadData"></purchaseDialog>
  </div>
</template>
  
  <script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import procurementManagement from '@/api/procurementManagement'
import purchaseDialog from '../module/flowDialog.vue'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
    purchaseDialog,
  },
  mixins: [TableMixin],
  data() {
    return {
      tableHeight: 1000,
      tableData: [],
      handleDeleteActive: true,
      handleDeleteId: [],
      multipleSelection: [],
      submiting: false,
      tableLoading: false,
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      search: {
        status: '1',
      },
      options: [
        {
          value: '1',
          label: '草稿',
        },
        {
          value: '2',
          label: '审批中',
        },
        {
          value: '3',
          label: '已审批',
        },
        {
          value: '4',
          label: '已立项',
        },
        {
          value: '8',
          label: '驳回',
        },
      ],
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'applicationSource',
          relation: 'AND',
          value: '1',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'applicationNum',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: '1',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matIfPlat',
          relation: 'AND',
          value: '1',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'applicationType',
          relation: 'AND',
          value: '',
        },
      ],
      showApproval: false,
    }
  },
  mounted() {
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.loadData()
    this.getAlias()
  },
  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
        })
    },
    //添加
    handleAdd() {
      this.$router.push({
        name: 'purchaseAddDialog',
        query: {type: 1},
      })
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'purchaseAddDialog',
        query: {editRows: rows, type: 1},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'purchaseDetail',
        query: {detailRows: rows, type: 1},
      })
    },
    // 删除
    handleDelete() {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      }).then(() => {
        procurementManagement
          .bizPurchasingApplicationRemove(this.handleDeleteId)
          .then((res) => {
            if (res.state) {
              this.$notify({
                type: 'success',
                message: '删除成功',
                duration: 2000,
              })

              this.loadData()
            }
          })
          .catch((res) => {
            this.$notify({
              type: 'error',
              message: res.message,
              duration: 2000,
              offset: 80,
            })
          })
      })
    },
    seleteType(val) {
      this.querys[2].value = val
      this.querys[2].operation = 'EQUAL'
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = '1'
      this.querys[1].value = ''
      this.querys[2].value = '1'
      this.querys[3].value = '1'
      this.querys[4].value = ''
      this.querys[2].operation = 'EQUAL'
      this.search.status = '1'
      this.loadData()
    },
    getAlias() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      params.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'a.APPROVAL_MENU_ALIAS_',
        relation: 'AND',
        value: this.$route.matched[1].instances.default.aliasPath[1].alias,
      })
      this.$http
        .post('${portal}/purchasingRulesDetail/v1/pageAllByOrg', params)
        .then((res) => {
          if (res.data.state) {
            let data = res.data.value.rows
            if (data.length != 0) {
              this.showApproval = true
            } else {
              this.showApproval = false
            }
          }
        })
    },
    // 提交审核
    handleSubmit() {
      if (this.showApproval == true) {
        let ids = this.handleDeleteId.join(',')
        this.submiting = true
        this.$http
          .post(
            '${portal}/purchasingApplication/v1/updateStatus?status=2' +
              '&ids=' +
              ids
          )
          .then((res) => {
            this.submiting = false
            if (res.data.state) {
              this.$message.success(res.data.message)
            }
            this.loadData()
          })
      } else {
        if (this.multipleSelection.length > 1) {
          this.$message.warning('请选择单条数据！')
          return
        }
        this.$refs.flowDialog.handleOpen(this.multipleSelection)
      }
    },
    // 撤回审核
    handleWithdraw() {
      this.submiting = true
      let ids = this.handleDeleteId.join(',')
      this.$http
        .post(
          '${portal}/purchasingApplication/v1/updateStatus?status=1' +
            '&ids=' +
            ids
        )
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
          }
          this.submiting = false
          this.loadData()
        })
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = 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>
</style>
  