<!--
  @description: 创建采购单-追加行项目信息弹框
  @Modifier：gmy
  @date：  2020-08-4 11:39:19
-->
<template>
  <el-dialog
    :visible.sync="dialogData.consignContractVisible"
    :before-close="close"
    :lock-scroll="false"
    append-to-body
    title="选择采购计划"
    width="calc(100vw-300px)"
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <div 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>
      <div :class="{'SearchContent_show':toggleSearchStatus}" class="clearfix SearchContent">
        <div class="SearchContent_leftInput fl">
          <el-form
            ref="searchForm"
            :model="searchForm"
            :inline="true"
            label-width="96px"
            label-position="right"
          >
            <!--采购计划名称-->
            <el-form-item
              ref="purchasePlanName"
              label="采购计划名称"
              prop="purchasePlanName"
            >
              <el-input
                v-model.trim="searchForm.purchasePlanName"
                placeholder="采购计划名称"
                style="width: 210px"
              />
            </el-form-item>
            <!--产品描述-->
            <el-form-item
              ref="purchaseDesc"
              label="产品描述"
              prop="purchaseDesc"
            >
              <el-input
                v-model.trim="searchForm.purchaseDesc"
                placeholder="产品描述"
                style="width:210px"
              />
            </el-form-item>
            <!--行项目编号-->
            <el-form-item
              ref="purchaseNo"
              label="行项目编号"
              prop="purchaseNo"
            >
              <el-input
                v-model.trim="searchForm.purchaseNo"
                placeholder="行项目编号"
                style="width: 210px"
              />
            </el-form-item>
            <!--产品编码-->
            <el-form-item
              ref="purchaseCode"
              label="产品编码"
              prop="purchaseCode"
            >
              <el-input
                v-model.trim="searchForm.purchaseCode"
                placeholder="产品编码"
                style="width: 210px"
              />
            </el-form-item>
            <!--类别-->
            <el-form-item
              ref="purchaseType"
              label="类别"
              prop="purchaseType"
            >
              <el-select v-model="searchForm.purchaseType" placeholder="请选择" clearable style="width:210px">
                <el-option
                  v-for="(item, index) in plan_purchase_typeList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--采购单位-->
            <el-form-item
              ref="purchaseDepartName"
              label="采购单位"
              prop="purchaseDepartName"
            >
              <el-input
                v-model.trim="searchForm.purchaseDepartName"
                placeholder="采购单位"
                style="width:210px"
              />
            </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>
      </div>
      <!--查询结果-->
      <div class="crud_menu_title">
        <span class="icon-line">查询结果</span>
      </div>
      <section>
        <el-table
          ref="smallTable"
          v-loading="loading"
          :data="tableData"
          :row-key="getRowKeys"
          class="tableLimit pointer"
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column v-if="purchaserType==='00'" :reserve-selection="true" type="selection" width="45" align="center" />
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50"
          />
          <el-table-column
            prop="purchasePlanName"
            label="采购计划名称"
            header-align="center"
            align="left"
            min-width="210"
          />
          <el-table-column
            prop="purchaseNo"
            label="行项目编号"
            min-width="180"
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="purchaseCode"
            label="产品编码"
            width="180"
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="purchaseDesc"
            label="产品描述"
            min-width="230"
            header-align="center"
            align="left"
          />
          <el-table-column
            prop="purchaseType"
            label="类别"
            header-align="center"
            align="center"
            min-width="50"
          >
            <template slot-scope="scope">
              <p
                v-for="(item, index) in plan_purchase_typeList"
                v-if="scope.row.purchaseType === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="purchaseDepartName"
            label="采购单位"
            min-width="180"
            header-align="center"
            align="left"
          />
        </el-table>
        <pagination :pagination-data="paginationData" @change="loadData" />
      </section>
    </div>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* api接口*/
import { listMyPurchasePlanDetailPages, appendPurchaseOrderSubjectMatter } from '@/api/procurement/purchasplan'
/* 公共参数方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { filterObj } from '@/assets/js/utils'
import { procurementGroupMixins } from '@/views/procurement/mixins/procurementGrounpMixins'
/* 分页组件*/
import pagination from '@comp/common/pagination/pagination'
/* api接口*/
export default {
  name: 'MyPurchasePlanListDialog',
  components: {
    pagination
  },
  mixins: [ListMixin, procurementGroupMixins],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return
      }
    }
  },
  data() {
    return {
      getRowKeys(row) {
        return row.planDetailId
      },
      purchaserType: '00', // 00采购员  01采购执行人
      loading: true,
      date: '',
      searchForm: {
        purchaseDepartName: '', // 采购单位
        purchaseNo: '', // 行项目编号
        purchaseCode: '', // 产品编码
        purchaseType: '', // 类别
        purchaseMethod: '', // 类别
        purchaseDesc: '', // 物资描述
        purchasePlanName: '', // 采购计划名称
        packId: '', // 项目标段id
        tenderId: '' // 项目id
      },
      tableData: [],
      implementPurchaseList: ''// 自行采购选中集合
    }
  },
  watch: {
    'dialogData.consignContractVisible'(val) {
      // 打开弹框加载数据
      if (val) {
        // 根据项目信息选择的采购方式来动态改变数据
        this.searchForm.purchaseMethod = this.dialogData.purchaseMethod
        this.searchForm.packId = this.dialogData.packId
        this.searchForm.tenderId = this.dialogData.tenderId
        this.loadData()
      }
    }
  },
  created() {
    this.initWebTypeGroup() // 字典接口数据方法
  },
  methods: {
    // 选中采购计划行项目信息
    handleSelectionChange(val) {
      const data = []
      val.forEach(ele => {
        data.push(ele.planDetailId)
      })
      this.implementPurchaseList = data.toString()
    },
    // 查询条件
    getQueryParams() {
      const param = Object.assign({}, this.searchForm)
      param.purchaseOrganization = this.dialogData.purchaseOrganization
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    // 加载数据
    loadData() {
      const params = this.getQueryParams() // 查询条件
      listMyPurchasePlanDetailPages(params)
        .then(response => {
          this.loading = false
          this.tableData = response.data.records
          this.paginationData.total = response.data.total
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 重置
    resetForm() {
      Object.keys(this.searchForm).forEach(val => {
        this.searchForm[val] = ''
      })
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 保存数据
    assetsData() {
      const data = {
        packId: this.dialogData.packId, // 项目标段id
        tenderId: this.dialogData.tenderId, // 项目id
        planDetailIds: this.implementPurchaseList
      }
      return data
    },
    // 采购计划取消按钮
    close() {
      Object.keys(this.searchForm).forEach(val => {
        this.searchForm[val] = ''
      })
      this.dialogData.consignContractVisible = false
    },
    // 采购计划选中提交信息确定按钮
    ensure() {
      // 是否选中校验
      if (this.implementPurchaseList !== '') {
        this.$confirm('确定添加吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          // 获取请求数据方法
          const data = this.assetsData()
          this.appendPurchaseOrderSubjectMatterData(data)
          Object.keys(this.searchForm).forEach(val => {
            this.searchForm[val] = ''
          })
        })
      } else {
        this.$message({
          message: '请选择要执行采购的采购计划!',
          type: 'warning' })
      }
    },
    // 封装追加数据接口方法
    appendPurchaseOrderSubjectMatterData(data) {
      appendPurchaseOrderSubjectMatter(data).then(res => {
        // 用于多选表格,清空用户的选择
        this.$refs.smallTable.clearSelection()
        this.$emit('consignContractInfo', true)
        this.dialogData.consignContractVisible = false
      }).catch(() => {})
    }
  }
}
</script>

<style scoped lang="less">
</style>
