<template>
  <div class="app-container">
    <search-form
      :list-loading="listLoading"
      @change="searchHandle"
    />
    <table-list
      :list-loading="listLoading"
      :data="tableData2"
      :columns="listConfig"
      :current-page="currentPage"
      :page-size="pageSize"
      :total-records="totalRecords"
      @currentChange="pageHandle"
      @sizeChange="sizeChange"
      :operationsDefaultLength="6"
      :insertSlotArr="[7]"
    >
      <div slot="OI7">
        <el-table-column
          width="140"
          label="保护期止"
          align="center"
        >
          <template slot-scope="scope">
            <template v-if="scope.row.report_state === '1' && scope.row.report_step === '1'">
              <div v-if="+new Date() > +new Date(scope.row.lock_at)">
                <div style="color: #f00">{{scope.row.lock_at}}</div>
                <el-tag type="danger" size="mini">已过期</el-tag>
              </div>
              <div v-else-if="(+new Date(scope.row.lock_at) - +new Date()) < 86400000">
                <div style="color: #e6a23c">{{scope.row.lock_at}}</div>
                <el-tag type="warning" size="mini">1天内过期</el-tag>
              </div>
              <div v-else>
                <div>{{scope.row.lock_at}}</div>
              </div>
            </template>
            <template v-else>
              <div>{{scope.row.lock_at}}</div>
            </template>
          </template>
        </el-table-column>
      </div>
    </table-list>
    <!-- :isAdd="true"
      @add="openPopup" -->
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
    <index-record
      :isShow="isQShow"
      :curObj="curObj"
      @close="closeQPopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Index'
import IndexRecord from './components/popup/IndexRecord'
import PopupEdit from './components/popup/IndexEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    IndexRecord,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'cust.admreportlist',
      searchForm: null,
      isDtlShow: false,
      curObj: {},
      isQShow: false,
      isAShow: false,
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        item.createBy = item.create_user ? item.create_user.nickname : '-'
        item.newRecord = `${item.record_estate_name ? item.record_estate_name : '-'}${item.record_protect_at && item.record_protect_at !== '1970-01-01' ? '(' + item.record_protect_at + ')' : ''}-${item.record_remark ? item.record_remark : ''}`
      })
      return arr
    }
  },
  created() {},
  mounted() {
    this.listConfig = {
      rows: [
        { label: '报备人', prop: 'createBy' },
        { label: '姓名', prop: 'name' },
        { label: '性别', prop: 'sex', type: 'tag', tags: arrToObj(this.$dictData.sex), tagTypeObj: {'male': 'primary', 'female': 'danger'} },
        { label: '电话', prop: 'phone' },
        { label: '客户状态', prop: 'report_state', type: 'tag', tags: arrToObj(this.$dictData.report_state	), tagTypeObj: {'1': 'success', '2': 'warning', '3': 'danger'}},
        { label: '报备进度', prop: 'report_step', type: 'tag', tags: arrToObj(this.$dictData.report_step	), tagTypeObj: {'1': 'info', '2': 'success', '3': 'success'} },
        { label: '报备楼盘', prop: 'estate_name'},
        { label: '报备时间', prop: 'report_at' },
        { label: '到访时间', prop: 'visit_at' },
        { label: '创建时间', prop: 'create_at' },
        { label: '备注', prop: 'remark', fullShow: true },
        { label: '操作', width: 90, type: 'handle2', operations:
          [
            { label: '报备详情', func: this.openQPopup, btnType: 'primary' },
            // { label: '修改状态', func: this.openPopup, btnType: 'warning' },
            // { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    openQPopup (row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isQShow = true
    },
    closeQPopup (obj) {
      this.isQShow = false
      if (obj) {
        this.fetchData()
      }
    },
    delHandle(row) {
      this.$msg(`您确定要删除该客户吗？`, 'confirm', () => {
        this.$api.user.admcustomerdel({
          id: row.id
        }).then(data => {
          this.$msgs(`已删除！`)
          this.fetchData()
        })
      }, null, true)
    },
    openPopup(row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isDtlShow = true
    },
    closePopup(obj) {
      this.isDtlShow = false
      if (obj) {
        this.fetchData()
      }
    }
  }
}
</script>
