<template>
    <div>
      <el-table border v-loading="loading" :data="customerContactsList" >
        <el-table-column label="ID" align="center" prop="contactsId" />
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="手机" align="center" prop="mobile" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
    </div>
  </template>
  
  <script>
  import { listCustomerContacts, getCustomerContacts, delCustomerContacts, addCustomerContacts, updateCustomerContacts, exportCustomerContacts } from "@/api/crm/customerContacts";
  export default {
    name: "CustomerContacts",
    data() {
      return {
        customerId:null,
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户联系人表格数据
        customerContactsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerId: null,
          name: null,
          mobile: null,
          email: null,
          weixin: null,
          address: null,
          createUserId: null,
          isDel: null,
          companyId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          customerId: [
            { required: true, message: "客户ID不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "姓名不能为空", trigger: "blur" }
          ],
          mobile: [
            { required: true, message: "手机号不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
     
    },
    methods: {
        getData(customerId){
            this.customerContactsList=[];
            this.customerId=customerId;
            this.queryParams.customerId=customerId
            this.getList();
        },
        /** 查询客户联系人列表 */
        getList() {
            this.loading = true;
            listCustomerContacts(this.queryParams).then(response => {
            this.customerContactsList = response.rows;
            this.total = response.total;
            this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
            contactsId: null,
            customerId: null,
            name: null,
            mobile: null,
            email: null,
            weixin: null,
            address: null,
            remark: null,
            createUserId: null,
            createTime: null,
            updateTime: null,
            isDel: null,
            companyId: 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.contactsId)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加客户联系人";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const contactsId = row.contactsId || this.ids
            getCustomerContacts(contactsId).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改客户联系人";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
            if (valid) {
                this.form.customerId=this.customerId
                if (this.form.contactsId != null) {
                updateCustomerContacts(this.form).then(response => {
                    if (response.code === 200) {
                    this.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                    }
                });
                } else {
                addCustomerContacts(this.form).then(response => {
                    if (response.code === 200) {
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                    }
                });
                }
            }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const contactsIds = row.contactsId || this.ids;
            this.$confirm('是否确认删除客户联系人编号为"' + contactsIds + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return delCustomerContacts(contactsIds);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有客户联系人数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return exportCustomerContacts(queryParams);
            }).then(response => {
                this.download(response.msg);
            }).catch(function() {});
        }
    }
  };
  </script>
  