<template>
  <ai-detail>
    <template slot="title">
      <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <AiSidebar :tabTitle="tabList" v-model="currIndex"></AiSidebar>
      <ai-card title="议题信息" v-show="currIndex === 0">
        <template #content>
          <ai-wrapper>
            <ai-info-item label="主题" value="主题"></ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
      <ai-card title="意见征集" v-if="false" v-show="currIndex === 1">
        <template #right>
          <el-button type="primary" size="small" @click="isShowAdd = true">发表意见</el-button>
        </template>
        <template #content>
          <ai-table
            class="detail-table__table"
            :border="true"
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="total"
            :current.sync="search.current"
            :size.sync="search.size"
            :stripe="false"
            @getList="getList">
          </ai-table>
        </template>
      </ai-card>
      <ai-card title="投票统计" v-show="currIndex === 1">
        <template #content>
          <div id="chart"></div>
          <ai-table
            class="detail-table__table"
            :border="true"
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="total"
            :current.sync="search.current"
            :size.sync="search.size"
            :stripe="false"
            @getList="getList">
          </ai-table>
        </template>
      </ai-card>
      <ai-dialog
        :visible.sync="isShowAdd"
        width="680px"
        height="580px"
        title="发表意见"
        @close="onClose"
        @onConfirm="onConfirm">
        <el-form ref="form" class="ai-form" :model="form" label-width="110px" label-position="right">
          <el-form-item label="发表意见" prop="name" style="width: 100%;" :rules="[{ required: true, message: '请发表你的观点和意见', trigger: 'blur' }]">
            <el-input size="small" type="textarea" :rows="5" placeholder="请发表你的观点和意见" v-model="form.name"></el-input>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        id: '',
        search: {
          current: 1,
          size: 10
        },
        isShowAdd: false,
        form: {
          name: ''
        },
        total: 0,
        currIndex: 0,
        tableData: [],
        colConfigs: [
          {slot: 'userinfo'},
          {prop: 'addWay', label: '客户来源', align: 'center', formart: v => this.dict.getLabel('wxCustomerAddWay', v)},
          {prop: 'createTime', label: '添加时间', align: 'center'}
        ],
        tabList: ['议题信息', '意见征集']
      }
    },

    created () {
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appeveryvillagecode/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            // this.form = res.data
          }
        })
      },

      initChart(data) {
        const y = data.map(v => this.$dict.getLabel('fpRiskType', v.risk_type) || '其他')
        const v = data.map(v => v.c)
        const option = {
          tooltip: {},
          color: ['#2896FF', '#09DBFE', '#61FDB9', '#FFBB69', '#8429FF', '#ea7ccc'],
          grid: {
            top: '4%',
            left: '4%',
            right: '6%',
            bottom: '0%',
            containLabel: true
          },
          xAxis: {
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                width: 1,
                type: 'solid',
                color: '#f5f5f5'
              }
            },
            axisTick: {
              length: 1,
              show: true
            },
            axisLabel: {
              align: 'center',
              padding: [2, 0, 0, 0],
              interval: 0,
              fontSize: 14,
              color: '#999'
            },
            boundaryGap: true,
            axisLine: {
              show: true,
              lineStyle: {
                width: 1,
                type: 'solid',
                color: '#aaa'
              }
            },
          },
          yAxis: {
            type: 'category',
            data: y,
            boundaryGap: true,
            axisTick: {
              length: 0,
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e9e9e9'],
                width: 1,
                type: 'solid'
              }
            },
            nameTextStyle: {
              color: '#999',
              align: 'left'
            },
            axisLine: {
              show: true,
              lineStyle: {
                width: 1,
                type: 'solid',
                color: '#aaa'
              }
            },
            axisLabel: {
              color: '#999'
            }
          },
          series: [
            {
              name: '2011',
              type: 'bar',
              data: v
            }
          ]
        }

        this.chart.setOption(option)
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appminihomeconfig/addOrUpdate`, {
              ...this.form
            }).then(res => {
              if (res.code === 0) {
                this.$message.success('添加成功')
                this.isShowAdd = false

                this.getList()
              }
            })
          }
        })
      },

      getList () {

      },

      onClose () {

      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'list',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
