<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"
    />
    <index-record
      :isShow="isQShow"
      :curObj="curObj"
      @close="closeQPopup"
    />
    <state-edit
      :isShow="isStateShow"
      :curObj="curObj"
      @close="closeStatePopup"
    />
  </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 StateEdit from './components/popup/StateEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'index',
  components: {
    SearchForm,
    PopupEdit,
    StateEdit,
    IndexRecord,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'cust.admtradelist',
      searchForm: null,
      isDtlShow: false,
      curObj: {},
      isQShow: false,
      isAShow: false,
      isStateShow: 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: 'trade_state', type: 'tag', tags: arrToObj(this.$dictData.trade_state), tagTypeObj: {'1': 'success', '2': 'danger'} },
        { label: '状态描述', prop: 'trade_state_des'},
        { 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: 'check_state', fullShow: true, type: 'flag', flags: arrToObj(this.$dictData.check_state) },
        { 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: 230, type: 'handle2', operations:
          [
            { label: '维护', func: this.openStatePopup, btnType: 'info' },
            { 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()
      }
    },
    openStatePopup (row) {
      if (row && row.id) {
        this.curObj = row
      } else {
        this.curObj = {}
      }
      this.isStateShow = true
    },
    closeStatePopup (obj) {
      this.isStateShow = 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>
