<!--
  @description: 招标计划-公司级分配规则
  @Modifier: Joel
  @routerPath: /tenderPlan/tenderPlanFirmClassAllotRule-list
  @date: 2020-6-2
-->
<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"
            inline
            label-width="124px"
            label-position="left"
          >
            <!--优先级-->
            <el-form-item label="优先级">
              <el-input v-model.trim="searchForm.priority" style="width: 220px" />
            </el-form-item>
            <!--概算金额-->
            <el-form-item
              label="概算金额（元）"
            >
              <el-input v-model.trim="searchForm.budgetAmountLower" style="width:78px" />
              -
              <el-input v-model.trim="searchForm.budgetAmountUpper" style="width:78px" />
            </el-form-item>
            <!--采购品类-->
            <el-form-item label="采购品类">
              <el-input v-model.trim="searchForm.purchaseClassName" style="width: 220px" @focus="dialogData.dialog=true" />
            </el-form-item>
            <!--采购级别-->
            <el-form-item label="采购级别">
              <el-select v-model="searchForm.purchaseLevel" style="width: 220px" placeholder="请选择">
                <el-option
                  v-for="(item,index) in purchaseSchemeLevelList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--评标办法-->
            <el-form-item label="评标办法">
              <el-select v-model="searchForm.bidEvaluationMethod" style="width: 220px" placeholder="请选择">
                <el-option
                  v-for="(item,index) in purchasesSchemeProEvalMethodList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--采购类别-->
            <el-form-item label="采购类别">
              <el-select v-model="searchForm.purchaseCategory" style="width: 220px" placeholder="请选择">
                <el-option
                  v-for="(item,index) in purchaseSchemeProjectTypeList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--采购类型-->
            <el-form-item label="采购类型">
              <el-select v-model="searchForm.purchaseType" style="width: 220px" placeholder="请选择">
                <el-option
                  v-for="(item,index) in purchaseSchemeTypeList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!-- 创建时间 -->
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="searchForm.createUserDate"
                :format="elValueFormat"
                :value-format="elValueSaveFormat"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width:220px"
              />
            </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 class="crud_menu_title">
        <span class="icon-line">查询结果</span>
      </div>
      <div class="mb10">
        <el-button type="primary" plain @click="addRule">
          添加分配规则
        </el-button>
        <el-button type="primary" plain @click="toggle">
          切换为{{ configData.repleseconfigText }}分配
        </el-button>
        <span class="hint">当前审核规则为<i class="toggle-text">{{ configData.configText }}</i>分配</span>
      </div>
      <div>
        <el-table
          ref="smallTable"
          v-loading="loading"
          :data="tableData"
        >
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60"
          />
          <el-table-column

            prop="buysersName"
            label="招标人"
            min-width="230"
            header-align="center"
            align="left"
          />
          <el-table-column
            prop="vfpUnitName"
            label="所属二级公司"
            min-width="230"
            header-align="center"
            align="left"
          />
          <el-table-column
            prop="schemeBudget"
            label="概算金额（元）"
            width="140"
            align="right"
          >
            <template slot-scope="scope">
              {{ scope.row.budgetAmountLower }}-{{ scope.row.budgetAmountUpper }}
            </template>
          </el-table-column>
          <el-table-column

            header-align="center"
            prop="purchaseClassName"
            label="采购品类"
            align="center"
            min-width="300"
          />
          <el-table-column
            header-align="center"
            prop="purchasLevel"
            label="采购级别"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span
                v-for="(item,index) in purchaseSchemeLevelList"
                v-if="scope.row.purchaseLevel === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="bidEvaluationMethod"
            align="center"
            min-width="150px"
            label="评标办法"
          >
            <template slot-scope="scope">
              <span
                v-for="(item,index) in purchasesSchemeProEvalMethodList"
                v-if="scope.row.bidEvaluationMethod === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            prop="purchaseCategory"
            label="采购类别"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span
                v-for="(item,index) in purchaseSchemeProjectTypeList"
                v-if="scope.row.purchaseCategory === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="采购类型"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span
                v-for="(item,index) in purchaseSchemeTypeList"
                v-if="scope.row.purchaseType === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            prop="priority"
            label="优先级"
            align="center"
            width="100"
          />
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            width="140"
            header-align="center"
          />
          <el-table-column
            prop="isUsed"
            label="状态"
            align="center"
            header-align="center"
          >
            <template slot-scope="scope">
              <span
                v-for="(item,index) in isUsedList"
                v-if="scope.row.isUsed === item.typecode"
                :key="index"
              >
                {{ item.typename }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="150"
          >
            <template slot-scope="scope">
              <!-- 启用 01 -->
              <el-link v-if="scope.row.isUsed==='01'" type="primary" @click.stop="disabledRule(scope.row)">
                [禁用]
              </el-link>
              <!-- 禁用 00 -->
              <template v-if="scope.row.isUsed==='00'">
                <el-link type="primary" @click.stop="enabledRule(scope.row)">
                  [启用]
                </el-link>
                <el-link type="primary" @click.stop="editRule(scope.row)">
                  [修改]
                </el-link>
                <el-link type="primary" @click.stop="deleteRule(scope.row.id)">
                  [删除]
                </el-link>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination :pagination-data="paginationData" @change="loadData" />
      </div>
      <!-- 采购品类 -->
      <category-tree-dialog :dialog-data="dialogData" @categoryTreeInfo="categoryTreeInfo" />
    </section>
  </div>
</template>

<script>
/* api */
import { getPretenderingPurchasesSchemeCompanyRuleVoPages, isUsedCompanyRuleById, deletePretPurchaseschemePretenderingCompanyRuleById } from '@/api/purchasePlan/pretcloudPurchasescheme'

/* 常用公共方法*/
import { filterObj, restoreInitialValue } from '@/assets/js/utils'// 清空value值
/* 混入公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { tenderPlan } from '../../../mixins/tenderPlan'
import { tenderPlanAllotMixin } from '../../../mixins/tenderPlanAllotMixin'
/* 组件*/
import pagination from '@comp/common/pagination/pagination' // 分页组件
import categoryTreeDialog from '@/views/common/tools/categoryTree-dialog'// 采购品类-弹框
export default {
  name: 'TenderPlanFirmClassAllotRuleList',
  components: {
    pagination,
    'category-tree-dialog': categoryTreeDialog// 采购品类-弹框
  },
  mixins: [ListMixin, tenderPlan, tenderPlanAllotMixin],
  data() {
    return {
      loading: false,
      tableData: [], // 自建项目列表

      // 弹窗-展示/隐藏状态 true 展示/false 隐藏
      dialogData: {
        title: '采购品类',
        dialog: false, // 采购品类
        selectCodes: [] // 当前选中的供货范围Code
      },
      searchForm: {
        priority: '', // 优先级
        departId: '', // 创建人所在公司
        budgetAmountUpper: '', // 概算金额（元）最大值
        budgetAmountLower: '', // 概算金额（元）最小值
        purchaseClassName: '', // 采购品类
        purchaseLevel: '', // 采购级别
        bidEvaluationMethod: '', // 评标办法
        purchaseCategory: '', // 采购类别
        purchaseType: '', // 采购类型
        createTimeStart: '', // 创建开始时间
        createTimeEnd: '', // 创建结束时间
        createUserDate: []// 创建时间-不传给后端
      }
    }
  },
  watch: {
    // 创建时间
    'searchForm.createUserDate'(val) {
      this.searchForm.createTimeStart = val[0]// 创建开始时间
      this.searchForm.createTimeEnd	 = val[1]// 创建结束时间
    },
    // 采购品类
    'searchForm.purchaseClassName'(val) {
      if (!val) {
        this.dialogData.selectCodes = []
        return
      }
    }
  },
  created() {
    this.loadData()// 获取数据
    this.initDataType('companyCurrentAssignedType', this.$store.getters.info.companyId)// 查询当前节点分配状态-mixins
    // 招标计划-字典信息
    this.initPurchaseSchemeWebTypeGroup()
  },
  methods: {
    // 获取数据
    loadData() {
      const params = this.getQueryParams(this.searchForm)// 查询条件
      getPretenderingPurchasesSchemeCompanyRuleVoPages(params).then(response => {
        this.loading = false
        this.tableData = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {

      })
    },
    // 启用/禁用接口
    isUsedSystemRuleById(row, isUsed) {
      const data = {
        id: row.id, // 分配规则id
        isUsed: isUsed// 启用/禁用,00禁用，01启用
      }
      isUsedCompanyRuleById(data).then(res => {
        row.isUsed = isUsed
        this.$message.success(`修改成功`)
      })
    },
    // 采购品类选择/清空
    categoryTreeInfo(data) {
      this.searchForm.purchaseClassName = data.labels
      this.dialogData.selectCodes = data.keys.split(',')
    },
    // 查询条件
    getQueryParams(searchForm) {
      const param = Object.assign({}, searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      param.departId = this.$store.getters.info.companyId// 创建人所在公司
      delete param.createUserDate// 创建时间-不传给后端
      return filterObj(param)
    },
    // 禁用
    disabledRule(row) {
      this.$confirm('确认禁用本条分配规则吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.isUsedSystemRuleById(row, '00')
      }).catch(() => {
      })
    },
    // 启用
    enabledRule(row) {
      this.$confirm('确认启用本条分配规则吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.isUsedSystemRuleById(row, '01')
      }).catch(() => {
      })
    },
    // 修改
    editRule(row) {
      this.$router.push({
        path: '/tenderPlan/tenderPlanFirmClassAllotRule-add',
        query: {
          id: row.id// 分配规则id
        }
      })
    },
    // 删除
    deleteRule(id) {
      this.$confirm('确认删除分配规则吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deletePretPurchaseschemePretenderingCompanyRuleById(id).then(res => {
          this.$message.success(`删除成功`)
          this.onSearch()// 搜索
        })
      }).catch(() => {
      })
    },
    // 添加分配规则
    addRule() {
      this.$router.push({
        path: '/tenderPlan/tenderPlanFirmClassAllotRule-add'
      })
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()// 获取数据
    },
    // 重置
    resetForm() {
      restoreInitialValue(this.searchForm)
      this.onSearch()
    }
  }
}
</script>

<style scoped lang="less">
  .hint{
    margin-left: 16px;
    .toggle-text{
      font-style: normal;
      color: red;
    }
  }
</style>
