<!-- 商机-列表 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商机名称" prop="boName">
        <el-input
          v-model="queryParams.boName"
          placeholder="请输入商机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机类型" prop="boType">
        <el-select v-model="form.boType" placeholder="请输入商机类型">
          <el-option
            v-for="dict in dict.type.mk_bo_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商机状态" prop="boState">
        <el-select v-model="queryParams.boState" placeholder="请输入商机状态">
          <el-option
            v-for="dict in dict.type.mk_bo_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商机来源" prop="boSource">
        <el-select v-model="queryParams.boSource" placeholder="请输入商机来源">
          <el-option
            v-for="dict in dict.type.mk_bo_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商机阶段" prop="boStage">
        <el-select v-model="queryParams.boStage" placeholder="请输入商机阶段">
          <el-option
            v-for="dict in dict.type.mk_bo_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机负责人" prop="principalName">
        <el-input
          v-model="queryParams.principalName"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mk:bo:basic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mk:bo:basic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mk:bo:basic:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basicList" @selection-change="handleSelectionChange" @cell-dblclick="enterDetails">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商机名称" align="center" prop="boName" />
      <el-table-column label="商机类型" align="center" prop="boType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_type" :value="scope.row.boType"/>
        </template>
      </el-table-column>
      <el-table-column label="商机状态" align="center" prop="boState" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_state" :value="scope.row.boState"/>
        </template>
      </el-table-column>
      <el-table-column label="商机来源" align="center" prop="boSource" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_source" :value="scope.row.boSource"/>
        </template>
      </el-table-column>
      <el-table-column label="商机阶段" align="center" prop="boStage" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_stage" :value="scope.row.boStage"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="医院耗材预估值(万元)" align="center" prop="hosDiscreetValue" />
      <el-table-column label="赢单率" align="center" prop="winningRate" />
      <el-table-column label="销售区域" align="center" prop="marketingArea" />
      <el-table-column label="销售组织" align="center" prop="marketingOrganizingName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="商机负责人" align="center" prop="principalName" />
      <!-- <el-table-column label="创建人" align="center" prop="createBy" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="enterDetails(scope.row)"
            v-hasPermi="['mk:bo:basic:detail']"
          >进入详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mk:bo:basic:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mk:bo:basic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商机基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机名称" prop="boName">
              <el-input v-model="form.boName" placeholder="请输入商机名称" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商机来源" prop="boSource">
              <el-select v-model="form.boSource" placeholder="商机来源">
                <el-option
                  v-for="dict in dict.type.mk_bo_source"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="推荐人" prop="referrer">
              <el-input v-model="form.referrer" placeholder="请输入推荐人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机类型" prop="boType">
              <el-select v-model="form.boType" placeholder="商机类型">
                <el-option
                  v-for="dict in dict.type.mk_bo_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入客户" >
                <el-button slot="append" icon="el-icon-more" @click="refereCustomer"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="医院营收总额" prop="totalHosRevenue">
              <el-select v-model="form.totalHosRevenue" placeholder="请输入医院营收总额">
                <el-option
                  v-for="dict in dict.type.mk_bo_total_revenue"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="医院耗材预估值（万元）" prop="hosDiscreetValue">
              <el-input v-model="form.hosDiscreetValue" placeholder="请输入医院耗材预估值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="商机内容">
          <editor v-model="form.boContent" :min-height="192"/>
        </el-form-item>
        <el-row>
          <el-col :span="6">
            <el-form-item label="销售区域" prop="marketingArea">
              <el-input v-model="form.marketingArea" placeholder="请输入销售区域" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="销售组织" prop="marketingOrganizingName">
              <el-input v-model="form.marketingOrganizingName">
                <el-button slot="append" icon="el-icon-more" @click="refereOrg"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="deptName">
              <el-input v-model="form.deptName">
                <el-button slot="append" icon="el-icon-more" @click="refereDept"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="商机负责人" prop="principalName">
              <el-input v-model="form.principalName">
                <el-button slot="append" icon="el-icon-more" @click="refereStaff"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户参照 -->
    <CustomerRef
      ref="customerSelect"
      @doSubmit="customerSelectionsToInput"
      :single="true"
    />
    <!-- 组织参照 -->
    <OrgRef
      ref="orgSelect"
      @doSubmit="orgSelectionsToInput"
      :single="true"
    />
    <!-- 部门参照 -->
    <DeptRef
      ref="deptSelect"
      @doSubmit="deptSelectionsToInput"
      :single="true"
    />
    <!-- 员工参照 -->
    <StaffRef
      ref="staffSelect"
      @doSubmit="staffSelectionsToInput"
      :single="true"
    />
  </div>
</template>

<script>
import { listBasic, getBasic, addBasic, updateBasic, delBasic } from "@/api/business/spd/bo/basic";
import CustomerRef from '@/views/business/spd/bo/refer/customer/index.vue';
import OrgRef from '@/views/business/spd/bo/refer/org/index.vue';
import DeptRef from '@/views/business/spd/bo/refer/dept/index.vue';
import StaffRef from '@/views/business/spd/bo/refer/staff/index.vue';

export default {
  name: "Basic",
  dicts: ['mk_bo_type','mk_bo_state','mk_bo_stage','mk_bo_source','mk_bo_total_revenue'],
  components: {CustomerRef,OrgRef,DeptRef,StaffRef},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商机基础信息表格数据
      basicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        boName: null,
        boType: null,
        boState: null,
        boSource: null,
        boStage: null,
        totalHosRevenue: null,
        projectApproval: null,
        startWinningRate: null,
        endWinningRate: null,
        biddingDocumentsPass: null,
        startConSignTime: null,
        endConSignTime: null,
        marketingOrganizingName: null,
        marketingArea: null,
        deptName: null,
        createBy: null,
        startCreateTime: null,
        endCreateTime: null,
      },
      // 表单参数
      form: {},
      //表单校验
      rules: {
        boSource: [
          { required: true, message: "商机来源不能为空", trigger: "blur" }
        ],
        boType: [
          { required: true, message: "商机类型不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        totalHosRevenue: [
          { required: true, message: "医院营收总额不能为空", trigger: "blur" }
        ],
        hosDiscreetValue: [
          { required: true, message: "医院耗材预估值不能为空", trigger: "blur" }
        ],
        boContent: [
          { required: true, message: "商机内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  //监听属性
  watch:{
    //阶段
    'form.totalHosRevenue':{
      handler(newName, oldName) {
        switch(newName) {
          //5000万以下
          case '0':
            this.form.hosDiscreetValue = 5000 * 0.02;
            break;
          //5000万-1亿
          case '1':
            this.form.hosDiscreetValue = 10000 * 0.02;
            break;
          //1亿-2亿
          case '2':
            this.form.hosDiscreetValue = 20000 * 0.02;
            break;
          //2亿-5亿
          case '3':
            this.form.hosDiscreetValue = 50000 * 0.02;
            break;
          //5亿-10亿
          case '4':
            this.form.hosDiscreetValue = 100000 * 0.02;
            break;
          //10亿以上
          case '5':
            this.form.hosDiscreetValue = 100000 * 0.02;
            break;
        }
      },
      immediate: true,
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商机基础信息列表 */
    getList() {
      this.loading = true;
      listBasic(this.queryParams).then(response => {
        this.basicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        boSource: null,
        boState: null,
        boStage: null,
        boName: null,
        referrer: null,
        boType: null,
        customerName: null,
        totalHosRevenue: null,
        hosDiscreetValue: null,
        boContent: null,
        marketingOrganizing: null,
        marketingOrganizingName: null,
        marketingArea: null,
        dept: null,
        deptName: null,
        principal: null,
        principalName: null,
        delFlag: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商机基础信息";
    },
    /** 进入详情按钮操作 */
    enterDetails(row) {
      console.log('列数据:',row);
      this.$router.push(`/basic/spd/bo/basic/details/${row.id}`);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBasic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商机基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBasic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商机基础信息编号为"' + ids + '"的数据项？').then(function() {
        return delBasic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/basic/export', {
        ...this.queryParams
      }, `basic_${new Date().getTime()}.xlsx`)
    },
    // 触发客户参照列表
    refereCustomer() {
      this.$refs.customerSelect.init()
    },
    //客户参照列表选择后
    customerSelectionsToInput (selections) {
      this.form.customer = selections[0].id;
      this.form.customerName = selections[0].name;
    },
    // 触发组织参照列表
    refereOrg() {
      this.$refs.orgSelect.init()
    },
    //组织参照列表选择后
    orgSelectionsToInput (selections) {
      this.form.marketingOrganizing = selections[0].deptId;
      this.form.marketingOrganizingName = selections[0].deptName;
    },
    // 触发部门参照列表
    refereDept() {
      this.$refs.deptSelect.init()
    },
    //部门参照列表选择后
    deptSelectionsToInput (selections) {
      this.form.dept = selections[0].deptId;
      this.form.deptName = selections[0].deptName;
    },
    // 触发员工参照列表
    refereStaff() {
      this.$refs.staffSelect.init()
    },
    //员工参照列表选择后
    staffSelectionsToInput (selections) {
      this.form.principal = selections[0].userId;
      this.form.principalName = selections[0].nickName;
    }
  }
};
</script>
