<!--
  @description: 公司级-新增分配规则
  @Modifier: Joel
  @routerPath: /tenderPlan/tenderPlanFirmClassAllotRule-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 v-if="allot==='00'">
            <td>
              <el-form-item label="分配层级" prop="repartitionLevel" />
            </td>
            <td>
              <el-radio-group v-model="formData.repartitionLevel">
                <el-radio
                  v-for="(item,index) in allotTierList"
                  :key="index"
                  :label="item.typecode"
                >
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
            <template v-if="formData.repartitionLevel==='00'">
              <td>
                <el-form-item label="业务部门" prop="departmentName" />
              </td>
              <td>
                <el-select v-model="formData.departmentName" style="width:100%" placeholder="请选择" @change="departmentChange">
                  <el-option
                    v-for="item in purchasesScheOperatingDepartmentList"
                    :key="item.departId"
                    :label="item.departName"
                    :value="item.departId"
                  />
                </el-select>
              </td>
            </template>
            <template v-if="formData.repartitionLevel==='01'">
              <td>
                <el-form-item label="项目经理" prop="managerId" />
              </td>
              <td>
                <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>
            </template>
          </tr>
          <!-- 部门计划管理员 -->
          <tr v-if="allot==='01'">
            <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>
          <!-- 00 业务部门 -->
          <tr v-if="formData.repartitionLevel==='00'">
            <td>
              <el-form-item label="所属二级公司" prop="vfpUnitName" />
            </td>
            <td colspan="3">
              <el-input v-model="formData.vfpUnitName" placeholder="所属二级公司" disabled style="width: calc(100% - 180px)" />
              <el-button type="primary" @click="handelvfpUnitName('00')">
                选择
              </el-button>
              <el-button @click="handelvfpUnitName('01')">
                清空
              </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" />
    <!--所属二级公司-->
    <secondLevelOrganTree ref="secondLevelOrganTree" @secondaryLevelCheckedNodes="checkedNodes" />
    <!-- 采购品类 -->
    <category-tree-dialog :dialog-data="dialogData" @categoryTreeInfo="categoryTreeInfo" />
    <!-- 项目经理-选择弹框 -->
    <projectManagerDialog
      :dialog-data="projectManagerDialogData"
      role-codes="11"
      @projectManagerInfo="projectManagerInfo"
    />
  </div>
</template>

<script>
/* api */
import { savePretPurchaseschemePretenderingCompanyRule, getPretPurchaseschemePretenderingCompanyRuleById } 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 secondLevelOrganTree from '@views/common/user/organizationTree/secondLevelOrganTree-dialog'// 所属二级公司-弹框
import categoryTreeDialog from '@/views/common/tools/categoryTree-dialog'// 采购品类-弹框
import projectManagerDialog from '@/views/common/user/projectManager-dialog'// 代理机构/项目经理-选择弹框
export default {
  name: 'TenderPlanFirmClassAllotRuleAdd',
  components: {
    money,
    subsidiaryOrganDialog,
    secondLevelOrganTree, // 所属二级公司-弹框
    'category-tree-dialog': categoryTreeDialog, // 采购品类-弹框
    projectManagerDialog
  },
  mixins: [tenderPlan, tenderPlanAllotMixin],
  data() {
    return {
      projectManagerDialogData: {
        departId: this.$store.getters.info.companyId, // 机构id
        dialog: false, // 展示状态
        title: '项目经理', // 标题
        departRoleName: '11', // 10采购人 11代理机构(项目经理)
        materialId: [] // Id去重列表
      },
      // 招标人
      buysersDialog: {
        dialog: false
      },
      // 弹窗-展示/隐藏状态 true 展示/false 隐藏
      dialogData: {
        title: '采购品类',
        dialog: false, // 采购品类
        selectCodes: [] // 当前选中的采购品类Code
      },
      allot: '00', // 00 公司计划管理员 01 部门计划管理员
      formData: {
        repartitionLevel: '00', // 分配层级 00 业务部门
        managerName: '', // 项目经理
        managerId: '', // 项目经理code
        departmentName: '', // 业务部门名称
        departmentId: '', // 业务部门id
        priority: '', // 优先级
        budgetAmountUpper: '', // 概算金额（元）最大值
        budgetAmountLower: '', // 概算金额（元） 最小值
        buysersName: '', // 招标人
        buysersId: '', // 招标人Code
        vfpUnitName: '', // 所属二级公司
        vfpUnitId: '', // 所属二级公司ID
        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: {
    // 分配层级
    'formData.repartitionLevel'(val) {
      if (val === '00') { // 业务部门
        this.formData.managerName = '' // 项目经理
        this.formData.managerId = ''// 项目经理code
      } else if (val === '01') { // 项目经理
        this.formData.purchaseClassName = ''// 采购品类
        this.formData.purchaseClass = ''// 采购品类编码
        this.formData.vfpUnitName = ''// 所属二级公司
        this.formData.vfpUnitId = ''// 所属二级公司ID
      }
    },
    // 采购品类Code
    'formData.purchaseClass'(val) {
      this.dialogData.selectCodes = val ? val.split(',') : []// 前选中的采购品类Code
    }
  },
  created() {
    // 判断是否有id 如果有则是编辑
    if (this.$route.query.id) {
      this.initData(this.$route.query.id)// 初始化数据
    }
    // 根据departId获取所属公司的所有下级部门信息
    this.getDepartInfoList()
    // 招标计划-字典信息
    this.initPurchaseSchemeWebTypeGroup()
  },
  methods: {
    // 初始化数据
    initData(id) {
      getPretPurchaseschemePretenderingCompanyRuleById(id).then(res => {
        this.formData = res.data
      })
    },
    // 所属公司二级公司-子组件返回已选所属公司
    checkedNodes(CheckedNodes) {
      this.formData.vfpUnitName = CheckedNodes.companyName // 所属二级公司名称
      this.formData.vfpUnitId = CheckedNodes.companyId // 所属二级公司id
    },
    // 选择/清空所属公司 00选择 01清空
    handelvfpUnitName(type) {
      if (type === '00') {
        this.$refs.secondLevelOrganTree.openDialog()
      } else {
        this.formData.vfpUnitName = ''
        this.formData.vfpUnitId = ''
      }
    },
    // 保存接口信息
    saveData() {
      const data = {
        ...this.formData
      }
      savePretPurchaseschemePretenderingCompanyRule(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(() => {
          })
        }
      })
    },
    // 业务部门
    departmentChange(data) {
      this.purchasesScheOperatingDepartmentList.map(item => {
        if (data === item.departId) {
          this.formData.departmentName = item.departName // 业务部门名称
          this.formData.departmentId = item.departId // 业务部门id
        }
      })
    },
    // 选择要分配的项目经理
    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/tenderPlanFirmClassAllotRule-list'
      })
    }
  }
}
</script>

<style scoped>

</style>
