<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"
      :isAdd="true"
      @add="openPopup"
    />
    <popup-edit
      :isShow="isDtlShow"
      :curObj="curObj"
      @close="closePopup"
    />
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import SearchForm from './components/searchForm/Compete'
import PopupEdit from './components/popup/CompeteEdit'
import baseTable from '_m/baseTable.js'
export default {
  name: 'price',
  components: {
    SearchForm,
    PopupEdit,
  },
  provide() {
    return {
      parentData: this
    }
  },
  mixins: [baseTable],
  data() {
    return {
      apiStr: 'house.admestatecompetelist',
      searchForm: {},
      isDtlShow: false,
      // noCreated: true,
      curObj: {},
    }
  },
  computed: {
    tableData2() {
      const arr = [...this.tableData]
      arr.map(item => {
        const product_type = item.product_type ? item.product_type.split(',') : []
        const productTypeName = product_type.map(v => {
          return arrToObj(this.$dictData.product_type)[v]
        })
        item.productTypeName = productTypeName.join(',')
      })
      return arr
    }
  },
  created() {
    const query = this.$route.query
    this.searchForm.estate_id = query.id || ''
  },
  mounted() {
    this.listConfig = {
      rows: [
        { label: '排序', prop: 'sort', type: 'input', width: 80},
        { label: '竞品', prop: 'estate_name'},
        { label: '所属区域', prop: 'area_type', type: 'flag', flags: arrToObj(this.$dictData.area_type) },
        { label: '产品类型', prop: 'productTypeName'},
        { label: '更新人', prop: 'update_by' },
        { label: '更新时间', prop: 'update_at' },
        { label: '操作', width: 200, type: 'handle2', operations:
          [
            // { label: '编辑', func: this.openPopup, btnType: 'primary' },
            { label: '保存排序', func: this.saveHandle, btnType: 'primary' },
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    saveHandle (row) {
      this.$api.house.admestatecompeteedit({
        id: row.id,
        compete_id: row.estate_id,
        sort: row.sort,
      }).then(data => {
        this.$msgs('更新成功')
        this.fetchData()
      })
    },
    delHandle(row) {
      this.$msg(`您确定要删除该竞品吗？`, 'confirm', () => {
        this.$api.house.admestatecompetedel({
          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>
