<template>
  <div class="app-container">
    <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="编号" align="center" prop="code" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gander" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="职务" align="center" prop="position" />
      <el-table-column label="决策力" align="center" prop="power" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="家庭地址" align="center" prop="address" />
      <!-- <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="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="mail" />
      <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="jobTitle" />
      <el-table-column label="任职科室" align="center" prop="section" />
      <el-table-column label="上级联系人" align="center" prop="superiorContact" />
      <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="操作" 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="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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          基本信息
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机id" prop="boId" v-if="source == 'Contact'">
              <el-input v-model="form.boId" placeholder="请输入商机id" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gander">
              <el-select v-model="form.gander" placeholder="请输入性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :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="customerName">
              <el-input v-model="form.customerName" placeholder="请输入所属客户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人分类" prop="contactClassification">
              <el-input v-model="form.contactClassification" placeholder="请输入联系人分类" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <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-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="籍贯" prop="birthplace">
              <el-input v-model="form.birthplace" placeholder="请输入籍贯" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务爱好" prop="hobby">
              <el-input v-model="form.hobby" placeholder="请输入业务爱好" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" placeholder="请输入状态">
                <el-option
                  v-for="dict in dict.type.mk_bo_contact_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          工作信息
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="上级联系人" prop="superiorContact">
              <el-input v-model="form.superiorContact" placeholder="请输入上级联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任职科室" prop="section">
              <el-select v-model="form.section" placeholder="请输入任职科室">
                <el-option
                  v-for="dict in dict.type.mk_bo_section"
                  :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="position">
              <el-select v-model="form.position" placeholder="请输入职务">
                <el-option
                  v-for="dict in dict.type.mk_bo_position"
                  :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="jobTitle">
              <el-select v-model="form.jobTitle" placeholder="请输入职称">
                <el-option
                  v-for="dict in dict.type.mk_bo_job_title"
                  :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="power">
              <el-select v-model="form.power" placeholder="请输入决策力">
                <el-option
                  v-for="dict in dict.type.mk_bo_power"
                  :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="support">
              <el-select v-model="form.support" placeholder="请输入支持度">
                <el-option
                  v-for="dict in dict.type.mk_bo_support"
                  :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="fieldExpertise">
              <el-select v-model="form.fieldExpertise" placeholder="请输入擅长领域">
                <el-option
                  v-for="dict in dict.type.mk_bo_field_expertise"
                  :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="decisionMaker">
              <el-select v-model="form.decisionMaker" placeholder="请输入关键决策人">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
          </el-col>
        </el-row>
        <el-row>
          联系信息
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮件" prop="mail">
              <el-input v-model="form.mail" placeholder="请输入邮件" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入家庭地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <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-col>
          <el-col :span="8">
            <el-form-item label="最佳拜访地点" prop="visitPlace">
              <el-input v-model="form.visitPlace" placeholder="请输入最佳拜访地点" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
          </el-col>
        </el-row>
        <el-row>
          其它信息
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="部门名称" prop="departmentName">
              <el-input v-model="form.departmentName" placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售区域" prop="area">
              <el-input v-model="form.area" placeholder="请输入销售区域" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售组织" prop="organization">
              <el-input v-model="form.organization" placeholder="请输入销售组织" />
            </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>
  </div>
</template>

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

export default {
  name: "contactList",
  props:["source","bo"],
  dicts: ['sys_user_sex','mk_bo_contact_state','mk_bo_section','mk_bo_position','mk_bo_job_title','mk_bo_power','mk_bo_support','mk_bo_field_expertise','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人管理表格数据
      contactList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boId: null,
        name: null,
        code: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        gander: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "所属客户名称不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        section: [
          { required: true, message: "任职科室不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "职务不能为空", trigger: "blur" }
        ],
        jobTitle: [
          { required: true, message: "职称不能为空", trigger: "blur" }
        ],
        power: [
          { required: true, message: "决策力不能为空", trigger: "blur" }
        ],
        decisionMaker: [
          { required: true, message: "关键决策人不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          // { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '请输入正确的号码格式', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.queryParams.boId = this.bo.id;
    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();
      if(this.bo.id){
        this.form.boId = this.bo.id;
      }
      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>
