<!--认证信息-->
<template>
  <div class="app-container certificationInfo">
    <div class="page-container certificationInfoPage">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="名称或电话号码"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="state">
            <el-select v-model="queryParams.state" clearable placeholder="请选择状态" style="width: 200px">
              <el-option :value="true" label="启用"></el-option>
              <el-option :value="false" label="停用"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="dateRange">
            <el-date-picker
              :clearable="false"
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="创建时间"
              end-placeholder="创建时间"
            ></el-date-picker>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="dialogOpen"
             v-hasPermiRouter="['auth:customer:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="tableList" ref="multipleTable">
          <el-table-column label="平台/企业" align="center" prop="customerName" show-overflow-tooltip/>
          <el-table-column label="昵称" align="center" prop="customerNickname" show-overflow-tooltip width="120"/>
          <el-table-column label="联系人" align="center" prop="contact" show-overflow-tooltip width="120"/>
          <el-table-column label="联系电话" align="center" prop="mobile" show-overflow-tooltip width="200"/>
          <el-table-column label="状态" align="center" prop="state" show-overflow-tooltip width="100">
            <template slot-scope="scope">
              {{scope.row.state?'启用':'停用'}}
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip width="240"/>
          <el-table-column label="创建时间" align="left" prop="createTime" show-overflow-tooltip width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="deptName" width="140"
                           v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(1,scope.row)"
                   v-hasPermiRouter="['auth:customer:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['auth:customer:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--新增弹窗-->
    <el-dialog class="trainingCourseAddDialog" :title='dialogTitle' width="560px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form  v-loading="loading" :rules="dialogRules" :model="dialogForm" ref="dialogForm" label-width="100px">
        <el-form-item label="平台/企业" prop="customerName">
          <el-input
            v-model="dialogForm.customerName"
            maxLength="20"
            placeholder="请输入平台/企业"
            clearable
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="昵称" prop="customerNickname">
          <el-input
            v-model="dialogForm.customerNickname"
            maxLength="10"
            placeholder="请输入昵称"
            clearable
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input
            v-model="dialogForm.contact"
            maxLength="10"
            placeholder="请输入联系人"
            clearable
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input
            v-model="dialogForm.mobile"
            maxLength="11"
            placeholder="请输入11位手机号码"
            clearable
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state" style="width:400px;">
          <el-radio v-model="dialogForm.state" :label="true">启用</el-radio>
          <el-radio v-model="dialogForm.state" :label="false">禁用</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="dialogForm.remark"
            maxLength="20"
            placeholder="请输入备注"
            clearable
            style="width: 400px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { authCustomerList,authCustomerAdd,authCustomerUpdate,authCustomerDelete,authCustomerDetail } from "@/api/serviceCenter/index";
  export default {
    name: 'index',
    data(){
      return{
        tableButtonType:this.hasPermiDom(['auth:customer:edit','auth:customer:detail','auth:customer:del']),
        loading:false,
        optionList:[{label:"启用",value:true},{label:"禁用",value:false}],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          state:null,
        },
        dateRange:[],
        tableList:[],
        total:0,
        //dialog弹层数据
        dialogTitle:"",
        dialogForm:{},
        dialogRules:{
          customerName: [
            { required: true, message: "请输入平台/企业", trigger: "blur" },
            { required: true, message: "请输入平台/企业", validator: this.spaceJudgment, trigger: "blur" }
          ],
          customerNickname: [
            { required: true, message: "请输入昵称", trigger: "blur" },
            { required: true, message: "请输入昵称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          contact: [
            { required: true, message: "请输入联系人", trigger: "blur" },
            { required: true, message: "请输入联系人", validator: this.spaceJudgment, trigger: "blur" }
          ],
          mobile: [
            { required: true, message: "请输入11位手机号码", trigger: "blur" },
            { validator: this.checkPhone, trigger: 'blur' },
            { required: true, message: "请输入11位手机号码", validator: this.spaceJudgment, trigger: "blur" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
            { required: true, message: "请选择状态", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
        dialogType:false,
      }
    },
    created(){
      this.getList();
    },
    mounted(){

    },
    methods:{
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层开启
      dialogOpen(){
        this.dialogFormReset();
        this.$set(this,'dialogTitle','新增');
        this.$set(this,'dialogType',true);
      },
      //弹层确定
      dialogSubmit(){
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            if(this.dialogForm.customerId){
              let obj = JSON.parse(JSON.stringify(this.dialogForm))
              authCustomerUpdate(obj).then(response => {
                this.msgSuccess(response.message)
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }else{
              let obj = JSON.parse(JSON.stringify(this.dialogForm))
              authCustomerAdd(obj).then(response => {
                this.msgSuccess(response.message)
                this.getList();
                this.$set(this,'dialogType',false);
              });
            }
          }
        })
      },
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          state:null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.startTime = this.dateRange[0]+'T00:00:00'
          obj.endTime = this.dateRange[1]+'T23:59:59'
        }else{
          obj.startTime = "";
          obj.endTime = "";
        }
        authCustomerList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'tableList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //预览
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //编辑
          authCustomerDetail({id:row.customerId}).then(response => {
            this.dialogFormReset();
            this.$set(this,'dialogTitle','编辑');
            this.$set(this,'dialogForm',{
              customerId:response.data.customerId,
              customerName:response.data.customerName,
              customerNickname:response.data.customerNickname,
              contact:response.data.contact,
              mobile:response.data.mobile,
              state:response.data.state,
              remark:response.data.remark,
            });
            this.$set(this,'dialogType',true);
          });
        }else if(type == 2){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            authCustomerDelete({customerId:row.customerId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          customerName:'',
          customerNickname:'',
          contact:'',
          mobile:'',
          state:true,
          remark:'',
        });
      },
      //手机号验证
      checkPhone(rule, value, callback) {
        if (!value) {
          return callback(new Error('手机号码不能为空'))
        } else {
          if(value == '19999999999'){
            return callback(new Error('请输入正确的手机号码'))
          }
          const reg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
          if (reg.test(value)) {
            callback()
          } else {
            return callback(new Error('请输入正确的手机号码'))
          }
        }
      },
    },
  }
</script>

<style scoped lang="scss">
  .certificationInfo{
    .certificationInfoPage{

    }
  }
</style>
