<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"
      :isAdd="true"
      @add="openPopup"
    >
    </table-list>
    <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.admtradelist',
      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: 'house_type', fullShow: true, type: 'flag', flags: arrToObj(this.$dictData.trade_house_type) },
        { label: '成交店员', prop: 'deal_clerk' },
        { label: '成交楼盘', prop: 'deal_item' },
        { label: '房号', prop: 'house_no' },
        { label: '成交类型', prop: 'deal_type', fullShow: true, type: 'flag', flags: arrToObj(this.$dictData.trade_deal_type) },
        { label: '成交日期', prop: 'deal_at' },
        { label: '客户姓名', prop: 'customer_name' },
        { label: '客户电话', prop: 'customer_phone' },
        { label: '报备渠道', prop: 'report_dept' },
        { label: '面积(㎡)', prop: 'area' },
        { label: '总价(万元)', prop: 'price' },
        { label: '创建时间', prop: 'create_at' },
        { label: '操作', width: 200, type: 'handle2', operations:
          [
            { label: '编辑', func: this.openPopup, btnType: 'success' },
            { label: '成交详情', func: this.openQPopup, btnType: 'primary' },
            { 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.cust.admtradedel({
          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>
