<template>
  <a-card :bordered="false">
    <div v-show="visible">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="关键字">
                <a-input v-model.trim="queryParam.keyword" placeholder="请输入名称/类型名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8 || 24" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="small"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="action" slot-scope="record">
          <template>
            <a @click="handleView(record)">查看</a>
          </template>
        </span>
      </s-table>
    </div>
    <detail-audit-scrap :audit="false" ref="detailAuditScrapModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import AssignForm from './modules/AssignForm'
import DetailAuditScrap from '@/views/sb/scraps/modules/DetailAudit'
import { getTaskPageStart } from '@/api/activiti/activiti'
import { fetchSbScrapForm } from '@/api/sb/scraps'
import { fetchCustomFieldTemplateVOData, getCustomFieldTemplateDataPage } from '@/api/customize/fieldTemplateData'

export default {
  name: 'TaskList',
  components: {
    STable,
    Ellipsis,
    DetailAuditScrap,
    AssignForm
  },
  data () {
    return {
      mdl: {},
      // 查询参数
      queryParam: {
        type: this.DictCache.VALUE.CUSTOM_TEMPLATE_SB_REMARK_NUMBER.ACTIVITY_FORM
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        /*{
          title: '类型',
          dataIndex: 'targetCode',
          customRender: (text, record, index) => {
            if (text === 'supplier') {
              return '供应商审批'
            } else if (text === 'sb_info_scrap' || text === '设备报废审批') {
              return '设备报废审批'
            }
          }
        },
        {
          title: '任务名称',
          dataIndex: 'targetName'
        },
        {
          title: '申请人',
          dataIndex: 'userName'
        },
        {
          title: '创建时间',
          dataIndex: 'applyTime'
        },*/
        {
          title: '类型',
          dataIndex: 'remark',
          customRender: (text, record, index) => {
            return this.DictCache.VALUE.SB_INFO_AUDIT_MODEL_NAME[text]
            /*if (text === 'supplier') {
              return '供应商审批'
            } else if (text === 'SB_SCRAP_FORM') {
              return '设备报废审批'
            }*/
          }
        },
        {
          title: '任务名称',
          dataIndex: 'remark',
          customRender: (text, record, index) => {
            return this.DictCache.VALUE.SB_INFO_AUDIT_MODEL_NAME[text]
          }
        },
        {
          title: '申请人',
          dataIndex: 'createdUserName'
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime'
        },
        {
          title: '状态',
          dataIndex: 'status',
          customRender: (text, record, index) => {
            if (text === 1) {
              return '待提交'
            } else if (text === 2) {
              return '审核中'
            } else if (text === 3) {
              return '已完成'
            } else if (text === 4) {
              return '已拒绝'
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: '220px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getCustomFieldTemplateDataPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
        /*return getTaskPageStart(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })*/
      },
      selectedRowKeys: [],
      selectedRows: [],
      menus: [],
      visible: true,

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleView (record) {
      this.visible = false
      const templateData = JSON.parse(record.data)
      fetchCustomFieldTemplateVOData({ id: record.id }).then(res => {
        this.$refs.detailAuditScrapModal.base(res.data, templateData)
      })
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less" scoped>

</style>
