<template>
  <div>
    <div class="content-head">
      <h3>新建短信群发</h3>
      <h5>新建短信群发</h5>
    </div>
    <div class="content">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" status-icon label-width="100px" class="demo-ruleForm">
        <el-form-item label="短信名称" prop="name" style="width:500px">
          <el-input v-model="ruleForm.name" placeholder="给短信群发起个名字"></el-input>
        </el-form-item>
        <el-form-item label="短信内容" prop="content" style="width:500px">
          <el-input
            v-model="ruleForm.content"
            type="textarea"
            placeholder="短信字符控制在70个，超过将分多条发送"
            maxlength="70"
            show-word-limit
            rows="3"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="选择群发对象" style="width:700px" prop="targetGroup">
          <el-button type="primary" @click="isForm=true">请选择发送对象</el-button>
          <dataTable 
            :table-data="ruleForm.tableData" 
            :columns="ruleForm.columns" 
            :breads="[]" 
            :checklist="[]" 
            :page-obj="pageObj"
            :margin-bottom="20" 
            :margin-top="50">
            <el-table-column slot="operate" label="操作" align="center" fixed="right" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleDelete(scope.row,scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </dataTable>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 250px;" @click="submitForm('ruleForm')">立即发送</el-button>
        </el-form-item>
      </el-form>
      <el-dialog :visible.sync="isForm" title="选择短信发送对象">
        <div class="flex-item absolute">
          <div>单位名称 </div>
          <el-input v-model="company" placeholder="请输入内容" prefix-icon="el-icon-search" style="width:200px">
          </el-input>
        </div>
        <ul class="flex navs" style="margin-bottom: 0;">
          <li v-for="(item,index) in navs" :key="index" :class="navIndex==index?'active':''" @click="changeNav(index)">{{ item }}</li>
        </ul>
        <div v-show="navIndex==0">
          <dataTable 
            :table-data="form" 
            :columns="formcolumns" 
            :breads="[]" 
            :checklist="[]" 
            :page-obj="pageObj"
            :margin-bottom="10" 
            :margin-top="10">
            <el-table-column slot="operate" label="操作" align="center" fixed="right" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleChoice(scope.row,scope.$index)">选择</el-button>
              </template>
            </el-table-column>
          </dataTable>
        </div>
        <div v-show="navIndex==1">
          <dataTable 
            :table-data="form" 
            :columns="formcolumns" 
            :breads="[]" 
            :checklist="[]"
            :page-obj="pageObj"
            :margin-bottom="10" 
            :margin-top="10">
            <el-table-column slot="operate" label="操作" align="center" fixed="right" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleChoice(scope.row,scope.$index)">选择</el-button>
              </template>
            </el-table-column>
          </dataTable>
        </div>
        <div class="flex-center">
          <el-button @click="isForm = false">关闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import dataTable from '@components/dataTable.vue'
export default {
  components: {
    dataTable
  },
  data() {
    // const validatePass = (rule, value, callback) => {
    //   if (this.ruleForm.tableData.length === '0') {
    //     callback(new Error('请选择发送对象'))
    //   } else {

    //     callback()
    //   }
    // }
    return {
      company: '',
      navs: ['采购商', '供应商'],
      navIndex: '0',
      formcolumns: [{
        label: '单位名称',
        prop: 'company'
      },
      {
        label: '用户姓名',
        prop: 'name'
      },
      {
        label: '手机号',
        prop: 'phone'
      },
      {
        label: '用户角色',
        prop: 'role'
      },
      {
        slot: 'operate'
      }
      ],
      form: [{
        company: '腾讯',
        name: '马爱华',
        phone: '17856785678',
        role: '管理员'
      },
      {
        company: '腾讯',
        name: '马爱华',
        phone: '17856785678',
        role: '管理员'
      },
      {
        company: '腾讯',
        name: '马爱华',
        phone: '17856785678',
        role: '管理员'
      }
      ],
      isForm: false,
      tableData: [],
      ruleForm: {
        title: '',
        content: '',
        columns: [{
          label: '单位名称',
          prop: 'company'
        },
        {
          label: '用户姓名',
          prop: 'name'
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '用户角色',
          prop: 'role'
        },
        {
          slot: 'operate'
        }
        ],
        tableData: []
      },
      rules: {
        name: {
          required: true,
          message: '请输入短信名称',
          trigger: 'blur'
        },
        content: {
          required: true,
          message: '请输入短信内容',
          trigger: 'blur'
        }
        // targetGroup: [{
        //   validator: validatePass,
        //   trigger: 'click'
        // }]
      },
      pageObj: {
        size: 10,
        total: 1,
        currentPage: 1,
        sizes: [100, 200, 300],
        func: (currentPage) => {
          this.pageTurning(currentPage)
        }
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    handleDelete(id, index) {
      this.$confirm('确定要删除么？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.$message.success('删除成功')
          this.ruleForm.tableData.splice(index, 1)
        })
        .catch(() => {})
    },
    // 选择
    handleChoice(row, index) {
      this.form.splice(index, 1)
      this.ruleForm.tableData.push(row)
    },
    changeNav(index) {
      this.navIndex = index
    }
  }
}
</script>

<style scoped>
	
	.absolute{
		position: absolute;
		right: 20%;
		top:17%
	}
</style>
