<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="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="部门名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="departmentCode">
        <el-input
          v-model="queryParams.departmentCode"
          placeholder="请输入部门编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售区域" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入销售区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售组织" prop="organization">
        <el-input
          v-model="queryParams.organization"
          placeholder="请输入销售组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最佳拜访地点" prop="visitPlace">
        <el-input
          v-model="queryParams.visitPlace"
          placeholder="请输入最佳拜访地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最佳拜访时间" prop="visitTime">
        <el-date-picker clearable
          v-model="queryParams.visitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最佳拜访时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="家庭地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入家庭地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮件" prop="mail">
        <el-input
          v-model="queryParams.mail"
          placeholder="请输入邮件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键决策人" prop="decisionMaker">
        <el-input
          v-model="queryParams.decisionMaker"
          placeholder="请输入关键决策人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="擅长领域" prop="fieldExpertise">
        <el-input
          v-model="queryParams.fieldExpertise"
          placeholder="请输入擅长领域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支持度" prop="support">
        <el-input
          v-model="queryParams.support"
          placeholder="请输入支持度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="决策力" prop="power">
        <el-input
          v-model="queryParams.power"
          placeholder="请输入决策力"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职称" prop="jobTitle">
        <el-input
          v-model="queryParams.jobTitle"
          placeholder="请输入职称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职务" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任职科室" prop="section">
        <el-input
          v-model="queryParams.section"
          placeholder="请输入任职科室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级联系人" prop="superiorContact">
        <el-input
          v-model="queryParams.superiorContact"
          placeholder="请输入上级联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务爱好" prop="hobby">
        <el-input
          v-model="queryParams.hobby"
          placeholder="请输入业务爱好"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="birthplace">
        <el-input
          v-model="queryParams.birthplace"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker clearable
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系人分类" prop="contactClassification">
        <el-input
          v-model="queryParams.contactClassification"
          placeholder="请输入联系人分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入所属客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gander">
        <el-input
          v-model="queryParams.gander"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          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="['system:contact: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="['system:contact: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="['system:contact:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:contact:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商机id" align="center" prop="boId" />
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="部门编码" align="center" prop="departmentCode" />
      <el-table-column label="销售区域" align="center" prop="area" />
      <el-table-column label="销售组织" align="center" prop="organization" />
      <el-table-column label="最佳拜访地点" align="center" prop="visitPlace" />
      <el-table-column label="最佳拜访时间" align="center" prop="visitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.visitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家庭地址" align="center" prop="address" />
      <el-table-column label="邮件" align="center" prop="mail" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="关键决策人" align="center" prop="decisionMaker" />
      <el-table-column label="擅长领域" align="center" prop="fieldExpertise" />
      <el-table-column label="支持度" align="center" prop="support" />
      <el-table-column label="决策力" align="center" prop="power" />
      <el-table-column label="职称" align="center" prop="jobTitle" />
      <el-table-column label="职务" align="center" prop="position" />
      <el-table-column label="任职科室" align="center" prop="section" />
      <el-table-column label="上级联系人" align="center" prop="superiorContact" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="业务爱好" align="center" prop="hobby" />
      <el-table-column label="籍贯" align="center" prop="birthplace" />
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人分类" align="center" prop="contactClassification" />
      <el-table-column label="所属客户名称" align="center" prop="customerName" />
      <el-table-column label="所属客户编码" align="center" prop="customerCode" />
      <el-table-column label="性别" align="center" prop="gander" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:contact:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:contact: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商机id" prop="boId">
          <el-input v-model="form.boId" placeholder="请输入商机id" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门编码" prop="departmentCode">
          <el-input v-model="form.departmentCode" placeholder="请输入部门编码" />
        </el-form-item>
        <el-form-item label="销售区域" prop="area">
          <el-input v-model="form.area" placeholder="请输入销售区域" />
        </el-form-item>
        <el-form-item label="销售组织" prop="organization">
          <el-input v-model="form.organization" placeholder="请输入销售组织" />
        </el-form-item>
        <el-form-item label="最佳拜访地点" prop="visitPlace">
          <el-input v-model="form.visitPlace" placeholder="请输入最佳拜访地点" />
        </el-form-item>
        <el-form-item label="最佳拜访时间" prop="visitTime">
          <el-date-picker clearable
            v-model="form.visitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最佳拜访时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家庭地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入家庭地址" />
        </el-form-item>
        <el-form-item label="邮件" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮件" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="关键决策人" prop="decisionMaker">
          <el-input v-model="form.decisionMaker" placeholder="请输入关键决策人" />
        </el-form-item>
        <el-form-item label="擅长领域" prop="fieldExpertise">
          <el-input v-model="form.fieldExpertise" placeholder="请输入擅长领域" />
        </el-form-item>
        <el-form-item label="支持度" prop="support">
          <el-input v-model="form.support" placeholder="请输入支持度" />
        </el-form-item>
        <el-form-item label="决策力" prop="power">
          <el-input v-model="form.power" placeholder="请输入决策力" />
        </el-form-item>
        <el-form-item label="职称" prop="jobTitle">
          <el-input v-model="form.jobTitle" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="职务" prop="position">
          <el-input v-model="form.position" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="任职科室" prop="section">
          <el-input v-model="form.section" placeholder="请输入任职科室" />
        </el-form-item>
        <el-form-item label="上级联系人" prop="superiorContact">
          <el-input v-model="form.superiorContact" placeholder="请输入上级联系人" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="业务爱好" prop="hobby">
          <el-input v-model="form.hobby" placeholder="请输入业务爱好" />
        </el-form-item>
        <el-form-item label="籍贯" prop="birthplace">
          <el-input v-model="form.birthplace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系人分类" prop="contactClassification">
          <el-input v-model="form.contactClassification" placeholder="请输入联系人分类" />
        </el-form-item>
        <el-form-item label="所属客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入所属客户名称" />
        </el-form-item>
        <el-form-item label="所属客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入所属客户编码" />
        </el-form-item>
        <el-form-item label="性别" prop="gander">
          <el-input v-model="form.gander" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import { listContact, getContact, delContact, addContact, updateContact } from "@/api/business/spd/bo/contact";

export default {
  name: "Contact",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人管理表格数据
      contactList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boId: null,
        createDate: null,
        departmentName: null,
        departmentCode: null,
        area: null,
        organization: null,
        visitPlace: null,
        visitTime: null,
        address: null,
        mail: null,
        telephone: null,
        decisionMaker: null,
        fieldExpertise: null,
        support: null,
        power: null,
        jobTitle: null,
        position: null,
        section: null,
        superiorContact: null,
        state: null,
        hobby: null,
        birthplace: null,
        birthday: null,
        contactClassification: null,
        customerName: null,
        customerCode: null,
        gander: null,
        name: null,
        code: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area: [
          { required: true, message: "销售区域不能为空", trigger: "blur" }
        ],
        organization: [
          { required: true, message: "销售组织不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "家庭地址不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        decisionMaker: [
          { required: true, message: "关键决策人不能为空", trigger: "blur" }
        ],
        power: [
          { required: true, message: "决策力不能为空", trigger: "blur" }
        ],
        jobTitle: [
          { required: true, message: "职称不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "职务不能为空", trigger: "blur" }
        ],
        section: [
          { required: true, message: "任职科室不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "所属客户名称不能为空", trigger: "blur" }
        ],
        gander: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询联系人管理列表 */
    getList() {
      this.loading = true;
      listContact(this.queryParams).then(response => {
        this.contactList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        boId: null,
        delFlag: null,
        updateTime: null,
        updateBy: null,
        createDate: null,
        createBy: null,
        departmentName: null,
        departmentCode: null,
        area: null,
        organization: null,
        visitPlace: null,
        visitTime: null,
        address: null,
        mail: null,
        telephone: null,
        decisionMaker: null,
        fieldExpertise: null,
        support: null,
        power: null,
        jobTitle: null,
        position: null,
        section: null,
        superiorContact: null,
        state: null,
        hobby: null,
        birthplace: null,
        birthday: null,
        contactClassification: null,
        customerName: null,
        customerCode: null,
        gander: null,
        name: null,
        code: null,
        id: 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 = "添加联系人管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContact(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) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContact(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 delContact(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
