<template>
  <el-row class="el-row-container" v-loading="loading">
    <el-col
      :span="24"
      style="padding: 16px; height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div style="display: flex;">
            <select-tree 
              :placeholder="'年份'" 
              style="width: 250px"
              :treeData="treeData"
              @handlerClick="handlerClick"
              :checkVal="cusareaName"
            ></select-tree>
            <el-input
              style="width: 250px;margin: 0 16px;"
              placeholder="请输入关键字"
              v-model="subsidypolicyTitle"
            />
            <el-button
              type="primary"
              size="small"
              @click="handleSearch"
              >查询</el-button
            >
          </div>
          <div class="top-left">
            <el-button
              type="primary"
              size="small"
              @click="addAssociation"
              >发布新政策
              </el-button
            ></div>
        </el-col>
        <el-col :span="24" class="elrow-main__col-bottom">
          <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="loadData"
            :columns="columns"
            isShowIndex
          >
            <template #styleImg="scope">
              <el-image
                :src="dialogImageUrl + scope.row.previews || ''"
                :preview-src-list="[dialogImageUrl + scope.row.previews || '']"
                slot="error"
                class="image-slot"
                style="width: 100px; height: 50px"
              >
                <div slot="error" class="image-slot"></div>
              </el-image>
            </template>
            <template #operate="scope">
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="editHandler(scope.row)"
                >修改</el-link
              >
              <el-link
              size="small"
              :underline="false"
              type="danger"
              style="margin-right: 10px"
              @click="deleteHandler(scope.row)"
              >删除</el-link
            >
            </template>
          </b-table>
        </el-col>
      </el-card>
    </el-col>
    <new-policy
      :visible.sync="showPolicy"
      :policyType="policyType"
      :areaId="areaId"
      :editRow="editRow"
      @addSuccess="addSuccessHandler"
    />
  </el-row>
</template>

<script>
import { getSubsidyPolicy, deleteSubsidyPolicy } from '@/api/subsidypolicy/index.js'
import SelectTree from '@/components/SelectTree';
import BTable from '@/components/Table/index.vue';
import { getTree } from '@/api/tree.js'
import newPolicy from './components/newPolicy.vue'
import { Message } from 'element-ui'

export default {
  name:'waterSubsidy',
  components: { BTable,newPolicy, SelectTree },
  data() {
    return {
      form: {
        productName: ''
      },
      policyType: 'add',
      subsidypolicyTitle:'',
      loading: false,
      showPolicy: false,
      treeData: [],
      areaId: '',
      cusareaName: '',
      editRow: {},
      dialogImageUrl: '',
      columns: [
        {
          label: '图片',
          prop: 'previews',
          customRender: 'styleImg',
          align: 'center'
        },
        {
          label: '标题',
          prop: 'subsidypolicyTitle',
          customRender: '',
          align: 'center'
        },
        {
          label: '浏览量',
          prop: 'subsidypolicyReadnum',
          customRender: '',
          align: 'center'
        },
        {
          label: '发布人',
          prop: 'subsidypolicyCreatorName',
          customRender: '',
          align: 'center'
        },{
          label: '发布时间',
          prop: 'subsidypolicyCreateddate',
          customRender: '',
          align: 'center'
        },
        {
          label: '操作',
          customRender: 'operate',
          align: 'center'
        }
      ]
    };
  },
  mounted() { 
    this.dialogImageUrl = process.env.VUE_APP_BASE_API
  },
  methods: {
    addSuccessHandler() { 
      this.handleSearch()
    },
    addAssociation() {
      this.policyType = 'add'
      this.showPolicy = true
    },
    async loadData() {
      if (!this.areaId) {
        const treeList = this.$store.state.tree.treeList
        let res = []
        if (treeList.code === '000000') {
          res = treeList
        } else {
          res = await getTree()
          this.$store.dispatch('tree/setTree', res)
        }
        this.treeData = res?.data
        this.cusareaName ||= this.treeData[0].cusareaName
        this.areaId = this.treeData[0].cusareaId
      }
      return this.getWaterrightListHandler()
    },
    getWaterrightListHandler() {
      return getSubsidyPolicy({
        areaIds: this.areaId,
        subsidypolicyTitle: this.subsidypolicyTitle
      })
    },
    handlerClick(value) {
      this.areaId = value?.cusareaId
    },
    handleSearch() {
      this.$refs.tableRef.refresh(true);
    },
    goDetail(row) {
      this.$refs.sourceCodeDetail.open(row.tarcecodeapplyBatchno)
    },
    editHandler(row) {
      this.policyType = 'edit'
      const params = {
        subsidypolicyTitle: row?.subsidypolicyTitle,
        src: this.dialogImageUrl + row?.previews,
        subsidypolicyContent: row?.subsidypolicyContent,
        subsidypolicyId: row?.subsidypolicyId
      }
      this.editRow = params
      this.showPolicy = true
    },
    deleteHandler(row) {
      const { subsidypolicyId } = row
      this.$modal
      .confirm(`是否确认删除`)
      .then(() => {
        deleteSubsidyPolicy({
          subsidypolicyIds: subsidypolicyId
        }).then(() => {
          Message({ message: "删除成功", type: 'success' });
          this.handleSearch()
        })
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row-container{
  height: 100%;
}
.elrow-main__col-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  .top-left {
    height: 100%;
    display: flex;
    align-items: center;
    i {
      background-color: #40d5ec;
      height: 45%;
      width: 5px;
      margin-right: 5px;
    }
  }
}
</style>
