<template>
  <el-dialog :title="modalTitle" :visible.sync="modalVisible" :width="formLabelWidth" top="2vh" @close="handleCancel">
    <div class="mobile">
      <div class="hc-box add">
        <div class="hc-box-header">贫困户信息</div>
        <div class="hc-box-wrap">
          <el-form label-width="100px" :model="form" :rules="rules" ref="from" class="from">
            <el-form-item label="编号:">
              <el-input type="text" v-model="form.farmerCode" :disabled="true" placeholder="类别值后台自动生成"></el-input>
            </el-form-item>
            <el-form-item label="姓名:" prop="farmerName">
              <el-input type="text" v-model="form.farmerName" placeholder="贫困户姓名" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="性别">
                  <el-radio-group v-model="form.sex" :disabled="limit.isRead">
                    <el-radio :label="item.id" v-for="(item,index) in sexLists">{{item.name}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="村名组号">
                    <el-input type="text" v-model="form.fkCollectiveId" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="民族">
                    <el-select v-model="form.nation" placeholder="请选择民族" :disabled="limit.isRead">
                      <el-option v-for="(item,index) in nationList" :key="index" :label="item.name" :value="item.name"></el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="人均年收入">
                    <el-input type="text" v-model="form.incomeVal" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="文化程度">
                    <el-input type="text" v-model="form.education" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话">
                    <el-input type="text" v-model="form.phone" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="家庭人数">
                    <el-input type="text" v-model="form.familyNum" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="身份证号">
                    <el-input type="text" v-model="form.pid" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="银行卡号">
                    <el-input type="text" v-model="form.bankCardNo" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="8">
                <el-form-item label="贫困户类型">
                    <el-select v-model="form.farmerType" placeholder="请贫困户类型" :disabled="limit.isRead">
                      <el-option
                        v-for="(item,index) in peopleTypeList"
                        :key="index"
                        :label="item.name"
                        :value="item.name"></el-option>
                    </el-select>
                  </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="银行名称">
                    <el-input type="text" v-model="form.bankName" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="银行机构代码">
                    <el-input type="text" v-model="form.bankCode" :disabled="limit.isRead"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="备注:">
                <el-input type="textarea"
                          :rows="3"
                          v-model="form.farmerRemark"
                          :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="帮扶区域" prop="fkPovertyAreaName">
              <el-input type="text" v-model="form.fkPovertyAreaName" :disabled="true"></el-input>
            </el-form-item>
            <div class="methodologyMain" v-if="modalAction !== 'view'">
                <div class="methodologyMain-header">
                  <el-form :inline="true" :model="searchParams" @submit.native.prevent>
                    <el-input
                      type="text"
                      v-model="searchParams.povertyAreaName"
                      size="mini"
                      clearable
                      placeholder="请输入帮扶区域"
                      style="width:200px;margin-right:10px;"></el-input>
                    <el-button
                      type="primary"
                      size="mini"
                      @click="onSearch">搜索</el-button>
                  </el-form>
                </div>
                <div class="methodologyMain-wrap">
                  <el-table   :data="areaList" v-loading="areaLoading" border>
                    <el-table-column label="序号" type="index" width="50" align="center" />
                    <el-table-column label="区域名称" align="center" prop="povertyAreaName" />
                    <el-table-column label="区域地址" align="center" prop="areaAddress"/>
                    <el-table-column label="区域描述" align="center" prop="areaDesc"/>
                    <el-table-column
                      label="操作"
                      align="center"
                      width="180"
                      class-name="small-padding fixed-width"
                    >
                      <template slot-scope="scope">
                        <el-button
                          size="mini"
                          type="success"
                          @click="selectArea(scope.row)"
                        >选择</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                  <pagination
                    v-show="total>0"
                    :total="total"
                    :page.sync="searchParams.pageNo"
                    :limit.sync="searchParams.pageSize"
                    @pagination="onSearch"
                  />
                </div>
            </div>

            <el-row :gutter="20">
              <el-col :span="24">
                  <el-divider content-position="left">贫困户图片</el-divider>
                  <div class="uploadImg">
                     <el-upload
                       v-if="!limit.isRead"
                       accept=".jpg, .png"
                       :headers="upload.headers"
                       :action="upload.url"
                       :disabled="upload.isUploading"
                       :on-progress="handleFileUploadProgress"
                       :on-success="handleFileSuccess"
                       :file-list="fileList">
                       <el-button size="small" type="primary">点击上传</el-button>
                       <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                     </el-upload>
                     <ul class="uploadImgList">
                       <li v-for="(item,index) in form.farmerImages" :key="index">
                         <img :src="imgRequest + item + '?imageView2/1/w/200/h/200'">
                         <span class="del" @click="handelImgRemove(index)" v-if="!limit.isRead"><i class="el-icon-delete-solid"></i></span>
                       </li>
                     </ul>
                  </div>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import {peopleType , nation , sexList} from "@/api/CONST.js"
import { listArea ,saverAreaPeople , updateAreaPeople} from "@/api/belongToSubject/people";
export default {
  name: 'addMobiel',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default: null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '1024px',
      peopleTypeList:peopleType,
      nationList:nation,
      sexLists:sexList,
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/upload/fileImg",
      },
      //验证
      rules: {
        farmerName: [{ required: true, message: '姓名不能为空!', trigger: 'blur' }],
        methodology: [{ required: true, message: '帮扶区域不能为空!', trigger: 'blur' }]
      },
      //图片列表
      fileList:[],
      restaurants:[],
      searchParams:{
        povertyAreaName:'',
        pageNo: 1,
        pageSize: 10
      },
      total:0,
      areaList:[],
      areaLoading:true,
      //表单
      form: {
        farmerImages:[],
        fkPovertyAreaId:'',
        fkPovertyAreaName:''
      }
    };
  },
  created() {
    this.onSearch()
    let datas;
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      case 'edit':
        this.limit.isAdd = true;
        console.log(this.modalSelectRow)
        datas = Object.assign({},this.modalSelectRow)
        datas.farmerImages = datas.farmerImages.split(';')
        datas.fkPovertyAreaName = datas.povertyArea.povertyAreaName
        this.form = datas
        break;
      case 'view':
        this.limit.isRead = true;
        datas = Object.assign({},this.modalSelectRow)
        datas.farmerImages = datas.farmerImages.split(';')
        datas.fkPovertyAreaName = datas.povertyArea.povertyAreaName
        this.form = datas
        break;
      default:
        break;
    }
  },
  methods: {
    //区域搜索
    onSearch(){
      this.areaLoading = true;
      listArea(this.searchParams).then(res => {
          this.areaLoading = false
          this.areaList = res.retBody
          this.total = res.retHead.total
      });
    },
    //区域确定
    selectArea(item){
      this.form.fkPovertyAreaId = item.guid
      this.form.fkPovertyAreaName = item.povertyAreaName
    },
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({}, this.form);
          params.farmerImages = params.farmerImages.join(";")
          console.log(params);
          if(this.modalAction === 'add'){
            saverAreaPeople(params).then(res => {
                console.log(res)
                if (res.retHead.errCode === 0) {
                    this.msgSuccess("操作成功");
                    this.modalVisible = false
                  	this.$emit('closeModal', true);
                }else{
                  this.msgError("保存失败，请检查!");
                }
            });
          }else{
            updateAreaPeople(params).then(res => {
                console.log(res)
                if (res.retHead.errCode === 0) {
                    this.msgSuccess("操作成功");
                    this.modalVisible = false
                  	this.$emit('closeModal', true);
                }else{
                  this.msgError("保存失败，请检查!");
                }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
       this.upload.isUploading = true;
    },
    // 上传成功处理
    handleFileSuccess(response, file, fileList){
       this.fileList = []
       this.form.farmerImages.push(response.retBody)
       this.upload.isUploading = false;
    },
    // 图片删除
    handelImgRemove(i){
      this.form.farmerImages.splice(i,1)
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>
.hc-box {
  border: 1px solid #e9e9e9;
  margin-bottom: 20px;
  .hc-box-header {
    height: 40px;
    line-height: 40px;
    background: #fafafa;
    font-weight: bold;
    color: #333;
    padding: 0 15px;
    margin-bottom:20px;
    .action {
      margin-left: 10px;
      color: #304ffe;
    }
  }
  .hc-box-wrap{
    .methodologyMain{
        padding:0 16px;
        &-header{
          margin-bottom:10px;
        }
    }
    .uploadImg{
      padding:0 30px 30px 30px;
      .uploadImgList{
        list-style:none;
        margin:0;
        padding:15px 0;
        display:flex;
        flex-flow: row;
        li{
          position:relative;
          border:1px solid #ddd;
          border-radius:6px;
          width:120px;
          height:120px;
          margin-right:13px;
          img{
            width:100%;
            height:100%;
            border-radius:6px;
          }
          .del{
            display:none;
            position:absolute;
            top: -9px;
            right: -9px;
            cursor:pointer;
            i{
              font-size:20px;
              color:red;
            }
          }
          &:hover{
            .del{
              display:block;
            }
          }
        }
      }
    }
  }
}
</style>
