<template>
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <el-button v-permission="{key: 'gltd_refresh', moduleName: '管理团队'}"  type="warning" size="medium" plain @click="refresh">{{
        $t('message.deviceManage.refresh')
      }}</el-button>
      <el-button 
        v-permission="{key: 'gltd_add', moduleName: '管理团队'}" 
        type="primary" 
        size="medium" 
        @click="add">{{
        $t('message.deviceManage.add')
      }}</el-button>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List" height="800">
        <el-table-column
          width="200"
          prop="jobName"
          align="center"
          :label="$t('message.manageTeam.jobName')"
        ></el-table-column>
        <el-table-column
          prop="personName"
          :label="$t('message.manageTeam.personName')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="phone"
          :label="$t('message.manageTeam.phone')"
          align="center"
        ></el-table-column>
        <el-table-column
          :label="$t('message.manageTeam.imageUrl')"
          width="80"
          align="center"
        >
          <div slot slot-scope="scope">
            <img
              :preview="scope.row.imageUrl[0] ? scope.row.imageUrl[0].url : []"
              :src="scope.row.imageUrl[0] ? scope.row.imageUrl[0].url : []"
              alt=""
              width="50px"
            />
          </div>
        </el-table-column>
        <el-table-column
          :label="$t('message.deviceManage.operation')"
          align="center"
        >
          <template slot-scope="scope">
            <div class="tableBtns">
              <!--<div @click="yu(scope.row)" class="operationText"><img src="@/assets/images/yu.png" alt="">{{$t('message.alarmValueSet.yjz')}}
                        </div>
                        <div @click="bao(scope.row)" class="operationText"><img src="@/assets/images/bao.png" alt="">{{$t('message.alarmValueSet.bjz')}}
                        </div>-->
              <div v-permission="{key: 'gltd_edit', moduleName: '管理团队'}" @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.edit') }}</span>
              </div>
              <div v-permission="{key: 'gltd_delete', moduleName: '管理团队'}" @click="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>{{ $t('message.deviceManage.delete') }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :modal-append-to-body="false"
      @close="close"
      :title="$t('message.manageTeam.Popup_title')[Popup.type]"
      :visible.sync="Popup.show"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          v-if="Popup.type === 'add' || Popup.type === 'edit'"
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item
            :label="$t('message.manageTeam.jobName')"
            prop="jobName"
          >
            <!-- <el-select v-model="addEditForm.jobName"
                                   :placeholder="$t('message.deviceManage.placeholder_select')">
                            <el-option
                                    v-for="item in $t('message.manageTeam.jobNameArr')"
                                    :key="item.id"
                                    :label="item"
                                    :value="item">
                            </el-option>
                        </el-select> -->
            <el-input
              v-model="addEditForm.jobName"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('message.manageTeam.personName')"
            prop="personName"
          >
            <el-input
              v-model="addEditForm.personName"
              :disabled="Popup.type === 'edit'"
              :placeholder="$t('message.deviceManage.placeholder')"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('message.manageTeam.phone')" prop="phone">
            <el-input
              v-model="addEditForm.phone"
              :placeholder="$t('message.deviceManage.placeholder')"
              type="number"
              oninput="if(value.length>11)value=value.slice(0,11)"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('message.manageTeam.imageUrl')">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              :file-list="fileList"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item :label="$t('message.manageTeam.identityCard')">
            <el-input
              v-model="addEditForm.identityCard"
              :disabled="Popup.type === 'edit'"
              :placeholder="$t('message.deviceManage.placeholder')"
              maxlength="18"
            ></el-input>
          </el-form-item>
          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="Popup.show = false"
              icon="el-icon-circle-close"
              size="medium"
              >{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  projectManagementGroupAdd,
  projectManagementGroupEdit,
  projectManagementGroupDelete,
  projectManagementGroupList
} from '../../../assets/js/api/manageTeam'

import {
  checkPhone,
  IdentityCodeValid,
} from '@/assets/js/util.js'

export default {
  mounted() {
    this.getList()
  },
  data() {
    return {
      fileList: [],
      props: {
        multiple: true
      },
      addEditRules: {
        jobName: [
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'change'
          }
        ],
        personName: [
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'change'
          }
        ],
        phone: [
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'blur'
          },
          {
            required: true,
            message: this.$t('message.manageTeam.required'),
            trigger: 'change'
          }
        ]
      },
      options: [
        /*{realName: "曾祥万", value: 1},
                    {realName: "李先生", value: 2},*/
      ],
      addEditForm: {},
      List: [],
      Popup: {
        type: 'add',
        show: false
      }
    }
  },
  methods: {
    handle(type, show) {
      //打开弹窗前的统一处理
      this.Popup = {
        type: type,
        show: show
      }
    },
    add() {
      this.handle('add', true)
      this.fileList = []
    },
    edit(obj) {
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileList = obj.imageUrl
      // if (typeof obj.imageUrl === 'string') {
      //   this.addEditForm.imageUrl = JSON.parse(obj.imageUrl)
      //   this.fileList = this.addEditForm.imageUrl
      //   this.fileList.map((file) => {
      //     console.log('url', file.url)
      //     file.url = this.$store.state.FILEURL + file.url
      //   })
      // }
      // this.$forceUpdate();
      console.log('#编辑', obj)
      this.handle('edit', true)
    },
    deleteDev(obj) {
      console.log('删除', obj)
      this.$confirm(
        this.$t('message.personnelPosition.beaconManage.table.confirmText') +
        '【' +
        obj.personName +
        '】?',
        this.$t('message.personnelPosition.beaconManage.table.Tips'),
        {
          confirmButtonText: this.$t(
            'message.personnelPosition.confirmButtonText'
          ),
          cancelButtonText: this.$t(
            'message.personnelPosition.cancelButtonText'
          ),
          type: 'warning'
        }
      )
        .then(() => {
          projectManagementGroupDelete({ id: obj.id }).then((result) => {
            if (result.success) {
              this.$message.success(result.message)
              this.getList()
            }
          })
        })
        .catch(() => { })
    },
    submit() {
      if (this.addEditForm.phone) {
        if (!checkPhone(this.addEditForm.phone)) {
          this.$message.error('请输入正确的手机号')
          return
        }
      }

      if (this.addEditForm.identityCard) {
        if (!IdentityCodeValid(this.addEditForm.identityCard)) {
          this.$message.error('请输入正确的身份证号');
          return;
        }
      }

      this.processTheFile()
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.Popup.type === 'add') {
            projectManagementGroupAdd(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.Popup.type === 'edit') {
            console.log('编辑信息', this.addEditForm)
            projectManagementGroupEdit(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.Popup.show = false
        } else {
          return false
        }
      })
    },
    refresh() {
      this.getList()
    },
    getList() {
      projectManagementGroupList({
        projectSn: this.$store.state.projectSn
      }).then((result) => {
        if (result.success) {
          console.log('fileList', result.result)
          result.result.map((item) => {
            item.imageUrl = JSON.parse(item.imageUrl)
            if (
              item.imageUrl.length !== 0 &&
              !item.imageUrl[0].url.includes(this.$store.state.FILEURL)
            ) {
              item.imageUrl[0].url =
                this.$store.state.FILEURL + item.imageUrl[0].url
            }

            // if (item.imageUrl.length !== 0) {
            //   item.imageUrl[0].url =
            //     this.$store.state.FILEURL + item.imageUrl[0].url
            //   console.log('有图片', item.imageUrl)
            // }
            // console.log('有图片或者没图片', item.imageUrl)
          })
          this.List = result.result
          // this.$nextTick(() => {
          setTimeout(() => {
            this.$previewRefresh()
          }, 100)
          // })
          console.log('run', this.List)
        }
      })
    },
    getLaborManagementList() {
      getLaborManagementInfoList({
        projectSn: this.$store.state.projectSn,
        workerName: ''
      }).then((result) => {
        if (result.success) {
          console.log('劳务人员', result)
          this.options = result.result
        }
      })
    },
    close() {
      this.addEditForm = {}
      this.fileList = []
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    cktp(img) {
      this.$forceUpdate()
      console.log('img')
    },
    processTheFile() {
      //处理el上传的文件格式(结构)
      this.addEditForm.imageUrl = []
      this.fileList.map((item) => {
        if (!item.response) {
          this.addEditForm.imageUrl.push(item)
          // console.log('符合的结构', item)
        } else if (item.response) {
          // console.log('不符合的结构', item)
          this.addEditForm.imageUrl.push({
            name: item.response.data[0].filename,
            url: item.response.data[0].imageUrl
          })
        }
      })
      console.log('处理el上传的结构后', this.addEditForm.imageUrl)
      this.addEditForm.imageUrl = JSON.stringify(this.addEditForm.imageUrl)
      console.log('转字符串后', this.addEditForm.imageUrl)
      // console.log('处理el上传的结构后转字符串', JSON.stringify(this.registerInfo.licenseFile));
      // console.log('最终要上传的图片', this.registerInfo.licenseFile)
    },
    handleSuccess(response, file, fileList) {
      console.log('图片上传成功', fileList)
      this.fileList = fileList
    },
    handleRemove(response, fileList) {
      this.fileList = fileList
    }
  }
}
</script>
<style lang="less" scoped>
.tables {
  img {
    cursor: pointer;
  }
}

.tableBtns {
  display: flex;
  justify-content: center;
}

.yj-dialogFormBox {
  width: 462px;
}
::v-deep .el-input__inner {
  line-height: 1px !important;
}
</style>