<!--
  @description: 采购计划寻源--产品未使用
  @author: gmy
  @routerPath:/procurementPlan/procurementPlanSourceList
  @date： 2020-8-3 13:25:01
-->
<template>
  <div>
    <!--标题-->
    <div class="title title_border p0-16">
      <i class="el-icon-s-home" />采购计划寻源
    </div>
    <section class="p0-16">
      <!--查询条件-->
      <div class="crud_menu_title clearfix">
        <span class="icon-line fl">查询条件</span>
        <span class="fr pointer" @click="handleToggleSearch">
          <el-link
            :underline="false"
            :class="toggleSearchStatus ? 'arrow-is-active' : ''"
            type="primary"
            class="toggleSearchStatus icon el-icon-arrow-down"
          />
          {{ toggleSearchStatus ? "收起高级查询" : "展开高级查询" }}
        </span>
      </div>
      <section :class="{ SearchContent_show: toggleSearchStatus }" class="clearfix SearchContent">
        <div class="SearchContent_leftInput fl">
          <el-form
            ref="searchForm"
            :model="searchForm"
            label-width="98px"
            label-position="left"
            inline
          >
            <!--采购计划编号-->
            <el-form-item
              ref="purchasePlanNo"
              label="采购计划编号"
              class="el-form-item--two"
              prop="purchasePlanNo"
            >
              <el-input
                v-model.trim="searchForm.purchasePlanNo"
                placeholder="采购计划编号"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
            <!--采购计划名称-->
            <el-form-item
              ref="purchasePlanName"
              label="采购计划名称"
              prop="purchasePlanName"
              class="el-form-item--two"
            >
              <el-input
                v-model.trim="searchForm.purchasePlanName"
                placeholder="采购计划名称"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
            <!--行项目编号-->
            <el-form-item
              ref="purchaseNo"
              label="行项目编号"
              class="el-form-item--two"
              prop="purchaseNo"
            >
              <el-input
                v-model.trim="searchForm.purchaseNo"
                placeholder="行项目编号"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
            <!--物资名称-->
            <el-form-item
              ref="purchaseDesc"
              label="产品描述"
              class="el-form-item--two"
              prop="purchaseDesc"
            >
              <el-input
                v-model.trim="searchForm.purchaseDesc"
                placeholder="产品描述"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
            <!--采购组织形式-->
            <el-form-item
              ref="purchaseOrganization"
              label="采购组织形式"
              class="el-form-item--two"
              prop="purchaseOrganization"
            >
              <el-select
                v-model="searchForm.purchaseOrganization"
                placeholder="请选择"
                style="width:20vw"
              >
                <el-option
                  v-for="item in plan_purchase_organizationList"
                  :key="item.typename"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--类别-->
            <el-form-item
              ref="purchaseType"
              label="类别"
              class="el-form-item--two"
              prop="purchaseType"
            >
              <el-select v-model="searchForm.purchaseType" placeholder="请选择" style="width:20vw">
                <el-option
                  v-for="item in plan_purchase_typeList"
                  :key="item.typename"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--采购单位-->
            <el-form-item
              ref="purchaseDepartName"
              label="采购单位"
              class="el-form-item--two"
              prop="purchaseDepartName"
            >
              <el-input
                v-model.trim="searchForm.purchaseDepartName"
                placeholder="采购单位"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
            <!--采购计划状态-->
            <el-form-item
              v-if="activeName === '00'"
              ref="tenderMethod"
              label="采购计划状态"
              class="el-form-item--two"
              prop="tenderMethod"
            >
              <el-select v-model="searchForm.dataStatus" placeholder="请选择" style="width:20vw">
                <el-option
                  v-for="item in plan_data_statusList"
                  v-if="item.typecode === '10' || item.typecode === '14'"
                  :key="item.typename"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              ref="purchaseCode"
              label="产品编码"
              class="el-form-item--two"
              prop="purchaseCode"
            >
              <el-input
                v-model.trim="searchForm.purchaseCode"
                placeholder="产品编码"
                style="width:20vw"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="fr">
          <el-button type="primary" @click="onSearch">
            搜 索
          </el-button>
          <el-button plain type="info" @click="resetForm('searchForm')">
            重 置
          </el-button>
        </div>
      </section>
      <div>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane
            v-for="(item,index) in planSourceList"
            :key="index"
            :label="item.typename"
            :name="item.typecode"
          />
        </el-tabs>
      </div>
      <div class="mb10">
        <!-- 寻源下可有执行采购计划和关闭采购计划 -->
        <template v-if="activeName==='00'">
          <el-button type="primary" @click="implementPurchase">
            执行采购
          </el-button>
          <el-button type="primary" plain @click="purchaseClose">
            关闭采购计划
          </el-button>
        </template> <template v-if="activeName==='01'">
          <el-button type="primary" @click="reopenClose">
            重新打开
          </el-button>
        </template>
      </div>
      <div>
        <el-table
          ref="smallTable"
          v-loading="loading"
          :row-key="getRowKeys"
          :data="tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            :reserve-selection="true"
            type="selection"
            width="55"
            fixed
            align="center"
          />
          <el-table-column type="index" label="序号" align="center" width="60" />
          <el-table-column
            prop="purchasePlanNo"
            label="采购计划编号"
            header-align="center"
            align="left"
            min-width="160"
          />
          <el-table-column
            prop="purchasePlanName"
            label="采购计划名称"
            header-align="center"
            align="left"
            min-width="280"
          />
          <el-table-column
            prop="purchaseNo"
            label="行项目编号"
            min-width="160"
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="purchaseCode"
            label="产品编码"
            min-width="160"
            header-align="center"
            align="center"
          />
          <el-table-column
            label="产品描述"
            prop="purchaseDesc"
            min-width="320"
            header-align="center"
            align="left"
          />
          <!--          <el-table-column
            prop="purchaseType"
            label="类别"
            header-align="center"
            align="center"
            min-width="80"
          >
            <template slot-scope="scope">
              <p>{{ filterDictText(plan_purchase_typeList,scope.row.purchaseType ) }}</p>
            </template>
          </el-table-column>-->
          <el-table-column
            prop="purchaseOrganization"
            label="采购组织形式"
            header-align="center"
            align="center"
            min-width="110"
          >
            <template slot-scope="scope">
              <p>{{ filterDictText(plan_purchase_organizationList,scope.row.purchaseOrganization ) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="采购单位"
            min-width="180"
            header-align="center"
            align="left"
          >
            <template slot-scope="scope">
              {{ scope.row.purchaseDepartName }}
            </template>
          </el-table-column>
          <el-table-column
            prop="dataStatus"
            label="采购计划状态"
            header-align="center"
            align="center"
            min-width="125"
          >
            <template slot-scope="scope">
              <p>{{ filterDictText(plan_data_statusList,scope.row.dataStatus ) }}</p>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="80" fixed="right">
            <template slot-scope="scope">
              <el-link type="primary" @click.stop="detail(scope.row)">
                [查看]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination :pagination-data="paginationData" @change="loadData" />
      </div>
    </section>
    <!-- 选择采购方式弹框 -->
    <selectMode-dialog
      v-if="selectModeData.dialog"
      :dialog-data="selectModeData"
      :select-tender-methoder="selectTenderMethoder"
    />
    <!-- 选择采购方式弹框 -->
    <lookTenderNo-dialog
      v-if="lookTederNoDialog.dialog"
      :dialog-data="lookTederNoDialog"
      :look-teder-no-return="lookTederNoReturn"
    />
  </div>
</template>

<script>
/* api接口*/
import {
  listPurchasePlanDetailPages, // 查询采购计划列表
  closePurchasePlanDetail, // 关闭采购计划
  openPurchasePlanDetail // 重新打开采购计划
} from '@/api/procurement/purchasplan'
/* 公共参数方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { filterObj, restoreInitialValue, filterDictText } from '@/assets/js/utils'
import { procurementPlanGrounpMixins } from '../mixins/procurementPlanGroupMixins' // 字典信息
/* 分页组件*/
import pagination from '@comp/common/pagination/pagination'
import selectMode from './procurementSelectMode-dialog' // 选择采购方式弹框
import lookTenderNo from './procurementLookTenderNo-dialog' // 查看采购单编号弹框
export default {
  name: 'BuildingProjectLedgerList',
  components: {
    'selectMode-dialog': selectMode, // 选择采购方式弹框
    'lookTenderNo-dialog': lookTenderNo, // 查看采购单编号
    pagination // 分页组件
  },
  mixins: [ListMixin, procurementPlanGrounpMixins],
  data() {
    return {
      getRowKeys(row) {
        return row.planDetailId
      },
      loading: true, // 加载效果
      //  当前用户选择采购单列表弹框
      myPurchasePlanListDialog: {
        dialog: false
      },
      searchForm: {
        purchaseType: '', // 类别
        purchaseOrganization: '', // 采购组织形式
        dataStatus: '', // 采购计划状态
        purchaseDepartName: '', // 采购单位
        purchaseDesc: '', // 物资描述
        purchaseNo: '', // 行项目编号
        purchasePlanNo: '', // 采购计划编号
        purchasePlanName: '', // 采购计划名称
        purchaseCode: '', // 产品编码
        tabType: '00' // 寻源项目状态 00寻源 01 关闭 默认00
      },
      activeName: '00', // 采购寻源tab切换状态 00 寻源 01关闭
      tableData: [], // 表格数据
      // 选择采购方式弹框
      selectModeData: {
        implementPurchaseList: '', // 自行采购选中数据id字符串集合
        dialog: false
      },
      // 查看采购单编号弹框
      lookTederNoDialog: {
        dialog: false,
        tenderNo: ''// 采购单编号
      },
      purchaseDepartName: '' // 采购单位
    }
  },
  watch: {
    // 查看行项目编号弹框关闭
    'lookTederNoDialog.dialog'(val) {
      if (!val) {
        this.loadData() // 获取数据
      }
    }
  },
  created() {
    // this.initWebTypeGroup() // 字典接口数据方法
    this.loadData() // 获取数据
  },
  methods: {
    filterDictText, // 数据字典
    // 采购计划关闭
    purchaseClose() {
      if (this.selectModeData.implementPurchaseList !== '') {
        this.$confirm('确认选中的采购计划不再执行采购吗？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          // 调用关闭采购计划方法
          closePurchasePlanDetail({
            planDetailIds: this.selectModeData.implementPurchaseList
          }).then(res => {
            // 获取数据
            this.loadData()
          })
        })
      } else {
        this.$message({
          message: '请选择一条记录!',
          type: 'warning'
        })
      }
    },
    // 采购计划重新打开
    reopenClose() {
      if (this.selectModeData.implementPurchaseList !== '') {
        this.$confirm('确认选中的采购计划重新开启吗？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          // 调用重新打开采购计划的方法
          openPurchasePlanDetail({
            planDetailIds: this.selectModeData.implementPurchaseList
          }).then(res => {
            // 获取数据
            this.loadData()
          })
        })
      } else {
        this.$message({
          message: '请选择一条记录!',
          type: 'warning'
        })
      }
    },
    // 选中采购计划行项目信息
    handleSelectionChange(val) {
      const data = []
      // 获取选中数据的planDetailId放道新数组中做参数
      val.forEach(ele => {
        data.push(ele.planDetailId)
      })
      // 获取选中的采购计划id集合转字符串
      this.selectModeData.implementPurchaseList = data.toString()
    },
    // 执行采购按钮
    implementPurchase() {
      // 判断有无选中采购计划项目
      if (this.selectModeData.implementPurchaseList !== '') {
        // 打开弹框
        this.selectModeData.dialog = true
        return
      }
      this.$message({
        message: '请选择要执行采购的采购计划!',
        type: 'warning'
      })
    },
    /*
    选择采购方式的回调
    tenderMethodData 回调参数 Object{tenderNo: 采购单编号，doalog：关闭弹框}
    */
    selectTenderMethoder(tenderMethodData) {
      // 用于多选表格,清空用户的选择
      this.$refs.smallTable.clearSelection()
      // 关闭选择采购方式弹框
      this.selectModeData.dialog = tenderMethodData.dialog
      this.lookTederNoDialog.tenderNo = tenderMethodData.tenderNo // 获取采购单编号
      setTimeout(() => {
        this.lookTederNoDialog.dialog = true // 打开查看采购单编号弹框
      }, 400)
    },
    /*
    查看采购单的回调
    lookTenderNo 回调参数 布尔值 false关闭弹框
    */
    lookTederNoReturn(lookTenderNo) {
      this.lookTederNoDialog.dialog = lookTenderNo
      this.loadData() // 获取数据
    },
    // 切换采购寻源状态
    handleClick() {
      // 初始化订单状态
      this.searchForm.tabType = this.activeName
      // 获取数据
      this.loadData()
    },
    // 获取数据
    loadData() {
      const params = this.getQueryParams() // 查询条件
      listPurchasePlanDetailPages(params)
        .then(response => {
          this.loading = false
          this.tableData = response.data.records
          this.paginationData.total = response.data.total
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 查询条件
    getQueryParams() {
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData() // 获取数据
    },
    // 重置
    resetForm() {
      // 清除数据
      restoreInitialValue(this.searchForm)
      // 寻源项目状态 00寻源 01 关闭 默认00
      this.searchForm.tabType = this.activeName
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData() // 获取数据
    },
    // 查看
    detail(row) {
      this.$router.push({
        path: '/procurement/procurementPlanSourceListDetail',
        query: { planDetailId: row.planDetailId }
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-table__body-wrapper::-webkit-scrollbar {
  padding-top: 5px;
  height: 12px;
}
</style>
