<template>
  <div class="dashboard-container">
    <el-form :model="peopleform" label-width="78px" ref="peopleform">
      <div class="search-div">
        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item label="粮食类型" class="fourLabel">
                <el-input placeholder="请输入" v-model="peopleform.companyName" clearable>
                </el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="9">
            <div>
              <el-form-item label="指标名称" class="fourLabel" label-width="80px">
                <el-input placeholder="请输入" v-model="peopleform.companyName" clearable>
                </el-input>
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="2">
            <div>
              <el-button class="outButton mgt-3" type="primary" @click="query">
                <i class="iconfont">&#xe67f;</i><span>查询</span>
              </el-button>
              <!-- <el-button class="outButton" icon="el-icon-search"  type="primary">清空</el-button> -->
            </div>
          </el-col>
        </el-row>
      </div>
      <div style="clear:both;border-top:1px dashed #5aaafa;margin-bottom:10px;">
      </div>
      <el-row>
        <div class="addBtnBg">
          <el-button class="outButton" @click="addBtn" type="primary">
            <i class="iconfont">&#xe698;</i><span>新增</span>
          </el-button>
        </div>

      </el-row>
    </el-form>
    <el-row>
      <el-table tooltip-effect="dark" style="width: 100%;" align="center" border stripe highlight-current-rowv-loading="tableLoading" :data="tableData">
        <el-table-column align="center" label="序号" width="60">
          <template slot-scope="scope"><span>{{scope.$index+1}}</span></template>
        </el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" label="粮食类型" prop="reportTypeName">
        </el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" label="指标名称" prop="indicatorName">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">

            <el-tooltip class="item" effect="light" content="修改" placement="top">
              <el-button class="button operate_edit_btn" type="primary" @click.stop="edit(scope.row)">
                <i class="iconfont">&#xe693;</i>
              </el-button>
            </el-tooltip>

            <el-tooltip class="item" effect="light" content="删除" placement="top">
              <el-button class="button operate_delete_btn" type="primary" @click.stop="deleteFn(scope.row)">
                <i class="iconfont"> &#xec21;</i>
              </el-button>
            </el-tooltip>

            <el-tooltip class="item" effect="light" content="查看" placement="top">
              <el-button class="button operate_show_btn" type="primary" @click.stop="see(scope.row)">
                <i class="iconfont">&#xe67e;</i>
              </el-button>
            </el-tooltip>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页开始 -->
      <div class="pagination-container">
        <el-pagination class="right" :current-page.sync="limit.page" :page-size="limit.size" :page-sizes="[10,20,30,40]" :total="limit.total" @size-change="handleChangeSize" @current-change="handleCurrentChangePage" layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-row>

    <el-dialog :title="titleHead" :visible.sync="reportDialog" width="40%">

      <el-form :model="peopleform" :rules="rules" ref="peopleform" label-width="80px" class="demo-ruleForm">
        <el-row>
          <el-col :span="24">
            <el-form-item label="粮食类型" prop="reportTypeName">
              <el-select class="wdh" placeholder="请选择" v-model="peopleform.reportTypeName" @change=chooseLs>
                <el-option v-for="item in lsArr" :key="item.id" :label="item.reportTypeName" :value="item.reportTypeName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="指标名称" prop="indicatorName">
              <el-select class="wdh" placeholder="请选择" v-model="peopleform.indicatorName" @change=chooseZb>
                <el-option v-for="item in indexArr" :key="item.id" :label="item.indName" :value="item.indName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item align="center">
              <el-button v-show='titleHead !== title.see' type="primary" @click="submitForm('peopleform')">
                <i class="iconfont">&#xe683;</i><span>确定</span>
              </el-button>
              <el-button type="primary" @click="reportDialog = false">
                <i class="iconfont">&#xe688;</i><span>关闭</span>
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { bbzbPage, bbzbDelete, selectZb, selectReport, bbzbSave, bbzbUpdate, bbzbFindById } from '@/api/indexManage/index'

export default {
  name: 'Daily',
  data() {
    return {
      ruleForm2: {},
      reportForm: {},
      reportDialog: false,
      titleHead: '',
      title: { add: '新增报表指标', edit: '修改报表指标', see: '查看报表指标' },
      lsArr: [],
      indexArr: [],
      rules: {
        indicatorName: [
          { required: true, message: '请选择指标名称', trigger: 'change' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        reportTypeName: [
          { required: true, message: '请选择粮食类型', trigger: 'change' }
        ],
      },
      tableData3: [{
        index: 1
      }],
      peopleform: {},
      yearArr: [{
        index: 1,
        value: '发布'
      }],
      status: [{
        index: 1,
        dictName: '未提交',
      }, {
        index: 2,
        dictName: '已提交',
      }, {
        index: 3,
        dictName: '审核通过',
      }, {
        index: 4,
        dictName: '驳回',
      }],
      tableData: [{
        index: 1
      }],
      isAdd: true,
      limit: {
        page: 1,
        size: 10,
        total: 0
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  mounted() {
    this.getAll(this.limit, {})
    selectZb().then(res => {
      this.indexArr = res.data.resultList
    })
    selectReport().then(res => {
      this.lsArr = res.data.resultList

    })
  },
  methods: {
    chooseLs(val) {
      this.lsArr.map(item => {
        if (item.reportTypeName === val) {
          this.peopleform.reportTypeId = item.id
        }
      })
    },
    chooseZb(val) {
      this.indexArr.map(item => {
        if (item.indName === val) {
          this.peopleform.indicatorId = item.id
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            bbzbSave(this.peopleform).then(res => {
              if (res.infoCode === 200) {
                this.$message({
                  type: 'success',
                  message: '保存成功'
                })
              } else {
                this.$message({
                  type: 'warning',
                  message: res.data
                })
              }
              this.getAll(this.limit, {})
              this.reportDialog = false
            })

          } else {
            bbzbUpdate(this.peopleform).then(res => {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.getAll(this.limit, {})
              this.reportDialog = false

            })

          }

        }
      })
    },
    getAll(obj, data) {
      bbzbPage(obj, data).then(res => {
        this.tableData = res.data.resultList
        this.limit.total = res.data.total
      })
    },
    query() {
      this.getAll(this.limit, this.peopleform)
    },
    deleteFn(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() => {
        let arr = [];
        arr.push(row.id)
        bbzbDelete(arr).then(res => {
          this.$message({
            type: "success",
            message: '删除成功'
          })
          this.getAll(this.limit, {})
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleChangeSize(value) {
      this.limit.size = value
      this.getAll(this.limit, {})

    },
    handleCurrentChangePage(value) {
      this.limit.page = value
      this.getAll(this.limit, {})
    },
    addBtn() {
      this.isAdd = true
      this.titleHead = this.title.add;
      this.peopleform = {}
      this.$refs['peopleform'].resetFields();
      this.reportDialog = true

    },
    chakan(id) {
      bbzbFindById(id).then(res => {
        this.peopleform = res.data
      })
    },
    edit(item) {
      this.isAdd = false
      this.titleHead = this.title.edit;
      this.reportDialog = true
      this.chakan(item.indicatorId)
    },
    see(item) {
      this.isAdd = false
      this.titleHead = this.title.see;
      this.reportDialog = true
      this.chakan(item.indicatorId)
    }
  }
}
</script>
