
<!--
  @description: 部门级-新增分配规则
  @Modifier: Joel
  @routerPath: /tenderPlan/tenderPlanSectionClassAllotRule-add
  @date: 2020-6-2
-->
<template>
  <div>
    <div class="title p0-16 clearfix">
      <div class="title-back pointer" @click="back">
        <i class="iconfont icon-fanhui mr10" />新增分配规则
      </div>
    </div>
    <div class="p0-16">
      <el-form
        ref="form"
        :model="formData"
        :rules="rules"
        :inline-message="true"
      >
        <table class="from_table">
          <tr>
            <td>
              <el-form-item label="项目经理" prop="managerId" />
            </td>
            <td colspan="3">
              <el-input
                v-model="formData.managerName"
                placeholder="请选择"
                style="width: calc(100% - 180px)"
                disabled
              />
              <el-button type="primary" @click="projectManagerDialogData.dialog=true">
                选择
              </el-button>
              <el-button @click="projectManagerInfo('clear')">
                清空
              </el-button>
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="优先级" prop="priority" />
            </td>
            <td>
              <el-input
                v-model="formData.priority"
              />
            </td>
            <td>概算金额（元）</td>
            <td>
              <money :money-value="formData" :thousands="true" :unit-fixed="0" style="display: inline" width="calc((100% - 60px) / 2)" name="budgetAmountLower" />
              -
              <money :money-value="formData" :thousands="true" :unit-fixed="0" style="display: inline" width="calc((100% - 60px) / 2)" name="budgetAmountUpper" />
            </td>
          </tr>
          <tr>
            <td>招标人</td>
            <td colspan="3">
              <el-input
                v-model="formData.buysersName"
                placeholder="请选择"
                style="width: calc(100% - 180px)"
                disabled
              />
              <el-button type="primary" @click="buysersDialog.dialog=true">
                选择
              </el-button>
              <el-button @click="buysersDialogInfo('clear')">
                清空
              </el-button>
            </td>
          </tr>
          <tr>
            <td>采购品类</td>
            <td colspan="3">
              <el-input
                v-model="formData.purchaseClassName"
                placeholder="请选择"
                style="width: calc(100% - 180px)"
                disabled
              />
              <el-button type="primary" @click="dialogData.dialog=true">
                选择
              </el-button>
              <el-button @click="categoryTreeInfo('clear')">
                清空
              </el-button>
            </td>
          </tr>
          <tr>
            <td>采购级别</td>
            <td>
              <!-- 采购级别-->
              <el-select v-model="formData.purchaseLevel" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in purchaseSchemeLevelList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
            <td>评标办法</td>
            <td>
              <el-select v-model="formData.bidEvaluationMethod" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="(item,index) in purchasesSchemeProEvalMethodList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <tr>
            <td>采购类别</td>
            <td>
              <!-- 采购类别-->
              <el-select v-model="formData.purchaseCategory" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in purchaseSchemeProjectTypeList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
            <td>采购类型</td>
            <td>
              <el-select v-model="formData.purchaseType" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in purchaseSchemeTypeList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <!-- 保存按钮 -->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button type="primary" @click="submit('form')">
            生效
          </el-button>
        </div>
      </div>
    </div>
    <!-- 招标人 -->
    <subsidiaryOrganDialog :dialog-data="buysersDialog" @buyersInfo="buysersDialogInfo" />
    <!-- 采购品类 -->
    <category-tree-dialog :dialog-data="dialogData" @categoryTreeInfo="categoryTreeInfo" />
    <!-- 项目经理-选择弹框 -->
    <projectManagerDialog
      :dialog-data="projectManagerDialogData"
      role-codes="11"
      @projectManagerInfo="projectManagerInfo"
    />
  </div>
</template>

<script>
/* api */
import { savePretPurchaseschemePretenderingDepartmentRule, getPretPurchaseschemePretenderingDepartmentRuleById } from '@/api/purchasePlan/pretcloudPurchasescheme'
/* 常用方法 */
import { checkRate } from '@/assets/js/validate'// 校验输入内容
/* 混入 */
import { tenderPlan } from '../../../mixins/tenderPlan'
import { tenderPlanAllotMixin } from '../../../mixins/tenderPlanAllotMixin'
/* 组件*/
import money from '@comp/common/money/money'
import subsidiaryOrganDialog from '@/views/purchasePlan/tenderPlan/tenderPlanInfo/baseInfo/subsidiaryOrgan-dialog'// 招标人-弹框
import categoryTreeDialog from '@/views/common/tools/categoryTree-dialog'// 采购品类-弹框
import projectManagerDialog from '@/views/common/user/projectManager-dialog'// 代理机构/项目经理-选择弹框
export default {
  name: 'TtenderPlanSectionClassAllotRuleAdd',
  components: {
    money,
    subsidiaryOrganDialog,
    'category-tree-dialog': categoryTreeDialog, // 采购品类-弹框
    projectManagerDialog
  },
  mixins: [tenderPlan, tenderPlanAllotMixin],
  data() {
    return {
      projectManagerDialogData: {
        departId: this.$store.getters.info.departid, // 部门id
        dialog: false, // 展示状态
        title: '项目经理', // 标题
        departRoleName: '11', // 10采购人 11代理机构(项目经理)
        materialId: [] // Id去重列表
      },
      // 招标人
      buysersDialog: {
        dialog: false
      },
      // 弹窗-展示/隐藏状态 true 展示/false 隐藏
      dialogData: {
        title: '供货范围',
        dialog: false, // 供货范围
        selectCodes: [] // 当前选中的供货范围Code
      },
      formData: {
        repartitionLevel: '00', // 分配层级 00 业务部门
        managerName: '', // 项目经理
        managerId: '', // 项目经理code
        priority: '', // 优先级
        budgetAmountUpper: '', // 概算金额（元）最大值
        budgetAmountLower: '', // 概算金额（元） 最小值
        buysersName: '', // 招标人
        buysersId: '', // 招标人Code
        purchaseClassName: '', // 采购品类
        purchaseClass: '', // 采购品类Code
        purchaseLevel: '', // 采购级别
        bidEvaluationMethod: '', // 评标办法
        purchaseCategory: '', // 采购类别
        purchaseType: '', // 采购类型
        isUsed: '01'// 启用/禁用,00禁用，01启用
      },
      // 校验输入内容
      rules: {
        repartitionLevel: [{ required: true, message: '请选择', trigger: 'change' }], //  分配层级
        departmentName: [{ required: true, message: '请选择', trigger: 'change' }], // 业务部门
        managerId: [{ required: true, message: '请选择', trigger: 'change' }], // 项目经理
        priority: [{ required: true, validator: checkRate, trigger: 'blur' }] // 优先级
      }
    }
  },
  watch: {
    // 采购品类Code
    'formData.purchaseClass'(val) {
      this.dialogData.selectCodes = val ? val.split(',') : []// 前选中的采购品类Code
    }
  },
  created() {
    // 判断是否有id 如果有则是编辑
    if (this.$route.query.id) {
      this.initData(this.$route.query.id)// 初始化数据
    }
    // 招标计划-字典信息
    this.initPurchaseSchemeWebTypeGroup()
  },
  methods: {
    // 初始化数据
    initData(id) {
      getPretPurchaseschemePretenderingDepartmentRuleById(id).then(res => {
        this.formData = res.data
      })
    },
    // 保存接口信息
    saveData() {
      const data = {
        ...this.formData
      }
      savePretPurchaseschemePretenderingDepartmentRule(data).then(res => {
        this.back()// 返回
      })
    },
    // 生效
    submit(formName) {
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          const verify = this.verify()// 校验-mixins
          if (!verify) return verify// 不满足校验条件
          this.$confirm('确认生效本条分配规则吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.saveData()
          }).catch(() => {
          })
        }
      })
    },
    // 选择要分配的项目经理
    projectManagerInfo(data) {
      if (data === 'clear') { // 清空
        this.formData.managerName = '' // 项目经理
        this.formData.managerId = ''// 项目经理code
      } else { // 选择
        this.formData.managerName = data.name
        this.formData.managerId = data.id
      }
      this.projectManagerDialogData.dialog = false// 关闭弹框
    },
    // 招标人
    buysersDialogInfo(data) {
      if (data === 'clear') { // 清空
        this.formData.buysersName = ''
        this.formData.buysersId = ''
      } else { // 选择
        this.formData.buysersName = data.departName
        this.formData.buysersId = data.id
      }
    },
    // 采购品类选择/清空
    categoryTreeInfo(data) {
      if (data === 'clear') { // 清空
        this.formData.purchaseClassName = ''
        this.formData.purchaseClass = ''
      } else { // 选择
        this.formData.purchaseClassName = data.labels
        this.formData.purchaseClass = data.keys
      }
    },
    // 返回
    back() {
      this.$router.push({
        path: '/tenderPlan/tenderPlanSectionClassAllotRule-list'
      })
    }
  }
}
</script>

<style scoped>

</style>
