<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.unit"
          clearable
          size="small"
          placeholder="输入单位名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入单位人员姓名"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <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="unit">
          <el-input v-model="form.unit" 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>
        <div class="contact-box">
          <div class="title">
            <span class="label">通讯录：</span>
            <el-button size="mini" type="primary" @click="addContact(form.contacts)">
              添加联系人
            </el-button>
          </div>
          <div v-for="(item, index) of form.contacts" :key="item.name" class="contact-item">
            <div class="title" style="height: 60px">
              <span class="label">联系人{{ index + 1 }}：</span>
              <el-button size="mini" type="danger" @click="deleteContact(form.contacts, index)">
                删除联系人
              </el-button>
              <el-button size="mini" type="primary" @click="addPhone(item.phone)">
                添加联系方式
              </el-button>
            </div>
            <div style="display: flex">
              <div style="flex: 1">
                <el-form-item label="联系人" prop="name">
                  <el-input v-model="item.name" style="width: 175px" />
                </el-form-item>
              </div>
              <div style="flex: 1; display: flex; flex-direction: column; flex-wrap: wrap">
                <el-form-item
                  v-for="(num, i) of item.phone"
                  :key="num"
                  :label="'联系方式' + (i + 1)"
                  prop="phone"
                >
                  <el-input v-model="num.value" style="width: 175px" />
                  <i class="delete el-icon-delete-solid" @click="deletePhone(item.phone, i)"> </i>
                </el-form-item>
              </div>
            </div>

            <!-- <i class="delete el-icon-delete-solid" @click="deleteContact(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="unit" align="center"> </el-table-column>
      <el-table-column label="联系人" prop="name" align="center">
        <template slot-scope="{ row }">
          <span v-for="item of row.contacts" :key="item.name">{{ item.name }}<br /></span>
        </template>
      </el-table-column>
      <el-table-column label="电话" prop="phone" align="center">
        <template slot-scope="{ row }">
          <span v-for="item of row.contacts" :key="item.name">
            <span v-for="p of item.phone" :key="p">{{ p }}&nbsp;&nbsp;&nbsp;</span>
            <br />
          </span>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" width="60"></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'contact:edit', 'contact:del'])"
        label="操作"
        width="140px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudContact from '@/api/emergency/contact'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { getToken } from '@/utils/auth'
const defaultForm = {
  id: '',
  unit: '', //标题
  sort: 0, // 排序
  contacts: [{ name: '', phone: [''] }], // 状态
}

export default {
  name: 'contact',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '应急通讯录',
      url: 'emergency/contact/getList',
      // url: 'system/icon/getListJson',
      listOrder: [],
      crudMethod: { ...crudContact },
    })
  },
  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', 'contact:add'],
        edit: ['admin', 'contact:edit'],
        del: ['admin', 'contact:del'],
      },
      rules: {
        // name: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        // type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        // mac: [{ required: true, message: '请输入MAC', trigger: 'blur' }],
      },
    }
  },
  methods: {
    [CRUD.HOOK.beforeToCU]() {
      // console.log(this.form)
      this.form.contacts = this.form.contacts.map((c) => {
        return {
          name: c.name,
          phone: c.phone.map((p) => {
            return { value: p }
          }),
        }
      })
    },
    [CRUD.HOOK.beforeSubmit]() {
      this.form.contacts = this.form.contacts.map((c) => {
        return {
          name: c.name,
          phone: c.phone.map((p) => {
            return p.value
          }),
        }
      })
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    addContact(contacts) {
      const contact = { name: '', phone: [{ value: '' }] }
      contacts.push(contact)
    },
    deleteContact(contacts, index) {
      contacts.splice(index, 1)
    },
    addPhone(phones) {
      const phone = { value: '' }
      phones.push(phone)
    },
    deletePhone(phones, i) {
      phones.splice(i, 1)
    },
  },
}
</script>

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