<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-select style="width:100%" clearable v-model="select.business_id" placeholder="请选择业务">
                  <el-option v-for="item in list_bus" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div v-if="stateObj.is_admin">
                <el-select style="width:100%" clearable v-model="select.area_id" placeholder="请选择区域">
                  <el-option v-for="item in list_area" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-date-picker style="width:100%" format="yyyy" value-format="yyyy" v-model="select.year" type="year"
                  placeholder="选择年份">
                </el-date-picker>
              </div>
              <div>
                <el-date-picker style="width:100%" format="MM月" value-format="MM" v-model="select.month" type="month"
                  placeholder="选择月份">
                </el-date-picker>
              </div>
              <div>
                <div>
                  <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
                </div>
                <!-- <div>
                <el-button type="primary" size="small" @click="editIndex('add')">新增</el-button>
              </div> -->
              </div>
            </div>
          </div>
        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" :total="total" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange"
        :table-item="table_item">
        <template v-slot:month>
          <el-table-column align='center' width="120" label="填报月份" header-align="center">
            <template slot-scope="scope">
              <div>
                {{monthArr[parseInt(scope.row.month)-1]}}
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:business_id>
          <el-table-column align='center' width="360" label="填报业务" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.business_id_details && scope.row.business_id_details.length>0">
                <el-tag>{{scope.row.business_id_details[0]['name']}}</el-tag>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:area_id>
          <el-table-column align='center' width="240" label="填报区域" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.area_id_details && scope.row.area_id_details.length>0">
                <el-tag>{{scope.row.area_id_details[0]['name']}}</el-tag>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:fill_data>
          <!-- <el-table-column align='center' label="填报数据" width="600" header-align="center">
            <el-table-column align='center' label="业务类别" width="240" header-align="center">
              <template slot-scope="scope">
                <template
                  v-if="scope.row.id_fill_datas_fill_id_relation && scope.row.id_fill_datas_fill_id_relation.length>0">
                  <template v-for="item in scope.row.id_fill_datas_fill_id_relation">
                    <div class="product-item">{{item.business_type?item.business_type.name:''}}</div>
                  </template>
                </template>
              </template>
            </el-table-column>
            <el-table-column align='center' label="填报数据" width="240" header-align="center">
              <template slot-scope="scope">
                <template
                  v-if="scope.row.id_fill_datas_fill_id_relation && scope.row.id_fill_datas_fill_id_relation.length>0">
                  <template v-for="item in scope.row.id_fill_datas_fill_id_relation">
                    <div class="product-item">{{item.business_data?item.business_data.name:''}}</div>
                  </template>
                </template>
              </template>
            </el-table-column>
            <el-table-column align='center' label="数据值" width="120" header-align="center">
              <template slot-scope="scope">
                <template
                  v-if="scope.row.id_fill_datas_fill_id_relation && scope.row.id_fill_datas_fill_id_relation.length>0">
                  <template v-for="item in scope.row.id_fill_datas_fill_id_relation">
                    <div class="product-item">{{item.value}}</div>
                  </template>
                </template>
              </template>
            </el-table-column>
          </el-table-column> -->
        </template>

        <template v-slot:btns>
          <el-table-column align='center' fixed="right" label="操作" width="240" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small"
                @click="showIndex('show',scope.row.id,scope.row.business_id)">查看</el-button>
              <el-button v-if="stateObj.login_id===scope.row.admin_id || stateObj.is_admin" type="primary" size="small"
                @click="editIndex('editor',scope.row.id,scope.row.business_id)">编辑</el-button>
              <el-popconfirm v-if="stateObj.login_id===scope.row.admin_id || stateObj.is_admin" style="margin:0 10px"
                @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-fill ref="addFill" @refresh="getList"></add-fill>
        <show-fill ref="showFill"></show-fill>
  </div>
</template>

<script>
  import addFill from './components/addFill.vue';
    import showFill from './components/showFill.vue';
  import {
    index,
    destroy
  } from "@/api/system/baseForm.js"
  import state from '@/store/modules/user.js'
  export default {
    components: {
      addFill,
      showFill
    },
    data() {
      return {
        select: {
          name: '',
          table_name: 'fills',
          page: 1,
          page_size: 10,
          business_id: '',
          area_id: '',
          year: '',
          month: ''
        },
        stateObj: {},
        list_bus: [],
        list_area: [],
        monthArr: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        total: 0,
        list: [],
        table_item: [{
          type: 'index',
          width: 50,
          fixed: 'left'
        }, {
          prop: 'year',
          label: '填报年份',
          align: 'center',
          width: 120
        }, {
          prop: 'month',
          label: '填报月份',
          align: 'center',
          width: 120
        }, {
          prop: 'business_id',
          label: '填报业务',
          align: 'center',
          width: 360,
        }, {
          prop: 'area_id',
          label: '填报区域',
          align: 'center',
          width: 120
        }, {
          prop: 'fill_data',
          label: '填报数据',
          align: 'center',
          width: 120
        }, {
          prop: 'admin.name',
          label: '填报人',
          align: 'center',
          width: 120
        }, {

          prop: 'created_at',
          label: '填报时间',
          align: 'center',
          width: 180
        }]

      }
    },
    created() {
      this.stateObj = state.state
      if (this.$route.query.id) {
        this.select.business_id = parseInt(this.$route.query.id)
      }
      if (this.$route.query.area_id) {
        this.select.area_id = parseInt(this.$route.query.area_id)
      }
      this.getBusinessList()
      this.getAreaList()
      this.getList()
    },
    methods: {
      async getBusinessList() {
        const res = await index({
          page_size: 999,
          page: 1,
          table_name: 'businesses',
        })
        this.list_bus = res.data
      },
      async getAreaList() {
        const res = await index({
          page_size: 999,
          page: 1,
          table_name: 'areas',
        })
        this.list_area = res.data
      },
      showIndex(type, id, business_id) {
        if (type == 'show') {
          this.$refs.showFill.id = id
          this.$refs.showFill.business_id = business_id
        }
        this.$refs.showFill.type = type
        this.$refs.showFill.isShow = true
      },
      editIndex(type, id, business_id) {
        if (type == 'editor') {
          this.$refs.addFill.id = id
          this.$refs.addFill.setBusinessId(business_id)
          this.$refs.addFill.business_id = business_id
        }
        this.$refs.addFill.type = type
        this.$refs.addFill.isShow = true
      },
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      async getList() {
        const res = await index({
          page_size: this.select.page_size,
          page: this.select.page,
          table_name: this.select.table_name,
          json_data_fields: ['area_id', 'business_id'],
          filter: [{
            "key": "business_id",
            "op": "eq",
            "value": this.select.business_id ? this.select.business_id : ''
          }, {
            "key": "area_id",
            "op": "eq",
            "value": this.stateObj.is_admin ? '' : this.stateObj.area_id
          }, {
            "key": "year",
            "op": "eq",
            "value": this.select.year ? this.select.year : ''
          }, {
            "key": "month",
            "op": "eq",
            "value": this.select.month ? this.select.month : ''
          }],
        })
        this.list = res.data
        this.total = res.total
      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id,
          table_name: this.select.table_name,
          destroy_relation: ['id_fill_datas_fill_id_relation']
        }).then(response => {
          this.$Message.success('操作成功');
          this.getList()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }

  ::v-deep .product-item {
    margin: 0 -10px;
    padding: 5px 10px;
    border-bottom: 1px solid #EBEEF5;
    height: 50px;
    display: flex;
    align-items: center;
  }

  ::v-deep .el-table td.el-table__cell div .product-item:last-child {
    border-bottom: none;
  }
</style>
