<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="姓名"
                v-model="listQuery.name"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="memberInfo_btn_add" style="margin-left: 10px;" @click="handleCreate"
                 type="primary" icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row
              style="width: 100%">
      <el-table-column align="center" label="序号" width="65">
        <template scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column width="80" align="center" label="姓名">
        <template scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column width="50" align="center" label="性别">
        <template scope="scope">
          <span v-if="scope.row.sex">女</span>
          <span v-else>男</span>
        </template>
      </el-table-column>
      <el-table-column width="110" align="center" label="卡号">
        <template scope="scope">
          <span>{{scope.row.cardNumber}}</span>
        </template>
      </el-table-column>

      <el-table-column width="150" align="center" label="生日">
        <template scope="scope">
          <span>{{scope.row.birthday}}</span>
        </template>
      </el-table-column>
      <el-table-column width="180" align="center" label="身份证号">
        <template scope="scope">
          <span>{{scope.row.idNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="证件地址">
        <template scope="scope">
          <span>{{scope.row.certificateAddress}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200" align="center" label="电子邮件">
        <template scope="scope">
          <span>{{scope.row.email}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200" align="center" label="微信">
        <template scope="scope">
          <span>{{scope.row.wx}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="会员类型">
        <template scope="scope">
          <span>{{memberTypeOptions[scope.row.memberType].label}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="联系电话">
        <template scope="scope">
          <span>{{scope.row.telephone}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="手机号码">
        <template scope="scope">
          <span>{{scope.row.mobilePhone}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="常住地址">
        <template scope="scope">
          <span>{{scope.row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="工作单位">
        <template scope="scope">
          <span>{{scope.row.workplace}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="职务">
        <template scope="scope">
          <span>{{scope.row.job}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="邀约人">
        <template scope="scope">
          <span>{{scope.row.inviter}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="职业">
        <template scope="scope">
          <span>{{scope.row.profession}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="支付类型">
        <template scope="scope">
          <span>{{payTypeOptions[scope.row.payType].label}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="会员签名日期">
        <template scope="scope">
          <span>{{scope.row.memberSignatureDate}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="经理">
        <template scope="scope">
          <span>{{scope.row.manager}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="经理签名日期">
        <template scope="scope">
          <span>{{scope.row.managerSignatureDate}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="家庭成员">
        <template scope="scope">
          <el-row v-for="item in scope.row.family">
            {{item.name}} - {{item.relationship}}
          </el-row>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="150">
        <template scope="scope">
          <el-button v-if="memberInfo_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="memberInfo_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select class="filter-item" v-model="form.sex" placeholder="请选择">
            <el-option v-for="item in sexOptions" :key="item.key" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入卡号"></el-input>
        </el-form-item>

        <el-form-item label="生日" prop="birthday">
          <el-date-picker v-model="form.birthday" type="date" @change="changeBirthday" placeholder="请输入生日" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>

        <el-form-item label="身份证号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号码"></el-input>
        </el-form-item>

        <el-form-item label="证件地址" prop="certificateAddress">
          <el-input v-model="form.certificateAddress" placeholder="请输入证件地址"></el-input>
        </el-form-item>

        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱"></el-input>
        </el-form-item>

        <el-form-item label="微信" prop="wx">
          <el-input v-model="form.wx" placeholder="请输入微信号"></el-input>
        </el-form-item>

        <el-form-item label="会员类型" prop="memberType">
          <el-select class="filter-item" v-model="form.memberType" placeholder="请选择">
            <el-option v-for="item in memberTypeOptions" :key="item.key" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话"></el-input>
        </el-form-item>

        <el-form-item label="手机" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入手机号码"></el-input>
        </el-form-item>

        <el-form-item label="常住地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入常住地址"></el-input>
        </el-form-item>

        <el-form-item label="工作单位" prop="workplace">
          <el-input v-model="form.workplace" placeholder="请输入工作单位"></el-input>
        </el-form-item>

        <el-form-item label="职务" prop="job">
          <el-input v-model="form.job" placeholder="请输入职务"></el-input>
        </el-form-item>

        <el-form-item label="邀约人" prop="inviter">
          <el-input v-model="form.inviter" placeholder="请输入邀约人"></el-input>
        </el-form-item>

        <el-form-item label="职业" prop="profession">
          <el-input v-model="form.profession" placeholder="请输入职业"></el-input>
        </el-form-item>

        <el-form-item label="支付方式" prop="payType">
          <el-select class="filter-item" v-model="form.payType" placeholder="请选择">
            <el-option v-for="item in payTypeOptions" :key="item.key" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="会员签名日期" prop="memberSignatureDate">
          <el-date-picker v-model="form.memberSignatureDate" type="date" @change="changeMemberSignatureDate" value-format="yyyy-MM-dd"
                          placeholder="请输入会员签名日期"></el-date-picker>
        </el-form-item>

        <el-form-item label="经理" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入经理姓名"></el-input>
        </el-form-item>

        <el-form-item label="经理签名日期" prop="managerSignatureDate">
          <el-date-picker v-model="form.managerSignatureDate" type="date" @change="changeManagerSignatureDate" value-format="yyyy-MM-dd"
                          placeholder="请输入经理签名日期"></el-date-picker>
        </el-form-item>

        <el-form-item label="家庭成员" prop="family">
          <el-row v-for="(item, index) in form.family">
            <el-col :span="9">
              <el-input v-model="item.name" placeholder="成员名称"></el-input>
            </el-col>
            <el-col class="line" :span="2" align="center">-</el-col>
            <el-col :span="9">
              <el-input v-model="item.relationship" placeholder="成员关系"></el-input>
            </el-col>
            <el-col :span="4" align="center">
              <el-button @click="handleDeleteFamily(index)" type="warning" size="mini" icon="el-tag__close el-icon-close"></el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-button @click="handleAddFamily()">添加</el-button>
          </el-row>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    page,
    addObj,
    getObj,
    delObj,
    putObj
  } from 'api/member/info/index'
  import { mapGetters } from 'vuex';

  export default {
    name: 'user',
    data() {
      return {
        form: {
          name: undefined,
          sex: false,
          cardNumber: undefined,
          birthday: undefined,
          idNumber: undefined,
          certificateAddress: undefined,
          email: undefined,
          wx: undefined,
          memberType: undefined,
          telephone: undefined,
          mobilePhone: undefined,
          address: undefined,
          workplace: undefined,
          job: undefined,
          inviter: undefined,
          profession: undefined,
          payType: undefined,
          memberSignatureDate: undefined,
          manager: undefined,
          managerSignatureDate: undefined,
          family: []
        },
        rules: {
          name: [
            {
              required: true,
              message: '请输入用户',
              trigger: 'blur'
            }
          ]
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        sexOptions: [
          {
            key: 'man',
            label: '男',
            value: false
          }, {
            key: 'woman',
            label: '女',
            value: true
          }
        ],
        memberTypeOptions: [
          {
            key: 0,
            label: '会员',
            value: 0
          },
          {
            key: 1,
            label: 'VIP会员',
            value: 1
          },
          {
            key: 2,
            label: '钻石会员',
            value: 2
          }
        ],
        payTypeOptions: [
          {
            key: 0,
            label: '现金',
            value: 0
          }, {
            key: 1,
            label: '刷卡',
            value: 1
          }, {
            key: 2,
            label: '微信支付',
            value: 2
          }, {
            key: 3,
            label: '支付宝支付',
            value: 3
          }, {
            key: 4,
            label: '银行转账',
            value: 4
          }],
        dialogFormVisible: false,
        dialogStatus: '',
        memberInfo_btn_edit: false,
        memberInfo_btn_del: false,
        memberInfo_btn_add: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    created() {
      this.getList();
      this.memberInfo_btn_edit = this.elements['memberInfo:btn_edit'];
      this.memberInfo_btn_del = this.elements['memberInfo:btn_del'];
      this.memberInfo_btn_add = this.elements['memberInfo:btn_add'];
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      changeBirthday(time) {
        this.form.birthday = time;
      },
      changeMemberSignatureDate(time) {
        this.form.memberSignatureDate = time;
      },
      changeManagerSignatureDate(time) {
        this.form.managerSignatureDate = time;
      },
      handleAddFamily() {
        this.form.family.push({
          name: undefined,
          relationship: undefined
        })
      },
      handleDeleteFamily(index) {
        console.log(index);
        this.form.family.splice(index, 1);
      },
      getList() {
        this.listLoading = true;
        page(this.listQuery)
          .then(response => {
            this.list = response.data.rows;
            this.total = response.data.total;
            this.listLoading = false;
          })
      },
      handleFilter() {
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        getObj(row.id)
          .then(response => {
            this.form = response.data;
            this.dialogFormVisible = true;
            this.dialogStatus = 'update';
          });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            delObj(row.id)
              .then(() => {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                });
                const index = this.list.indexOf(row);
                this.list.splice(index, 1);
              });
          });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form)
              .then(() => {
                this.dialogFormVisible = false;
                this.getList();
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
              })
          } else {
            return false;
          }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        this.$refs[formName].resetFields();
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false;
            this.form.password = undefined;
            putObj(this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      resetTemp() {
        this.form = {
          name: undefined,
          sex: false,
          cardNumber: undefined,
          birthday: undefined,
          idNumber: undefined,
          certificateAddress: undefined,
          email: undefined,
          wx: undefined,
          memberType: 0,
          telephone: undefined,
          mobilePhone: undefined,
          address: undefined,
          workplace: undefined,
          job: undefined,
          inviter: undefined,
          profession: undefined,
          payType: 0,
          memberSignatureDate: undefined,
          manager: undefined,
          managerSignatureDate: undefined,
          family: []
          // name: '张三',
          // sex: false,
          // cardNumber: '123456789',
          // birthday: '1999-04-01',
          // idNumber: '421000000000000000',
          // certificateAddress: '皇后大道',
          // email: 'xx@xx.com',
          // wx: '13412341234',
          // memberType: 0,
          // telephone: '0769-22223333',
          // mobilePhone: '13412341234',
          // address: '皇后大道',
          // workplace: '皇后大道',
          // job: '艺人',
          // inviter: undefined,
          // profession: '艺人',
          // payType: 0,
          // memberSignatureDate: '2019-04-01 12:12:12',
          // manager: '李四',
          // managerSignatureDate: '2019-04-01 12:12:12',
          // family: [
          //   {
          //     name: '熊大',
          //     relationship: '兄弟'
          //   },
          //   {
          //     name: '熊二',
          //     relationship: '兄弟'
          //   }
          // ]
        };
      }
    }
  }
</script>
