<template>
  <div>
    <el-button size="small" type="primary" icon="el-icon-plus" @click="onShow">新 建</el-button>
    <el-drawer
        title="创建项目阶段"
        :visible.sync="drawer"
        size="35%"
        direction="rtl"
        append-to-body
        :show-close="false"
        @close="onClose">
      <div class="drawer__panel">
        <el-row>
          <el-form :model="form" :rules="rules" ref="form"  size="mini" label-position="right">
            <el-col :span="24">
              <el-form-item label="阶段名称:"  prop="stagename">
                <el-input v-model="form.stagename" autocomplete="off" placeholder="输入阶段名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="顺序" prop="sequence" >
                <el-input  v-model="form.sequence" autocomplete="off" placeholder="输入阶段顺序"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="说明:" prop="remarks">
                <el-input v-model="form.remarks" autocomplete="off" placeholder="输入阶段说明"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="请选择适用项目类型:" label-width="160px" prop="projecttype">
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="text-align:right">
                <el-checkbox v-model="checked" @change="checkedChange">适用所有项目类型</el-checkbox>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <layoutTable ref="projectType" :layout="tablecols" :data="list"  :custom="true" :checkbox="true" @checkboxCallBack="checkboxCallBack">
                <template v-slot:customcol="scope">
                  <p>{{scope.column.data[scope.column.columnname]}}</p>
                </template>
              </layoutTable>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import layoutTable from '@/components/table/index2'
export default {
  name: "add",
  components:{
    layoutTable
  },
  data(){
    return {
      drawer:false,
      checked:true,
      tablecols:[],
      list:[],
      selectType:[],
      total:0,
      form:{
        "sa_projstagemagid":0,
        "stagename": "",
        "remarks": "",
        "allprojecttype":0,
        "sequence":0,
        "projecttype":["水务","燃气"]
      },
      rules:{
        stagename: [
          { required: true, message: '阶段名称不能为空', trigger: 'blur' },
        ],
        remarks: [
          { required: true, message: '说明不能为空', trigger: 'blur' },
        ],
        projecttype: [
          { required: true, message: '还未选择项目类型', trigger: 'change' },
        ],
        sequence: [
          { required: true, message: '请输入顺序', trigger: 'change' },
        ],
      },
      param:{
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "projecttype",
          "parameter": {
          }
        }
      }
    }
  },
  methods:{
    onShow(){
      this.drawer = true
      this.typeData()
    },
    /*获取项目类型*/
    async typeData(){
      const res = await this.$api.requested(this.param)
      console.log(res,"项目类型")
      this.list = res.data
      this.total = res.total
      this.$nextTick(()=>{
        for (let i=0;i<this.list.length;i++){
          this.$refs.projectType.$refs.tables.toggleRowSelection(this.list[i],true)
        }
      })
    },
    onSubmit(){
      this.form.projecttype = this.selectType
      if (this.checked){
        this.form.allprojecttype = 1
      }else {
        this.form.allprojecttype = 0
      }
      console.log(this.form)
      this.$refs.form.validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221128143504",
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.drawer = false
          this.$refs.form.resetFields()
          this.form = {
            "sa_projstagemagid":0,
            "stagename": "",
            "remarks": "",
            "allprojecttype":0,
            "sequence":'',
            "projecttype":''
          }
          this.selectType = []
          this.$emit('onSuccess')
        })
      })
    },
    onClose(){
      this.drawer = false
      this.$refs.form.resetFields()
      this.form = {
        "sa_projstagemagid":0,
        "stagename": "",
        "remarks": "",
        "allprojecttype":0,
        "sequence":'',
        "projecttype":''
      }
      this.selectType = []
    },
    checkboxCallBack(val){
      this.selectType = []
      this.selectType = val.map(e=>{
        return {
          type:e.value,
          remarks:e.remarks
        }
      })
      this.$nextTick(()=>{
        if (this.selectType.length < this.total){
          this.checked = false
        }else {
          this.checked = true
        }
      })

    },
    checkedChange(){
      if (this.checked){
        for (let i=0;i<this.list.length;i++){
          this.$refs.projectType.$refs.tables.toggleRowSelection(this.list[i],true)
        }
      }else{
        for (let i=0;i<this.list.length;i++){
          this.$refs.projectType.$refs.tables.toggleRowSelection(this.list[i],false)
        }
      }
    }
  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).projectTypeTable.tablecols
  }
}
</script>

<style scoped>

</style>