<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input placeholder="请输入姓名、昵称、联系方式、备注" v-model="queryParam.keywords"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="预约来访时间" >
              <a-select ref="select" v-model:value="queryParam.beginTime" placeholder="请选择预约来访时间">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">过去来访</a-select-option>
                <a-select-option value="2">今日来访</a-select-option>
                <a-select-option value="3">即将来访</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="跟进人">
              <j-dict-select-tag placeholder="请选择跟进人" v-model="queryParam.salesCode" dictCode="sys_user,realname,id"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="来访状态">
              <j-dict-select-tag placeholder="请输入来访状态" v-model="queryParam.visitingStatus" dictCode="customer_visiting_status"/>
            </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="校区ID">
              <j-dict-select-tag placeholder="请选择校区ID" v-model="queryParam.schoolDistrictId" dictCode="school_organization,name,id"/>
            </a-form-item>
          </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleSign(record)" v-if="record.visitingStatus === '1'">签到</a>
          <a-divider type="vertical" v-if="record.visitingStatus === '1'"/>
          <a @click="handleRe(record)" v-if="record.visitingStatus === '1'">重新预约</a>
          <a-divider type="vertical" v-if="record.visitingStatus === '1'"/>
          <a @click="handleCancel(record)" v-if="record.visitingStatus === '1'">取消预约</a>
        </span>
      </a-table>
    </div>
    <a-modal :visible="signVisible" title="确认信息" @ok="signOk"  @cancel="signCancel">
      <p>是否确认签到?</p>
    </a-modal>

    <a-modal :visible="reVisible" title="确认信息" @ok="reOk"  @cancel="reCancel">
      <a-row style="display: flex; align-items: center; margin-bottom: 10px;">
            <a-col span="5" style="text-align: right;"><span style="color: tomato; ">*</span>预约来访时间:</a-col>
            <a-col span="1"></a-col>
            <a-col span="17">
                <a-date-picker showTime valueFormat="YYYY-MM-DD HH:mm:ss" :disabledDate="disabledDate"
                    v-model="visitingTime" />
            </a-col>
        </a-row>
        <a-row style="display: flex; align-items: center; margin-bottom: 10px;">
            <a-col span="5" style="text-align: right;"><span style="color: tomato; ">*</span>备注:</a-col>
            <a-col span="1"></a-col>
            <a-col span="17">
                <a-textarea type="textarea" placeholder="请输入备注" v-model="mark" :rows="5" />
            </a-col>
        </a-row>
        <a-row style="display: flex; align-items: center; margin-bottom: 10px;">
            <a-col span="5" style="text-align: right;"><span style="color: tomato; ">*</span>下次跟进时间:</a-col>
            <a-col span="1"></a-col>
            <a-col span="17">
                <a-date-picker showTime valueFormat="YYYY-MM-DD HH:mm:ss" :disabledDate="disabledDate"
                    v-model="nextFollow" />
            </a-col>
        </a-row>
    </a-modal>

    <a-modal :visible="cancelVisible" title="确认信息" @ok="cancelOk"  @cancel="cancelCancel">
      <p>是否取消预约来访?</p>
    </a-modal>

    <customer-visiting-modal ref="modalForm" @ok="modalFormOk"></customer-visiting-modal>
  </a-card>
</template>

<script>

  import { postAction } from '@/api/manage'
  import store from '@/store'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CustomerVisitingModal from './modules/CustomerVisitingModal'
  import moment from 'moment'

  export default {
    name: 'CustomerVisitingList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CustomerVisitingModal
    },
    data () {
      return {
        signVisible: false,
        signId: undefined,
        cancelVisible: false,
        cancelId: undefined,
        reVisible: false,
        reId: undefined,
        cpId: undefined,
        visitingTime: undefined,
        mark: undefined,
        nextFollow: undefined,
        description: '来访管理管理页面',
        // 表头
        columns: [
          {
            title:'学员姓名',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'联系方式',
            align:"center",
            dataIndex: 'contactPhone'
          },
          {
            title:'顾问',
            align:"center",
            dataIndex: 'salesCode_dictText'
          },
          {
            title:'预约来访时间',
            align:"center",
            dataIndex: 'visitingTime'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'markString'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'visitingStatus_dictText'
          },
          {
            title:'校区',
            align:"center",
            dataIndex: 'schoolDistrictId_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/customer/customerVisiting/listAll",
          delete: "/customer/customerVisiting/delete",
          deleteBatch: "/customer/customerVisiting/deleteBatch",
          exportXlsUrl: "/customer/customerVisiting/exportXls",
          importExcelUrl: "customer/customerVisiting/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      disabledDate(current) {
          return current < moment().subtract(1,'day');// 当前时间之前的所有日期都禁用
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'schoolDistrictId',text:'校区ID',dictCode:"school_organization,name,id"})
        fieldList.push({type:'string',value:'cpName',text:'潜客姓名',dictCode:''})
        fieldList.push({type:'string',value:'cpPhone',text:'潜客手机',dictCode:''})
        fieldList.push({type:'datetime',value:'visitingTime',text:'预约日期'})
        fieldList.push({type:'string',value:'salesCode',text:'顾问',dictCode:''})
        fieldList.push({type:'string',value:'visitingStatus',text:'来访状态',dictCode:''})
        fieldList.push({type:'datetime',value:'realVisitingTime',text:'到访日期'})
        fieldList.push({type:'Blob',value:'mark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      },
      handleSign(record) {
        this.signVisible = true;
        this.signId = record.id;
        this.cpId = record.cpId;
      },
      handleCancel(record) {
        this.cancelVisible = true;
        this.cancelId = record.id;
        this.cpId = record.cpId;
      },
      handleRe(record) {
        this.reVisible = true;
        this.reId = record.id;
        this.cpId = record.cpId;
      },
      signOk() {
        let params = {
          id: this.signId,
          visitingStatus: 2,
          cpId: this.cpId,
          updateBy:store.getters.userInfo.id,
        }
        postAction("/customer/customerVisiting/edit", params).then((res) => {
          if (res.success) {
              this.$message.success('编辑成功');
              this.$emit('childReturn',false);
          }
          this.signVisible = false;
          this.loadData();
        })
      },
      cancelOk() {
        let params = {
          id: this.cancelId,
          visitingStatus: 0,
          cpId: this.cpId,
          updateBy:store.getters.userInfo.id,
        }
        postAction("/customer/customerVisiting/edit", params).then((res) => {
          if (res.success) {
              this.$message.success('编辑成功');
              this.$emit('childReturn',false);
          }
          this.cancelVisible = false;
          this.loadData();
        })
      },
      reOk() {
        let params = {
          id: this.reId,
          visitingStatus: 1,
          cpId: this.cpId,
          updateBy:store.getters.userInfo.id,
          visitingTime: this.visitingTime,
          markString: this.mark,
          nextFollowTime: this.nextFollow,
        }
        postAction("/customer/customerVisiting/edit", params).then((res) => {
          if (res.success) {
              this.$message.success('编辑成功');
              this.$emit('childReturn',false);
          }
          this.reVisible = false;
          this.loadData();
        })
      },
      signCancel(){
        this.signVisible = false;
        this.signId = undefined;
        this.cpId = undefined;
      },
      cancelCancel() {
        this.cancelVisible = false;
        this.cancelId = undefined;
        this.cpId = undefined;
      },
      reCancel() {
        this.reVisible = false;
        this.reId = undefined;
        this.cpId = undefined;
      }
      
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>