<template>
  <section class="List">
    <ai-list>
      <ai-title slot="title" title="土地流转经营" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="toAdd('')">添加</el-button>
            <ai-select v-model="select.circulateType" @change="onChange" placeholder="流转形式" :selectList="$dict.getDict('landCirculateType')"></ai-select>
          </template>
          <template #right>
            <el-input size="small" placeholder="查询合同编号/流出方/承接方/证件号码" v-model="search.contractNo" clearable @clear="page.current = 1, search.contractNo = '',getTableData()" 
            suffix-icon="iconfont iconSearch" v-throttle="() => {(page.current = 1), getTableData();}"/>
            <!-- 导入导出 -->
            <ai-import :instance="instance" :dict="dict" importUrl="/applandcirculateinfo/import" url="/applandcirculateinfo/downloadTemplate" name="土地流转经营信息" @success="getTableData(), $message.success('导入成功!')">
              <el-button size="small">导入</el-button>
            </ai-import>
            <ai-download :instance="instance" url="/applandcirculateinfo/export" :params="param" fileName="土地流转经营信息"
                         :disabled="tableData.length == 0">
              <el-button size="small">导出</el-button>
            </ai-download>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作"  align="center">
            <template slot-scope="{ row }">
              <el-button type="text" @click="toAdd(row.id)">详情</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "List",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  data() {
    return {
      search: {contractNo: ""},
      select: {circulateType: ''},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      readType:'',
      areaId: '',
    }
  },
  computed: {
    colConfigs() {
      return [
        { prop: "landOutflowName", label: '土地流出方', align: "center", width: "180px", },
        { prop: "landArea", label: '土地面积/㎡', align: "center", width: "180px", },
        { prop: "circulateType", label: '流转形式', align: "center", width: "180px", dict: "landCirculateType" },
        { prop: "landUndertakeName", label: '土地承接方', align: "center", width: "180px", },
        { prop: "landUndertakeNumber", label: '承接方证件号码', align: "center", width: "180px", },
        { prop: "contractNo", label: '合同编号', align: "center", width: "180px", },
        { prop: "endDate", label: '有效期限', align: "center", width: "180px", },
        { slot: "options" ,},
      ]
    },
    ...mapState(['user']),
    param() {
      return {
        ...this.select,
        ...this.search,
      }
    }
  },
  created() {
    this.$dict.load('landCirculateType')
    this.getTableData()
    this.areaId = this.user.info.areaId
  },
  methods: {
    getTableData() {
      this.instance.post("/applandcirculateinfo/list", null, {
        params: {...this.page, ...this.search,...this.select}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    onChange(v) {
      this.tableData = []
      this.select.circulateType = v;
      this.getTableData()
    },
    toAdd(id) {
      this.$emit('change', {
        type: 'Add',
        params: {
          id: id || ''
        }
      })
    },
    handleDelete(id) {
      this.$confirm("是否要删除？").then(() => {
        this.instance.post(`/applandcirculateinfo/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success("删除成功")
              this.getTableData()
            }
          })
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.List {
  height: 100%;
  background: #f3f4f5;
}
</style>