<template>
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['crm:customerContacts:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['crm:customerContacts:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['crm:customerContacts:remove']"
          >删除</el-button>
        </el-col>
      </el-row>
  
      <el-table border v-loading="loading" :data="customerContactsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <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-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text"  size="mini" @click="callNumber(scope.row.customerId,scope.row.contactsId,null,null)">拨号</el-button>
            <el-button type="text"  size="mini" @click="handleSms(scope.row)">短信</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['crm:customerContacts:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['crm:customerContacts: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="120px">
          <el-form-item label="联系人名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入联系人名称" />
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入手机" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" 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>
      <el-dialog :title="addSms.title" :visible.sync="addSms.open" width="600px" append-to-body>
            <add-sms ref="sms" @close="closeSms()"></add-sms>
        </el-dialog>
    </div>
  </template>
  
  <script>
  import addSms from './addSms.vue';
  import { listCustomerContacts, getCustomerContacts, delCustomerContacts, addCustomerContacts, updateCustomerContacts, exportCustomerContacts } from "@/api/crm/customerContacts";
  export default {
    components: {addSms },
    name: "CustomerContacts",
    data() {
      return {
        addSms:{
            open:false,
            title:"发短信"
        },
        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: {
        closeSms(){
            this.addSms.open=false;
            this.getDetails(this.customerId)
        },
        handleSms(row){
            this.addSms.open=true;
            var that=this;
            setTimeout(() => {
                that.$refs.sms.reset(row.customerId,row.mobile);
            }, 500);
            
        },
        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>
  