<template>
  <div class="chain-project-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="20">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent />
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="4">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('服务编号')"
        prop="chain_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('服务名称')"
        prop="chain_project_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('配置图片')"
        prop="chain_project_image"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.chain_project_image"
            :preview-src-list="[scope.row.chain_project_image]"
            :src="scope.row.chain_project_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('可配置状态')"
        prop="chain_project_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="chain_project_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.chain_project_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="100"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, getList, updateState } from '@/api/chain/project/base'
  import Edit from './components/ChainProjectEdit'

  export default {
    name: 'ChainProject',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.chain_project_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      async sChange(row) {
        const param = {
          chain_project_id: row.chain_project_id,
          chain_project_enable: row.chain_project_enable,
        }
        const { msg, status } = await updateState(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },
      formatState(row) {
        const project_state = row.chain_project_state
        if (project_state === 401) {
          return this.__('忙碌')
        } else if (project_state === 402) {
          return this.__('适中')
        } else if (project_state === 403) {
          return this.__('空闲')
        }
      },
      handleDelete(row) {
        if (row.chain_project_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              chain_project_id: row.chain_project_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }

            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const chain_project_id = this.selectRows
              .map((item) => item.chain_project_id)
              .join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ chain_project_id })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },

      async enable(row, enable_name) {
        let param = {
          page_id: row.chain_project_id,
          enable_name: enable_name,
          enable_id: 'chain_project_id',
        }

        param[enable_name] = row[enable_name]
        const { msg, status } = await enable(param)

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
    },
  }
</script>
