<template>
  <div>
    <xy-dialog :is-show.sync="isShow" type="form" :title="type === 'add' ? '新增在线客服' : '编辑在线客服'" :form="form"
      :rules="rules" ref="dialog" @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>姓名：
          </div>
          <div class="xy-table-item-content">
            <el-input clearable placeholder="请填写姓名" v-model="form.name" style="width: 300px;" />
          </div>
        </div>
      </template>
      <template v-slot:nickname>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>昵称：
          </div>
          <div class="xy-table-item-content">
            <el-input clearable placeholder="请填写昵称" v-model="form.nickname" style="width: 300px;" />
          </div>
        </div>
      </template>
      <template v-slot:phone>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>电话：
          </div>
          <div class="xy-table-item-content">
            <el-input clearable placeholder="请填写电话" v-model="form.phone" style="width: 300px;" />
          </div>
        </div>
      </template>
      <template v-slot:wechat>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>微信号：
          </div>
          <div class="xy-table-item-content">
            <el-input clearable placeholder="请填写微信" v-model="form.wechat" style="width: 300px;" />
          </div>
        </div>
      </template>
      <template v-slot:state>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>状态：
          </div>
          <div class="xy-table-item-content">
            <el-select clearable placeholder="请选择状态" v-model="form.state" style="width: 300px;">
              <el-option v-for="item in states" :value="item.value" :label="item.label" :key="item.value"></el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:sort_number>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>排序：
          </div>
          <div class="xy-table-item-content">
            <el-input clearable placeholder="请填写排序" v-model="form.sort_number" style="width: 300px;" />
          </div>
        </div>
      </template>
      <template v-slot:wechat_code>
        <div class="xy-table-item">
          <div class="xy-table-item-label">微信：
          </div>
          <div class="xy-table-item-content">
            <div v-if="form.wechat_code" style="position: relative;">
              <img :src="form.wechat_code" class="avatar">
              <Button shape="circle" icon="md-close" type="error" size="small" class="img__delete"
                @click="form.wechat_code = ''"></Button>
            </div>
            <el-upload v-else accept="picture" :limit="1" class="avatar-uploader" list-type="picture-card"
              :action="action" :show-file-list="true" :on-error="uploadFailCode" :on-success="uploadSuccessCode"
              :before-upload="uploadBeforeCode">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div style="font-size: 10px;white-space: nowrap;word-break: keep-all">建议尺寸： 宽度750*高度400</div>
          </div>
        </div>
      </template>
      <template v-slot:company_wechat_code>
        <div class="xy-table-item">
          <div class="xy-table-item-label">企业微信：
          </div>
          <div class="xy-table-item-content">
            <div v-if="form.company_wechat_code" style="position: relative;">
              <img :src="form.company_wechat_code" class="avatar">
              <Button shape="circle" icon="md-close" type="error" size="small" class="img__delete"
                @click="form.company_wechat_code = ''"></Button>
            </div>
            <el-upload v-else accept="picture" :limit="1" class="avatar-uploader" list-type="picture-card"
              :action="action" :show-file-list="true" :on-error="uploadFail" :on-success="uploadSuccess"
              :before-upload="uploadBefore">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div style="font-size: 10px;white-space: nowrap;word-break: keep-all">建议尺寸： 宽度750*高度400</div>
          </div>
        </div>
      </template>
    </xy-dialog>
  </div>
</template>

<script>
  import {
    store,
    show,
    update
  } from '@/api/marketing/service'
  import {
    Message
  } from 'element-ui'
  import axios from 'axios'
  export default {
    props: {
      systems: {
        type: Array
      }
    },
    data() {
      return {
        id: '',
        type: 'add',
        isShow: false,
        action: `${process.env.VUE_APP_BASE_API}api/admin/upload-file`,
        states: [{
            label: '禁用',
            value: 0
          },
          {
            label: '启动',
            value: 1
          }
        ],

        form: {
          name: '',
          phone: '',
          nickname: "",
          wechat: "",
          wechat_code: '',
          company_wechat_code: '',
          sort_number: '',
          state: '',
        },
        rules: {
          name: [{
            required: true,
            message: "请填写名称"
          }],
          phone: [{
            required: true,
            message: "请填写联系电话"
          }],
          state: [{
            required: true,
            message: "请选择状态"
          }],
          sort_number: [{
            required: true,
            message: "请填写排序"
          }],
          wechat_code: [{
            required: true,
            message: "请选择图标"
          }]
        }
      }
    },
    methods: {

      //上传
      uploadFailCode(err) {
        console.log(err)
      },
      uploadSuccessCode(response) {
        console.log(response)
        this.form.wechat_code = response.url
      },
      uploadBeforeCode(file) {
        console.log(file)
        if ((file.size / 1000) > 2024) {
          Message({
            type: 'warning',
            message: '上传图片大小超过2M！'
          })
          return false
        }
      },
      //上传
      uploadFail(err) {
        console.log(err)
      },
      uploadSuccess(response) {
        console.log(response)
        this.form.company_wechat_code = response.url
      },
      uploadBefore(file) {
        console.log(file)
        if ((file.size / 1000) > 2024) {
          Message({
            type: 'warning',
            message: '上传图片大小超过2M！'
          })
          return false
        }
      },

      submit() {
        if (this.type === 'add') {
          store(this.form).then(res => {
            Message({
              type: 'success',
              message: '添加在线客服成功'
            })
            this.resetForm();
          })
        } else if (this.type === 'editor') {
          update({
            id: this.id,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '编辑在线客服成功'
            })
            this.resetForm();
          })
        }
      },
      resetForm() {

        this.isShow = false
        this.$emit('refresh')
        Object.keys(this.form).forEach((key) => (this.form[key] = ''));
        this.id = ''
      },

      //获取详情
      async getDetail() {
        const res = await show({
          id: this.id
        })
        Object.assign(this.form, res);
      }
    },
    watch: {
      isShow(val) {
        if (val) {

          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.$refs['dialog'].reset()
          this.id = ''
        }
      }
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    width: 110px;
    height: 110px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #BF617C;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 110px;
    height: 110px;
    line-height: 110px;
    text-align: center;
  }

  .avatar {
    width: 110px;
    display: block;
    border-radius: 6px;
  }

  .img__delete {
    transform: scale(0.7, 0.7);

    position: absolute;
    top: 1px;
    left: 2%;
  }
</style>
