<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入标题"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.type"
          filterable
          clearable
          size="small"
          placeholder="请选择类型"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item of options.typeArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="600px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" inline>
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.name" style="width: 470px" />
        </el-form-item>
        <el-form-item label="副标题" prop="subName">
          <el-input v-model="form.subName" style="width: 470px" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select
            v-model="form.type"
            filterable
            size="small"
            placeholder="请选择类型"
            class="filter-item"
            style="width: 190px"
          >
            <el-option
              v-for="item in options.typeArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" style="width: 190px" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model.number="form.sort"
            :min="0"
            :max="999"
            controls-position="right"
            style="width: 190px"
          />
        </el-form-item>
        <el-form-item label="展示人员" prop="showPerson">
          <el-select
            v-model="form.showPerson"
            filterable
            size="small"
            placeholder="请选择展示人员"
            class="filter-item"
            style="width: 190px"
          >
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>
        <div class="step-box">
          <div class="title">
            <span>步骤：</span>
            <el-button size="mini" type="primary" @click="addStep">添加步骤</el-button>
          </div>
          <div v-for="(step, index) of form.steps" :key="step.id" class="step-item">
            <el-form-item label="责任人" prop="personInCharge">
              <el-input v-model="step.personInCharge" style="width: 175px" />
            </el-form-item>
            <el-form-item label="排序" prop="sort">
              <el-input-number
                v-model.number="step.sort"
                :min="0"
                :max="999"
                controls-position="right"
                style="width: 175px"
              />
            </el-form-item>
            <el-form-item label="步骤内容" prop="content">
              <el-input type="textarea" v-model="step.content" style="width: 440px" />
            </el-form-item>
            <i class="delete el-icon-delete-solid" @click="deleteStep(index)"></i>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="标题" prop="name" align="center" width="400"> </el-table-column>
      <el-table-column label="副标题" prop="subName" align="center" width="400"></el-table-column>
      <el-table-column label="类型" prop="type" align="center" width="100"></el-table-column>
      <el-table-column label="编号" prop="code" align="center"></el-table-column>
      <el-table-column label="排序" prop="sort" align="center" width="60"></el-table-column>
      <el-table-column label="展示人员" prop="showPerson" align="center">
        <template slot-scope="{ row }">
          {{ row.showPerson == true ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        prop="baseModifyTime"
        align="center"
        :sortable="true"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'card:edit', 'card:del'])"
        label="操作"
        width="140px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="table-opt-box">
            <!-- <div class="slot-btn">
              <el-button size="mini" icon="el-icon-upload2" type="success"></el-button>
              <el-button size="mini" icon="el-icon-download" type="success"></el-button>
            </div> -->
            <udOperation :data="scope.row" :permission="permission" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <preview-PDF :dialogConfig="pdfConfig" />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudCard from '@/api/emergency/card'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { cardList } from '../tableConfig'
import { getToken } from '@/utils/auth'
import PreviewPDF from '@/components/PreviewPDF'
const defaultForm = {
  id: '',
  name: '', //标题
  subName: '', //副标题
  type: '', // 类型
  code: '', // 编号
  sort: 0, // 排序
  showPerson: true, //
  steps: [], // 状态
}

export default {
  name: 'card',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, PreviewPDF },
  cruds() {
    return CRUD({
      title: '应急处置卡',
      url: 'emergency/card/getList',
      // url: 'system/icon/getListJson',
      listOrder: [],
      crudMethod: { ...crudCard },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
    token() {
      return getToken() ?? ''
    },
  },
  data() {
    return {
      permission: {
        add: ['admin', 'card:add'],
        edit: ['admin', 'card:edit'],
        del: ['admin', 'card:del'],
      },
      rules: {
        name: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        mac: [{ required: true, message: '请输入MAC', trigger: 'blur' }],
      },
      options: {
        typeArr: [
          { label: '总部', text: '总部', value: '1' },
          { label: '合资公司', text: '合资公司', value: '2' },
          { label: '管理处', text: '管理处', value: '3' },
          { label: '维抢修中心', text: '维抢修中心', value: '4' },
        ],
      },
      fileList: [],
      pdfConfig: {
        show: false,
        url: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',
      },
    }
  },
  methods: {
    [CRUD.HOOK.afterRefresh]() {
      // this.crud.page.total = cardList.length
      // this.crud.data = cardList
      // this.crud.resetDataStatus()
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    addStep() {
      const newStep = {
        cardId: this.form.id,
        personInCharge: '',
        sort: 0,
        content: '',
      }
      this.form.steps.push(newStep)
    },
    deleteStep(index) {
      this.form.steps.splice(index, 1)
    },
  },
}
</script>

<style lang="scss" scoped>
.table-opt-box {
  display: flex;
  justify-content: center;
  .slot-btn {
    .el-button {
      margin-left: 0;
      margin-right: 8px;
    }
  }
}
.step-box {
  .title {
    height: 40px;
    display: flex;
    align-items: center;
    span {
      width: 80px;
      text-align: right;
      font-weight: 600;
      margin-right: 20px;
    }
  }
  .step-item {
    border: 1px solid #dcdfe6;
    padding-top: 20px;
    margin-top: 10px;
    // padding: 10px;
    position: relative;
    .delete {
      position: absolute;
      font-size: 16px;
      top: 8px;
      right: 8px;
      color: #f56c6c;
      cursor: pointer;
    }
  }
}
</style>
